Skip to content

Commit

Permalink
merge : 인기 케이크 조회 - #42
Browse files Browse the repository at this point in the history
[FEAT] 인기 케이크 조회 - #42
  • Loading branch information
lreowy authored Jan 16, 2025
2 parents 37ca00f + 27c6e5c commit c62a524
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public ResponseEntity<BaseResponse<?>> getPopularCakesByStationStore(
) {
return ApiResponseUtil.success(SuccessCode.OK, cakeService.getPopularCakesByStationStore(userId, station, cakeLikesCursor,cakeIdCursor, size));
}

@GetMapping("/lank")
public ResponseEntity<BaseResponse<?>> getLankCakesByStationStore(@RequestHeader(required = false) final Long userId) {
return ApiResponseUtil.success(SuccessCode.OK, cakeService.getCakeByPopularity(userId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.cakey.cake.dto;

import java.util.List;

public record CakeListByPopularityRes(
List<CakeByPopularityDto> cakeList
) {
public CakeListByPopularityRes of(List<CakeByPopularityDto> cakeList) {
return new CakeListByPopularityRes(cakeList);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.cakey.cake.service;

import com.cakey.cake.dto.CakeByPopularityDto;
import com.cakey.cake.dto.CakeInfo;
import com.cakey.cake.dto.CakeInfoDto;
import com.cakey.cake.dto.CakeListByPopularityRes;
import com.cakey.cake.dto.CakesLatestByStationStoreRes;
import com.cakey.cake.dto.CakesPopularByStationStoreRes;
import com.cakey.cake.facade.CakeFacade;
Expand Down Expand Up @@ -90,4 +92,9 @@ public CakesPopularByStationStoreRes getPopularCakesByStationStore(final Long us

return CakesPopularByStationStoreRes.from(nextLikesCursor, nextCakeIdCursor, totalCakeCount, isLastData, cakes);
}

public CakeListByPopularityRes getCakeByPopularity(final Long userId) {
List<CakeByPopularityDto> cakeByPopularityDtos = cakeFacade.findCakeByPopularity(userId);
return new CakeListByPopularityRes(cakeByPopularityDtos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.cakey.cake.dto;

import com.cakey.store.domain.Station;

public record CakeByPopularityDto(
Long cakeId,
Long storeId,
String imageUrl,
String storeName,
Long cakeLike,
Station station,
boolean isLiked
) {
public static CakeByPopularityDto from(final Long cakeId,
final Long storeId,
final String imageUrl,
final String storeName,
final Long cakeLike,
final Station station,
final boolean isLiked
) {
return new CakeByPopularityDto(cakeId, storeId, imageUrl, storeName, cakeLike, station, isLiked);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cakey.cake.facade;

import com.cakey.cake.domain.Cake;
import com.cakey.cake.dto.CakeByPopularityDto;
import com.cakey.cake.dto.CakeInfoDto;
import com.cakey.cake.dto.CakeMainImageDto;
import com.cakey.store.domain.Station;
Expand Down Expand Up @@ -52,4 +53,8 @@ public List<CakeInfoDto> findLatestCakesByStation(final Long userId, final Stati
public List<Cake> findAllByStoreId(final Long storeId) {
return cakeRetriever.findAllByStoreId(storeId);
}

public List<CakeByPopularityDto> findCakeByPopularity(final Long userId) {
return cakeRetriever.findCakesByPopularity(userId);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cakey.cake.facade;

import com.cakey.cake.domain.Cake;
import com.cakey.cake.dto.CakeByPopularityDto;
import com.cakey.cake.dto.CakeInfoDto;
import com.cakey.cake.dto.CakeMainImageDto;
import com.cakey.cake.repository.CakeRepository;
Expand Down Expand Up @@ -39,4 +40,8 @@ public List<CakeInfoDto> findPopularCakesByStation(final Long userId,
return cakeRepository.findPopularCakesByStation(userId, station, likesCursor, cakeIdCursor, size);
}

public List<CakeByPopularityDto> findCakesByPopularity(final Long userId) {
return cakeRepository.findCakesByPopularity(userId);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cakey.cake.repository;

import com.cakey.cake.domain.Cake;
import com.cakey.cake.dto.CakeByPopularityDto;
import java.util.List;

import com.cakey.store.domain.Station;
Expand All @@ -16,4 +17,13 @@ public interface CakeRepository extends JpaRepository<Cake, Long>, CakeRepositor
//해당 지하철역 스토어의 케이크들 개수
@Query("SELECT COUNT(c) FROM Cake c JOIN Store s ON c.storeId = s.id WHERE s.station = :station")
int countCakesByStation(@Param("station") final Station station);

@Query("SELECT new com.cakey.cake.dto.CakeByPopularityDto(c.id, c.storeId, c.imageUrl, s.name, COUNT(cl.id), s.station, " +
"COALESCE(CASE WHEN EXISTS (SELECT 1 FROM CakeLikes cl2 WHERE cl2.cakeId = c.id AND cl2.userId = :userId) THEN true ELSE false END, false)) " +
"FROM Cake c " +
"LEFT JOIN CakeLikes cl ON c.id = cl.cakeId " +
"JOIN Store s ON c.storeId = s.id " +
"GROUP BY c.id, c.storeId, c.imageUrl, s.name, s.station " +
"ORDER BY COUNT(cl.id) DESC, c.id ASC LIMIT 10")
List<CakeByPopularityDto> findCakesByPopularity(@Param("userId") Long userId);
}

0 comments on commit c62a524

Please sign in to comment.