Skip to content

Commit

Permalink
Merge branch 'feature/filter' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
JSoi committed Jul 15, 2022
2 parents b0fa3e6 + 8bf55ef commit d74df96
Show file tree
Hide file tree
Showing 19 changed files with 430 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mpnp.baechelin.config.httpclient.HttpConfig;
import com.mpnp.baechelin.api.dto.*;
import com.mpnp.baechelin.api.model.LocationKeywordSearchForm;
import com.mpnp.baechelin.store.domain.Category;
import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.store.repository.StoreRepository;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -44,7 +45,7 @@ public class PublicApiService {
private final StoreRepository storeRepository;
private final LocationService locationService;
// private final HttpConfig httpConfig;

//
// public PublicApiResponseDto processApiToDBWithWebclientMono(PublicApiRequestDto publicApiRequestDto) throws UnsupportedEncodingException {
// WebClient client = WebClient.builder()
// .baseUrl("http://openapi.seoul.go.kr:8088")
Expand Down Expand Up @@ -166,9 +167,9 @@ private void saveDTO(List<PublicApiResponseDto.Row> rows) {

private String categoryFilter(String category) {
if (category == null) {
return "기타";
return Category.ETC.getDesc();
} else if (category.contains(">")) {
return category.split(" > ")[1];
return Category.giveCategory(category.split(" > ")[1]).getDesc();
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.mpnp.baechelin.bookmark.dto.BookmarkRequestDto;
import com.mpnp.baechelin.bookmark.service.BookmarkService;
import com.mpnp.baechelin.oauth.entity.UserPrincipal;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -14,8 +17,10 @@ public class BookmarkController {

/** 북마크 생성 폴더 담기 */
@PostMapping("/bookmark")
public void bookmark(@RequestBody BookmarkRequestDto bookmarkRequestDto){

bookmarkService.bookmark(bookmarkRequestDto);
public void bookmark(@RequestBody BookmarkRequestDto bookmarkRequestDto, @AuthenticationPrincipal User user){
if(user==null){
throw new IllegalArgumentException("로그인 해주세요!");
}
bookmarkService.bookmark(bookmarkRequestDto,user.getUsername());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.mpnp.baechelin.bookmark.domain.Bookmark;
import com.mpnp.baechelin.bookmark.domain.Folder;
import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface BookmarkRepository extends JpaRepository<Bookmark, Integer> {
List<Bookmark> findAllByFolderId(Folder folderId);
boolean existsByStoreIdAndUserId(Store store, User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.mpnp.baechelin.bookmark.repository.FolderRepository;
import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.store.repository.StoreRepository;
import com.mpnp.baechelin.user.domain.User;
import com.mpnp.baechelin.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -17,9 +19,10 @@ public class BookmarkService {
private final BookmarkRepository bookmarkRepository;
private final FolderRepository folderRepository;
private final StoreRepository storeRepository;
private final UserRepository userRepository;

public void bookmark(BookmarkRequestDto bookmarkRequestDto) {

public void bookmark(BookmarkRequestDto bookmarkRequestDto, String socialId) {
User user = userRepository.findBySocialId(socialId);
Folder folder = folderRepository.findById(bookmarkRequestDto.getFolderId())
.orElseThrow(()-> new IllegalArgumentException("폴더가 존재하지 않습니다"));
Store store = storeRepository.findById(bookmarkRequestDto.getStoreId())
Expand All @@ -29,6 +32,7 @@ public void bookmark(BookmarkRequestDto bookmarkRequestDto) {
.builder()
.folderId(folder)
.storeId(store)
.userId(user)
.build();

storeRepository.save(store.updateBookmarkCount(1));
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/com/mpnp/baechelin/config/QuerydslConfig.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.mpnp.baechelin.config;

import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.store.dto.StoreCardResponseDto;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.StringPath;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Pageable;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.math.BigDecimal;
import java.util.List;

import static com.mpnp.baechelin.store.domain.QStore.store;

Expand All @@ -22,12 +28,4 @@ public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(entityManager);
}

public static BooleanBuilder locationBuilder(BigDecimal latStart, BigDecimal latEnd, BigDecimal lngStart, BigDecimal lngEnd) {
BooleanBuilder builder = new BooleanBuilder();
builder.and(latStart == null ? null : store.latitude.goe(latStart));
builder.and(latEnd == null ? null : store.latitude.loe(latEnd));
builder.and(lngStart == null ? null : store.longitude.goe(lngStart));
builder.and(lngEnd == null ? null : store.longitude.loe(lngEnd));
return builder;
}
}
90 changes: 90 additions & 0 deletions src/main/java/com/mpnp/baechelin/config/QuerydslLocation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.mpnp.baechelin.config;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.StringPath;
import org.springframework.data.domain.Pageable;

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

import static com.mpnp.baechelin.store.domain.QStore.store;

public class QuerydslLocation {
public static BooleanBuilder locationBuilder(BigDecimal latStart, BigDecimal latEnd, BigDecimal lngStart, BigDecimal lngEnd) {
BooleanBuilder builder = new BooleanBuilder();
builder.and(latStart == null ? null : store.latitude.goe(latStart));
builder.and(latEnd == null ? null : store.latitude.loe(latEnd));
builder.and(lngStart == null ? null : store.longitude.goe(lngStart));
builder.and(lngEnd == null ? null : store.longitude.loe(lngEnd));
return builder;
}

private static BooleanExpression facilityTF(String facility) {
if (facility == null || facility.isEmpty()) return null;
return givePath(facility).eq("Y");
}

private static StringPath givePath(String dbFacility) {
if (dbFacility.equals("elevator"))
return store.elevator;
if (dbFacility.equals("heightDifferent"))
return store.heightDifferent;
if (dbFacility.equals("parking"))
return store.parking;
if (dbFacility.equals("approach"))
return store.approach;
if (dbFacility.equals("toilet"))
return store.toilet;
throw new IllegalArgumentException("배리어 프리 태그를 확인해주세요");
}

public static BooleanBuilder locAndConditions(BigDecimal latStart, BigDecimal latEnd, BigDecimal lngStart, BigDecimal lngEnd, String category, List<String> facility) {
BooleanBuilder builder = locationBuilder(latStart, latEnd, lngStart, lngEnd);
return getBooleanBuilder(category, facility, builder);
}


public static BooleanBuilder locTwoPointAndConditions(BigDecimal latitude, BigDecimal longitude, String category, List<String> facility) {
BooleanBuilder builder = new BooleanBuilder();
if (latitude != null && longitude != null) {
BigDecimal[] location = getRange(latitude, longitude, 20);
builder = locationBuilder(location[0], location[1], location[2], location[3]);
}
return getBooleanBuilder(category, facility, builder);
}

public static BooleanBuilder getBooleanBuilder(String category, List<String> facility, BooleanBuilder builder) {
builder.and(category == null ? null : store.category.eq(category));
if (facility != null && facility.size() > 0) {
for (String fac : facility) {
builder.and(facilityTF(fac));
}
}
return builder;
}

public static BigDecimal[] getRange(BigDecimal lat, BigDecimal lng, int km) {
// km->lat,lng로 변환하기
final BigDecimal latitude = BigDecimal.valueOf(km / 110.569); // 반경
final BigDecimal longitude = BigDecimal.valueOf(km / 111.322);
// 남서, 북동으로 받아오기
// start lat-lng, end lat-lng으로 Array 받아오기
return new BigDecimal[]{lat.subtract(latitude), lat.add(latitude),
lng.subtract(longitude), lng.add(longitude)};
}



public static int[] getStartEndPage(List<?> resultList, Pageable pageable){
int totalCount = resultList.size();
int totalPage = totalCount / pageable.getPageSize();
int pageStartIndex = Long.valueOf(resultList.size() / pageable.getPageSize() * pageable.getOffset()).intValue();

// index 처리하기
int start = 0, end = resultList.size();
start = Math.max(start, pageStartIndex);
end = Math.min(end, pageStartIndex + pageable.getPageSize() - 1);
return new int[]{start, end};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
public class ReviewController {

private final ReviewService reviewService;
private final ReviewQueryRepository reviewQueryRepository;

@GetMapping("/review/{storeId}")
public ResponseEntity<List<ReviewResponseDto>> getStoreReview(@PathVariable int storeId) {
Expand Down Expand Up @@ -57,31 +56,13 @@ public ResponseEntity<?> review(@ModelAttribute ReviewRequestDto reviewRequestDt
// return new ResponseEntity<>(HttpStatus.OK);
// }

// TODO - 최근 등록한 리뷰 보여주기
// TODO - 최근 등록한 리뷰 보여주기 - 로그인 불필요
// 반경 넓히기
@GetMapping("/recent-review")
public List<ReviewMainResponseDto> recentReview(@RequestParam(required = false) BigDecimal lat,
@RequestParam(required = false) BigDecimal lng,
@RequestParam int limit) {

BigDecimal[] locationRange = getRange(lat, lng, 20);


List<ReviewMainResponseDto> result = reviewQueryRepository
.findRecentReviews(locationRange[0], locationRange[1], locationRange[2], locationRange[3], limit)
.stream().map(ReviewMainResponseDto::new).collect(Collectors.toList());
return result;
}

// km : 반경
private BigDecimal[] getRange(BigDecimal lat, BigDecimal lng, int km) {
// km->lat,lng로 변환하기
final BigDecimal latitude = BigDecimal.valueOf(km / 110.569); // 반경
final BigDecimal longitutde = BigDecimal.valueOf(km / 111.322);
// 남서, 북동으로 받아오기
// start lat-lng, end lat-lng으로 Array 받아오기
return new BigDecimal[]{lat.subtract(latitude), lat.add(latitude),
lng.subtract(longitutde), lng.add(longitutde)};
return reviewService.getRecentReview(lat, lng, limit);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mpnp.baechelin.review.dto;

import com.mpnp.baechelin.review.domain.Review;
import com.mpnp.baechelin.store.domain.Store;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -15,11 +16,16 @@
@Builder
public class ReviewMainResponseDto {
// review 테이블 컬럼
private int storeId;
private int userId;
private String storeName;
private String userName;
private String comment; //리뷰 코멘트
private double point; //별점
private List<ReviewImageResponseDto> imageFileUrl; //리뷰 이미지 사진

public ReviewMainResponseDto(Review review) {
// this.storeId = store.getId();
this.comment = review.getContent();
this.point = review.getPoint();
this.imageFileUrl = review.getReviewImageList().parallelStream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.mpnp.baechelin.review.domain.Review;
import com.mpnp.baechelin.tag.domain.Tag;
import com.mpnp.baechelin.user.domain.User;
import lombok.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Builder
Expand All @@ -23,7 +25,6 @@ public class ReviewResponseDto {

private LocalDateTime createdAt;
private LocalDateTime modifiedAt;

private List<TagResponseDto> tagList;

public ReviewResponseDto(Review review) {
Expand All @@ -35,8 +36,8 @@ public ReviewResponseDto(Review review) {
this.createdAt = review.getCreatedAt();
this.modifiedAt = review.getModifiedAt();
this.reviewImageUrlList = review.getReviewImageList()
.stream().map(ReviewImageResponseDto::new).collect(Collectors.toList());
this.tagList = review.getTagList().stream().map(TagResponseDto::new).collect(Collectors.toList());
.parallelStream().map(ReviewImageResponseDto::new).collect(Collectors.toList());
this.tagList = review.getTagList().parallelStream().map(TagResponseDto::new).collect(Collectors.toList());
}
@Builder
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.mpnp.baechelin.review.repository;

import com.mpnp.baechelin.config.QuerydslLocation;
import com.mpnp.baechelin.review.domain.Review;
import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.user.domain.User;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;

import static com.mpnp.baechelin.config.QuerydslConfig.locationBuilder;
import static com.mpnp.baechelin.review.domain.QReview.review;
import static com.mpnp.baechelin.store.domain.QStore.store;

Expand All @@ -24,22 +27,19 @@ public ReviewQueryRepository(JPAQueryFactory queryFactory) {
this.queryFactory = queryFactory;
}

public List<Review> findRecentReviews(BigDecimal latStart,
BigDecimal latEnd,
BigDecimal lngStart,
BigDecimal lngEnd,
int limit) {
BooleanBuilder builder = locationBuilder(latStart, latEnd, lngStart, lngEnd);
public List<Review> findRecentReviews(BigDecimal lat,
BigDecimal lng,
int limit) {
BigDecimal[] location = QuerydslLocation.getRange(lat, lng, 20);
BooleanBuilder builder = QuerydslLocation.locationBuilder(location[0], location[1], location[2], location[3]);
// 위도 경도에 해당하는 가게를 찾음 -> 해당 댓글을 다 가져옴 -> 내림차순 정렬 -> limit
// TODO 쿼리문 개선하기
return queryFactory.selectFrom(review)
.innerJoin(review.storeId, store)
.leftJoin(review.storeId, store)
.on(review.storeId.id.eq(store.id))
.where(builder)
.orderBy(review.createdAt.desc())
.limit(limit)
.fetch();
}



}
Loading

0 comments on commit d74df96

Please sign in to comment.