Skip to content

Commit

Permalink
#18 [Refactor] Dto 메서드 추출, Service, Controller 분리 리팩터링
Browse files Browse the repository at this point in the history
  • Loading branch information
JSoi committed Jul 11, 2022
1 parent ca921d3 commit b2c8e54
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
package com.mpnp.baechelin.api.controller;

import com.mpnp.baechelin.api.repository.MapQueryRepository;
import com.mpnp.baechelin.api.service.LocationService;
import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.store.repository.StoreRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequiredArgsConstructor
@Slf4j
Expand All @@ -24,26 +15,4 @@ public class LocationController {
private final LocationService locationService;
private final StoreRepository storeRepository;

// TODO 위도, 경도를 두 개 받아 해당 범위 안에 있는 Store 리턴하기
@GetMapping
public List<Store> giveStoreInRange(@RequestParam BigDecimal latStart,
@RequestParam BigDecimal latEnd,
@RequestParam BigDecimal lngStart,
@RequestParam BigDecimal lngEnd,
// sort 기준 정하기
//@PageableDefault(sort = {""}, direction = Sort.Direction.DESC) Pageable pageable){
@PageableDefault Pageable pageable) {
// TODO 페이징 적용
log.info("latStart : {}", latStart); // 위도
log.info("latEnd : {}", latEnd); // 위도
log.info("lngStart : {}", lngStart); // 경도
log.info("lngEnd : {}", lngEnd); // 경도
return locationService.giveStoresByRange(latStart,latEnd,lngStart,lngEnd,pageable);
}
@GetMapping("/test")
public List<Store> storeTest(){
return storeRepository.findAll();
}


}
17 changes: 5 additions & 12 deletions src/main/java/com/mpnp/baechelin/api/dto/PublicApiResponseDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.mpnp.baechelin.api.model.LocationKeywordSearchForm;
import com.mpnp.baechelin.store.domain.Store;
import lombok.*;

import java.math.BigDecimal;
import java.util.List;

@Builder
Expand Down Expand Up @@ -46,9 +48,9 @@ public static class Result {
public static class Row {
@JsonProperty("SEQ")
int SEQ;
String storeId;
String latitude;
String longitude;
Integer storeId;
BigDecimal latitude;
BigDecimal longitude;
String category;
@JsonProperty("SISULNAME")
String SISULNAME;
Expand Down Expand Up @@ -86,14 +88,5 @@ public static class Row {
String ST11;
@JsonProperty("ST12")
String ST12;
public Row updateLatLng(String lat, String lng){
this.latitude = lat;
this.longitude = lng;
return this;
}
public Row updateCategory(String category){
this.category = category;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.mpnp.baechelin.config.httpclient.HttpConfig;
import com.mpnp.baechelin.api.model.LocationKeywordSearchForm;
import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.api.repository.MapQueryRepository;
import com.mpnp.baechelin.store.controller.StoreQueryRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.*;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.stereotype.Service;
Expand All @@ -16,7 +14,6 @@
import org.springframework.web.util.UriComponentsBuilder;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.net.URI;
import java.util.*;

Expand All @@ -26,7 +23,7 @@
@Transactional
public class LocationService {
private final HttpConfig httpConfig;
private final MapQueryRepository mapQueryRepository;
private final StoreQueryRepository storeQueryRepository;
/**
* @param address 주소
* @return LocationKeywordSearchForm의 규격에 맞는 결과 하나를 가져옴
Expand Down Expand Up @@ -124,7 +121,4 @@ public LocationKeywordSearchForm giveCategoryByLatLngKeywordRest(String lat, Str
return resultRe.getBody();

}
public List<Store> giveStoresByRange(BigDecimal latStart, BigDecimal latEnd, BigDecimal lngStart, BigDecimal lngEnd, Pageable pageable) {
return mapQueryRepository.findBetweenLngLat(latStart, latEnd, lngStart, lngEnd, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriComponentsBuilder;
import software.amazon.ion.Decimal;

import javax.transaction.Transactional;
import java.io.IOException;
Expand Down Expand Up @@ -83,7 +84,7 @@ public class PublicApiService {
//
// }

public PublicApiResponseDto processApiToDBWithRestTemplate(PublicApiRequestDto publicApiRequestDto) throws UnsupportedEncodingException, JsonProcessingException {
public PublicApiResponseDto processApiToDBWithRestTemplate(PublicApiRequestDto publicApiRequestDto){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
Expand Down Expand Up @@ -132,20 +133,20 @@ private boolean setRowLngLat(PublicApiResponseDto.Row row) throws JsonProcessing
LocationKeywordSearchForm.Documents latLngDoc = Arrays.stream(latLngSearchForm.getDocuments()).findFirst().orElse(null);
if (latLngDoc == null)
return false;
row.setLatitude(latLngDoc.getY());
row.setLongitude(latLngDoc.getX());
row.setLatitude(Decimal.valueOf(latLngDoc.getY()));
row.setLongitude(Decimal.valueOf(latLngDoc.getX()));
// 카테고리 ENUM으로 전환하기
row.setCategory(categoryFilter(Optional.of(latLngDoc.getCategory_name()).orElse("기타")));
return true;
}

private void setRowCategoryAndId(PublicApiResponseDto.Row row) throws JsonProcessingException {
LocationKeywordSearchForm categorySearchForm = locationService.giveCategoryByLatLngKeywordRest(row.getLatitude(), row.getLongitude(), row.getSISULNAME());
LocationKeywordSearchForm categorySearchForm = locationService.giveCategoryByLatLngKeywordRest(String.valueOf(row.getLatitude()), String.valueOf(row.getLongitude()), row.getSISULNAME());
// LocationKeywordSearchForm categorySearchForm = locationService.giveCategoryByLatLngKeyword(row.getLatitude(), row.getLongitude(), row.getSISULNAME());
LocationKeywordSearchForm.Documents categoryDoc = Arrays.stream(categorySearchForm.getDocuments()).findFirst().orElse(null);
if (categoryDoc == null || !Arrays.asList("FD6", "CE7").contains(categoryDoc.getCategory_group_code()))
return;
row.setStoreId(categoryDoc.getId());
row.setStoreId(Integer.parseInt(categoryDoc.getId()));
row.setSISULNAME(categoryDoc.getPlace_name());
row.setCategory(categoryFilter(Optional.of(categoryDoc.getCategory_name()).orElse(null)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mpnp.baechelin.api.repository;
package com.mpnp.baechelin.store.controller;

import com.mpnp.baechelin.store.domain.Store;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -14,27 +14,27 @@

@Repository
@Transactional
public class MapQueryRepository extends QuerydslRepositorySupport {
public class StoreQueryRepository extends QuerydslRepositorySupport {
private final JPAQueryFactory queryFactory;

public MapQueryRepository(JPAQueryFactory queryFactory) {
public StoreQueryRepository(JPAQueryFactory queryFactory) {
super(Store.class);
this.queryFactory = queryFactory;
}


public List<Store> findBetweenLngLat(BigDecimal latStart,
BigDecimal latEnd,
BigDecimal lngStart,
BigDecimal lngEnd,
Pageable pageable) {
return queryFactory.selectFrom(store)
List<Store> storeList = queryFactory.selectFrom(store)
.where(store.latitude.goe(latStart),
store.latitude.loe(latEnd),
store.longitude.goe(lngStart),
store.longitude.loe(lngEnd))
.limit(pageable.getPageSize())
.offset(pageable.getOffset())
.fetch();
return storeList;
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/mpnp/baechelin/store/domain/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class Store {

public Store(PublicApiResponseDto.Row row) {
//storeId - 임시
this.id = Integer.parseInt(row.getStoreId());
this.id = row.getStoreId();
this.name = row.getSISULNAME();
this.address = row.getADDR();
this.phoneNumber = row.getTEL();
Expand All @@ -75,8 +75,8 @@ public Store(PublicApiResponseDto.Row row) {
//화장실
this.toilet = row.getST5();

this.latitude = new BigDecimal(row.getLatitude());
this.longitude = new BigDecimal(row.getLongitude());
this.latitude = row.getLatitude();
this.longitude = row.getLongitude();
this.category = row.getCategory();
}

Expand Down
60 changes: 31 additions & 29 deletions src/main/java/com/mpnp/baechelin/store/service/StoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,43 @@ public List<StoreResponseDto> getStoreList() {
List<StoreResponseDto> storeResponseList = new ArrayList<>();

for (Store store : storeList) {
List<Review> reviewList = reviewRepository.findAllByStoreId(store);
storeResponseList.add(storeToResDto(store));
}

return storeResponseList;
}

double totalPoint = 0;
public StoreResponseDto storeToResDto(Store store){
List<Review> reviewList = reviewRepository.findAllByStoreId(store);

double pointAvg = 0;
if (reviewList.size() > 0) {
for (Review review : reviewList) {
totalPoint += review.getPoint();
}
double totalPoint = 0;

pointAvg = Double.parseDouble(String.format("%.1f", totalPoint / reviewList.size()));
} else {
pointAvg = 0;
double pointAvg = 0;
if (reviewList.size() > 0) {
for (Review review : reviewList) {
totalPoint += review.getPoint();
}

StoreResponseDto storeResponse = StoreResponseDto.builder()
.storeId(store.getId())
.category(store.getCategory())
.name(store.getName())
.latitude(store.getLatitude())
.longitude(store.getLongitude())
.address(store.getAddress())
.elevator(store.getElevator())
.toilet(store.getToilet())
.parking(store.getParking())
.phoneNumber(store.getPhoneNumber())
.heightDifferent(store.getHeightDifferent())
.approach(store.getApproach())
.storeImgList(null)
.pointAvg(pointAvg)
.build();
storeResponseList.add(storeResponse);
pointAvg = Double.parseDouble(String.format("%.1f", totalPoint / reviewList.size()));
} else {
pointAvg = 0;
}


return storeResponseList;
return StoreResponseDto.builder()
.storeId(store.getId())
.category(store.getCategory())
.name(store.getName())
.latitude(store.getLatitude())
.longitude(store.getLongitude())
.address(store.getAddress())
.elevator(store.getElevator())
.toilet(store.getToilet())
.parking(store.getParking())
.phoneNumber(store.getPhoneNumber())
.heightDifferent(store.getHeightDifferent())
.approach(store.getApproach())
.storeImgList(null)
.pointAvg(pointAvg)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class UserInfoResponseDto {

public UserInfoResponseDto(User user) {
this.name = user.getName();
this.reviewList = user.getReviewList().stream().map(ReviewResponseDto::new).collect(Collectors.toList());
this.reviewList = user.getReviewsList().stream().map(ReviewResponseDto::new).collect(Collectors.toList());
this.bookmarkFolderList = user.getFolderList().stream().map(BookmarkFolderResponseDto::new).collect(Collectors.toList());
//folder

Expand Down
6 changes: 0 additions & 6 deletions src/main/java/com/mpnp/baechelin/user/entity/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,4 @@ public User(String socialId,
this.providerType = providerType;
this.roleType = roleType;
}

@OneToMany(mappedBy = "userId", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Review> reviewList;

@OneToMany(mappedBy = "userId", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Folder> folderList = new ArrayList<>();
}

0 comments on commit b2c8e54

Please sign in to comment.