Skip to content

Commit

Permalink
#3 feat: 포장마차 좋아요 목록 조회 API
Browse files Browse the repository at this point in the history
  • Loading branch information
ryr0121 committed Dec 9, 2023
1 parent 8f18c7a commit a064658
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/s1350/sooljangmacha/global/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ public class Constants {
public static final String AUTHORIZATION_HEADER = "Authorization";
public static final String BEARER_PREFIX = "bearer ";
public static final String CLAIM_NAME = "userId";
public static final String REVIEW = "후기순";
public static final String DESC = "최신순";
public static final String LIKE = "좋아요순";
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class StoreController {
})
public BaseResponse<List<GetStoreListRes>> getStores(@Parameter(hidden = true) @UserAccount User user,
@Parameter(description = "(String) 카테고리(좋아요순/후기순/최신순)", example = "후기순", required = true) @RequestParam(name = "category") String category){
return BaseResponse.OK(storeService.getStores(category));
return BaseResponse.OK(storeService.getStores(user, category));
}

@Operation(summary = "[윤희슬] 포장마차 상세 조회", description = "포장마차를 상세조회한다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ public class GetStoreListRes {
private Long likeCount;
@Schema(type = "Integer", description = "상점 후기 수", example = "10")
private Integer reviewCount;
@Schema(type = "Boolean", description = "상점 좋아요 유무", example = "true")
private Boolean isLike;

public static GetStoreListRes toDto(Store store, Long likeCount){
public static GetStoreListRes toDto(Store store, Long likeCount, boolean isLike){
String imgUrl = getThumbnail(store);
return GetStoreListRes.builder()
.name(store.getName())
Expand All @@ -48,6 +50,7 @@ public static GetStoreListRes toDto(Store store, Long likeCount){
.imgUrl(imgUrl)
.likeCount(likeCount)
.reviewCount(store.getStoreReviewList().size())
.isLike(isLike)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
public interface StoreLikeRepository extends JpaRepository<StoreLike, Long> {
Optional<StoreLike> findByUserAndStore(User user, Store store);

@Query("SELECT COUNT(s.id) FROM StoreLike s WHERE s.isEnable = 1 and s.store = :store")
@Query("SELECT COUNT(s.id) FROM StoreLike s WHERE s.isEnable = true and s.store = :store")
Long getLikeCountByIsEnable(@Param("store") Store store);

boolean existsByUserAndStoreAndIsEnable(User user, Store store, boolean isEnable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.s1350.sooljangmacha.global.Constants.*;
import static com.s1350.sooljangmacha.global.exception.BaseResponseCode.REQUEST_VALIDATION;

@Service
Expand All @@ -35,14 +36,15 @@ public class StoreService {
private final StoreLikeRepository storeLikeRepository;
private final StoreImgRepository storeImgRepository;
private final StoreReviewRepository storeReviewRepository;
private static final String REVIEW = "후기순";
private static final String DESC = "최신순";
private static final String LIKE = "좋아요순";

// 위치별 포장마차 전체 조회
public List<GetStoreListRes> getStores(String category) {
public List<GetStoreListRes> getStores(User user, String category) {
Stream<GetStoreListRes> getStoreListResStream = storeRepository.findAllByIsEnable(true).stream()
.map(store -> GetStoreListRes.toDto(store, storeLikeRepository.getLikeCountByIsEnable(store)));
.map(store -> GetStoreListRes.toDto(store, storeLikeRepository.getLikeCountByIsEnable(store), storeLikeRepository.existsByUserAndStoreAndIsEnable(user, store, true)));
return sortStoreList(category, getStoreListResStream);
}

public List<GetStoreListRes> sortStoreList(String category, Stream<GetStoreListRes> getStoreListResStream) {
switch (category) {
case REVIEW:
return getStoreListResStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.s1350.sooljangmacha.global.dto.BaseResponse;
import com.s1350.sooljangmacha.global.resolver.UserAccount;
import com.s1350.sooljangmacha.store.dto.response.GetStoreListRes;
import com.s1350.sooljangmacha.user.dto.request.LoginReq;
import com.s1350.sooljangmacha.user.dto.request.PatchProfileReq;
import com.s1350.sooljangmacha.user.dto.request.SignupReq;
Expand All @@ -21,6 +22,7 @@
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Tag(name = "users", description = "유저 API")
@RestController
Expand Down Expand Up @@ -80,7 +82,15 @@ public BaseResponse patchProfile(@Parameter(hidden = true) @UserAccount User use
return BaseResponse.OK();
}

// @Operation(summary = "포장마차 좋아요 목록 조회", description = "")
// @GetMapping("/likes")
@Operation(summary = "[김초원] 포장마차 좋아요 목록 조회", description = "마이페이지의 좋아요 한 포차 조회")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "(S0001)요청에 성공했습니다."),
@ApiResponse(responseCode = "404", description = "(E0001)잘못된 요청입니다.", content = @Content(schema = @Schema(implementation = BaseResponse.class))),
})
@GetMapping("/likes")
public BaseResponse<List<GetStoreListRes>> getStoreOfLike(@Parameter(hidden = true) @UserAccount User user,
@Parameter(description = "(String) 카테고리(좋아요순/후기순/최신순)", example = "후기순", required = true) @RequestParam(name = "category") String category) {
return BaseResponse.OK(userService.getStoreOfLike(user, category));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.s1350.sooljangmacha.global.exception.BaseException;
import com.s1350.sooljangmacha.global.exception.BaseResponseCode;
import com.s1350.sooljangmacha.global.utils.JwtUtil;
import com.s1350.sooljangmacha.store.dto.response.GetStoreListRes;
import com.s1350.sooljangmacha.store.repository.StoreLikeRepository;
import com.s1350.sooljangmacha.store.service.StoreService;
import com.s1350.sooljangmacha.user.dto.request.LoginReq;
import com.s1350.sooljangmacha.user.dto.request.PatchProfileReq;
import com.s1350.sooljangmacha.user.dto.request.SignupReq;
Expand All @@ -16,12 +19,17 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Stream;


@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;
private final JwtUtil jwtUtil;
private final StoreLikeRepository storeLikeRepository;
private final StoreService storeService;

// 로그인
public LoginRes login(LoginReq request) {
Expand Down Expand Up @@ -56,4 +64,9 @@ public void patchProfile(User user, PatchProfileReq request) {
// 프로필 편집

// 포장마차 좋아요 목록 조회
public List<GetStoreListRes> getStoreOfLike(User user, String category) {
Stream<GetStoreListRes> likeStoreList = user.getStoreLikeList().stream()
.map(sl -> GetStoreListRes.toDto(sl.getStore(), storeLikeRepository.getLikeCountByIsEnable(sl.getStore()), storeLikeRepository.existsByUserAndStoreAndIsEnable(user, sl.getStore(), true)));
return storeService.sortStoreList(category, likeStoreList);
}
}

0 comments on commit a064658

Please sign in to comment.