diff --git a/src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteFinder.java b/src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteFinder.java index c0d3d93e..bba401b5 100644 --- a/src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteFinder.java +++ b/src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteFinder.java @@ -19,6 +19,10 @@ public Favorite findById(final Long id) { } public List findAllByIds(final List ids) { - return favoriteRepository.findAllByIds(ids); + return favoriteRepository.findByIdIn(ids); + } + + public List findAllByUserId(final Long userId) { + return favoriteRepository.findByUserIdOrderByCreatedAtDesc(userId); } } diff --git a/src/main/java/org/hankki/hankkiserver/api/user/controller/UserController.java b/src/main/java/org/hankki/hankkiserver/api/user/controller/UserController.java index a5b41c54..c719af8e 100644 --- a/src/main/java/org/hankki/hankkiserver/api/user/controller/UserController.java +++ b/src/main/java/org/hankki/hankkiserver/api/user/controller/UserController.java @@ -7,6 +7,7 @@ import org.hankki.hankkiserver.api.user.service.UserCommandService; import org.hankki.hankkiserver.api.user.service.UserQueryService; import org.hankki.hankkiserver.api.user.service.command.UserUniversityPostCommand; +import org.hankki.hankkiserver.api.user.service.response.UserFavoritesGetResponse; import org.hankki.hankkiserver.api.user.service.response.UserProfileAndNicknameResponse; import org.hankki.hankkiserver.api.user.service.response.UserUniversityFindResponse; import org.hankki.hankkiserver.auth.UserId; @@ -35,6 +36,11 @@ public HankkiResponse findUserUniversity(@UserId fin @GetMapping("/users/me") public HankkiResponse getUserProfileAndNickname(@UserId final Long userId) { - return HankkiResponse.success(CommonSuccessCode.OK, userQueryService.getUserProfileAndNickname(userId)); + return HankkiResponse.success(CommonSuccessCode.OK, userQueryService.getUserProfileAndNickname(userId)); + } + + @GetMapping("/users/me/favorites") + public HankkiResponse findUserFavorites(@UserId final Long userId) { + return HankkiResponse.success(CommonSuccessCode.OK,userQueryService.findUserFavorites(userId)); } } diff --git a/src/main/java/org/hankki/hankkiserver/api/user/service/UserQueryService.java b/src/main/java/org/hankki/hankkiserver/api/user/service/UserQueryService.java index 0e0f956b..e23807a1 100644 --- a/src/main/java/org/hankki/hankkiserver/api/user/service/UserQueryService.java +++ b/src/main/java/org/hankki/hankkiserver/api/user/service/UserQueryService.java @@ -2,6 +2,8 @@ import lombok.RequiredArgsConstructor; import org.hankki.hankkiserver.api.auth.service.UserInfoFinder; +import org.hankki.hankkiserver.api.favorite.service.FavoriteFinder; +import org.hankki.hankkiserver.api.user.service.response.UserFavoritesGetResponse; import org.hankki.hankkiserver.api.user.service.response.UserProfileAndNicknameResponse; import org.hankki.hankkiserver.api.user.service.response.UserUniversityFindResponse; import org.hankki.hankkiserver.common.code.UserUniversityErrorCode; @@ -14,6 +16,7 @@ public class UserQueryService { private final UserUniversityFinder userUniversityFinder; + private final FavoriteFinder favoriteFinder; private final UserInfoFinder userInfoFinder; @Transactional(readOnly = true) @@ -22,8 +25,13 @@ public UserUniversityFindResponse findUserUniversity(Long userId) { .orElseThrow(() -> new NotFoundException(UserUniversityErrorCode.USER_UNIVERSITY_NOT_FOUND))); } + @Transactional(readOnly = true) + public UserFavoritesGetResponse findUserFavorites(final Long userId) { + return UserFavoritesGetResponse.of(favoriteFinder.findAllByUserId(userId)); + } + @Transactional(readOnly = true) public UserProfileAndNicknameResponse getUserProfileAndNickname(final Long userId) { - return UserProfileAndNicknameResponse.of(userInfoFinder.getUserInfo(userId)); + return UserProfileAndNicknameResponse.of(userInfoFinder.getUserInfo(userId)); } } diff --git a/src/main/java/org/hankki/hankkiserver/api/user/service/response/UserFavoriteResponse.java b/src/main/java/org/hankki/hankkiserver/api/user/service/response/UserFavoriteResponse.java new file mode 100644 index 00000000..2748fa9c --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/user/service/response/UserFavoriteResponse.java @@ -0,0 +1,14 @@ +package org.hankki.hankkiserver.api.user.service.response; + +import org.hankki.hankkiserver.domain.favorite.model.Favorite; + +public record UserFavoriteResponse( + Long id, + String title, + String imageUrl +) { + + public static UserFavoriteResponse of(Favorite favorite) { + return new UserFavoriteResponse(favorite.getId(), favorite.getName(), favorite.getImageUrl()); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/api/user/service/response/UserFavoritesGetResponse.java b/src/main/java/org/hankki/hankkiserver/api/user/service/response/UserFavoritesGetResponse.java new file mode 100644 index 00000000..7dfa596e --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/user/service/response/UserFavoritesGetResponse.java @@ -0,0 +1,13 @@ +package org.hankki.hankkiserver.api.user.service.response; + +import java.util.List; +import org.hankki.hankkiserver.domain.favorite.model.Favorite; + +public record UserFavoritesGetResponse( + List favorites +) { + + public static UserFavoritesGetResponse of(List favorites) { + return new UserFavoritesGetResponse(favorites.stream().map(UserFavoriteResponse::of).toList()); + } +} \ No newline at end of file diff --git a/src/main/java/org/hankki/hankkiserver/domain/favorite/model/Favorite.java b/src/main/java/org/hankki/hankkiserver/domain/favorite/model/Favorite.java index 7ad6bd39..47c5a91c 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/favorite/model/Favorite.java +++ b/src/main/java/org/hankki/hankkiserver/domain/favorite/model/Favorite.java @@ -32,7 +32,7 @@ public class Favorite extends BaseTimeEntity { private String detail; @Column(nullable = false) - private String image_url; + private String imageUrl; @OneToMany(mappedBy = "favorite") @BatchSize(size = 100) @@ -51,6 +51,6 @@ private Favorite(User user, String name, String detail) { this.user = user; this.name = name; this.detail = detail; - this.image_url = "default.com"; + this.imageUrl = "default.com"; } -} +} \ No newline at end of file diff --git a/src/main/java/org/hankki/hankkiserver/domain/favorite/repository/FavoriteRepository.java b/src/main/java/org/hankki/hankkiserver/domain/favorite/repository/FavoriteRepository.java index ed703df3..9df367a4 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/favorite/repository/FavoriteRepository.java +++ b/src/main/java/org/hankki/hankkiserver/domain/favorite/repository/FavoriteRepository.java @@ -13,6 +13,7 @@ public interface FavoriteRepository extends JpaRepository { @Query("delete from Favorite f where f in :favorites") void deleteAll(@Param("favorites") List favorites); - @Query("select f from Favorite f where f.id in :favoriteId") - List findAllByIds(@Param("favoriteId") List favoriteId); -} + List findByIdIn(@Param("favoriteId") List favoriteId); + + List findByUserIdOrderByCreatedAtDesc(Long userId); +} \ No newline at end of file diff --git a/src/main/java/org/hankki/hankkiserver/domain/store/model/Store.java b/src/main/java/org/hankki/hankkiserver/domain/store/model/Store.java index 61f7ffc8..69fff806 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/store/model/Store.java +++ b/src/main/java/org/hankki/hankkiserver/domain/store/model/Store.java @@ -65,4 +65,5 @@ public void decreaseHeartCount() { public void increaseHeartCount() { this.heartCount++; } + }