Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat-3] 포장마차 좋아요 목록 조회 API #16

Merged
merged 4 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,5 +4,8 @@ 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 = "좋아요순";
public static final String LOGOUT = "LOGOUT";
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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 @@ -15,7 +15,9 @@ 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 @@ -22,6 +23,7 @@

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Tag(name = "users", description = "유저 API")
@RestController
Expand Down Expand Up @@ -99,7 +101,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 @@ -5,6 +5,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 @@ -17,6 +20,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;


Expand All @@ -25,6 +30,8 @@
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 @@ -70,4 +77,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);
}
}
Loading