Add mapper for entity to dto

This commit is contained in:
Zahra Karimi 2024-11-13 14:25:14 +03:30
parent 6bcb203243
commit 43b578d298
10 changed files with 174 additions and 1 deletions

16
pom.xml
View File

@ -59,6 +59,22 @@
<artifactId>h2</artifactId> <artifactId>h2</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>
<!-- MapStruct annotation processor -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>

View File

@ -0,0 +1,12 @@
package ir.chaarfasl.back.api.core.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/attractions")
public class AttractionController {
}

View File

@ -0,0 +1,12 @@
package ir.chaarfasl.back.api.core.dataAccess.repository;
import ir.chaarfasl.back.api.core.dataAccess.entity.Attraction;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface AttractionRepository extends JpaRepository<Attraction, Long> {
List<Attraction> findByLatitudeBetweenAndLatitudeBetween(Double lat1, Double lat2, Double long1, Double long2);
}

View File

@ -0,0 +1,17 @@
package ir.chaarfasl.back.api.core.dto;
import ir.chaarfasl.back.api.core.dataAccess.entity.Province;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class AttractionDto extends PointDto{
private String bestTime;
private Float hardnessDegree;
public AttractionDto(Long id, String name, String description, Double latitude, Double longitude, Province province, Float rate) {
super(id, name, description, latitude, longitude, province, rate);
}
}

View File

@ -0,0 +1,15 @@
package ir.chaarfasl.back.api.core.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Coordinates {
protected Double latitude;
protected Double longitude;
}

View File

@ -0,0 +1,19 @@
package ir.chaarfasl.back.api.core.dto;
import ir.chaarfasl.back.api.core.dataAccess.entity.Province;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class PointDto {
private Long id;
private String name;
private String description;
private Double latitude;
private Double longitude;
private Province province;
private Float rate;
}

View File

@ -0,0 +1,15 @@
package ir.chaarfasl.back.api.core.mapper;
import ir.chaarfasl.back.api.core.dataAccess.entity.Attraction;
import ir.chaarfasl.back.api.core.dto.AttractionDto;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper(componentModel = "spring")
public interface AttractionMapper {
AttractionMapper INSTANCE = Mappers.getMapper(AttractionMapper.class);
AttractionDto attractionToAttractionDto(Attraction attraction);
Attraction attractionDtoToAttraction(AttractionDto attractionDto);
}

View File

@ -0,0 +1,33 @@
package ir.chaarfasl.back.api.core.service;
import ir.chaarfasl.back.api.core.dataAccess.entity.Attraction;
import ir.chaarfasl.back.api.core.dataAccess.repository.AttractionRepository;
import ir.chaarfasl.back.api.core.dto.Coordinates;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AttractionService {
private static final Logger log = LoggerFactory.getLogger(new Object() {}.getClass().getEnclosingClass());
AttractionRepository attractionRepository;
public AttractionService(AttractionRepository attractionRepository) {
this.attractionRepository = attractionRepository;
}
public List<Attraction> getAttractions(Coordinates leftTop, Coordinates rightDown){
List<Attraction> resultPoints = null;
try {
resultPoints = attractionRepository.findByLatitudeBetweenAndLatitudeBetween(leftTop.getLatitude(), rightDown.getLatitude(), leftTop.getLongitude(), rightDown.getLongitude());
} catch (Exception e){
log.error(e.getMessage());
throw e;
}
return resultPoints;
}
}

View File

@ -6,4 +6,7 @@ spring.datasource.username=zahra
spring.datasource.password=password spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.generate-ddl=true spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop #spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

View File

@ -0,0 +1,31 @@
package ir.chaarfasl.back.api.core;
import com.fasterxml.jackson.databind.ObjectMapper;
import ir.chaarfasl.back.api.core.controller.AttractionController;
import ir.chaarfasl.back.api.core.dto.Coordinates;
import ir.chaarfasl.back.api.core.service.AttractionService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
@WebMvcTest(AttractionController.class)
public class AttractionControllerTest {
private static final String END_POINT_PATH = "/attractions";
@Autowired
MockMvc mockMvc;
@Autowired
ObjectMapper mapper;
@MockBean
AttractionService attractionService;
@Test
public void testGet() throws Exception {
/*Coordinates coordinates = new Coordinates();
mockMvc.perform(get)*/
}
}