From b8a706feabe720eadcc01fc7568b992d77366d23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=84=9C=EC=A7=84?= <109809242+PicturePark1101@users.noreply.github.com> Date: Tue, 16 Jul 2024 03:43:23 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20=EC=A1=B1=EB=B3=B4=EC=97=90=20?= =?UTF-8?q?=EA=B0=80=EA=B2=8C=20=EC=B6=94=EA=B0=80=20=EC=82=AD=EC=A0=9C=20?= =?UTF-8?q?API=20=EA=B5=AC=ED=98=84=20(#65)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [feat] setting command dto * [feat] setting repository * [feat] add logic in Entity * [feat] add login in service and controller * [feat] add error code * [feat] imple repository logic * [feat] add check data logic * [refac] change Favorite imageUrl to imageType * [refac] change FavoriteImage variable name * [refac] fix wrong code --- .../controller/FavoriteController.java | 29 ++++++++++- .../service/FavoriteCommandService.java | 51 +++++++++++++++++-- .../api/favorite/service/FavoriteDeleter.java | 10 ++-- .../api/favorite/service/FavoriteFinder.java | 4 ++ .../service/command/FavoritePostCommand.java | 2 - .../command/FavoriteStoreDeleteCommand.java | 12 +++++ .../command/FavoriteStorePostCommand.java | 15 ++++++ .../response/FavoriteStoreFindResponse.java | 4 +- .../service/FavoriteStoreDeleter.java | 12 ++--- .../service/FavoriteStoreFinder.java | 31 +++++++++++ .../service/FavoriteStoreUpdater.java | 17 +++++++ .../api/store/service/StoreFinder.java | 3 +- .../response/UserFavoriteResponse.java | 5 +- .../common/code/FavoriteStoreErrorCode.java | 3 +- .../common/exception/NotFoundException.java | 2 +- .../domain/favorite/model/Favorite.java | 9 +++- .../domain/favorite/model/FavoriteImage.java | 25 +++++++++ .../repository/FavoriteRepository.java | 6 ++- .../favoritestore/model/FavoriteStore.java | 14 +++++ .../repository/FavoriteStoreRepository.java | 14 ++++- 20 files changed, 236 insertions(+), 32 deletions(-) create mode 100644 src/main/java/org/hankki/hankkiserver/api/favorite/service/command/FavoriteStoreDeleteCommand.java create mode 100644 src/main/java/org/hankki/hankkiserver/api/favorite/service/command/FavoriteStorePostCommand.java create mode 100644 src/main/java/org/hankki/hankkiserver/api/favoritestore/service/FavoriteStoreFinder.java create mode 100644 src/main/java/org/hankki/hankkiserver/api/favoritestore/service/FavoriteStoreUpdater.java create mode 100644 src/main/java/org/hankki/hankkiserver/domain/favorite/model/FavoriteImage.java diff --git a/src/main/java/org/hankki/hankkiserver/api/favorite/controller/FavoriteController.java b/src/main/java/org/hankki/hankkiserver/api/favorite/controller/FavoriteController.java index 7b58cee3..ed032ad9 100644 --- a/src/main/java/org/hankki/hankkiserver/api/favorite/controller/FavoriteController.java +++ b/src/main/java/org/hankki/hankkiserver/api/favorite/controller/FavoriteController.java @@ -8,9 +8,14 @@ import org.hankki.hankkiserver.api.favorite.service.command.FavoriteGetCommand; import org.hankki.hankkiserver.api.favorite.service.command.FavoritePostCommand; import org.hankki.hankkiserver.api.favorite.controller.request.FavoritePostRequest; +import org.hankki.hankkiserver.api.favorite.service.command.FavoriteStoreDeleteCommand; +import org.hankki.hankkiserver.api.favorite.service.command.FavoriteStorePostCommand; import org.hankki.hankkiserver.api.favorite.service.command.FavoritesDeleteCommand; import org.hankki.hankkiserver.auth.UserId; import org.hankki.hankkiserver.common.code.CommonSuccessCode; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.Mapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.hankki.hankkiserver.api.favorite.service.FavoriteQueryService; @@ -18,6 +23,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @@ -44,6 +50,27 @@ public HankkiResponse deleteFavorite(@UserId final Long userId, @RequestBo @GetMapping("/favorites/{favoriteId}") public HankkiResponse getFavorite(@UserId final Long userId, @PathVariable(name = "favoriteId") final Long favoriteId) { - return HankkiResponse.success(CommonSuccessCode.OK, favoriteQueryService.findFavorite(FavoriteGetCommand.of(userId, favoriteId))); + return HankkiResponse.success(CommonSuccessCode.OK, + favoriteQueryService.findFavorite(FavoriteGetCommand.of(userId, favoriteId))); + } + + @PostMapping("/favorites/{favoriteId}/stores/{storeId}") + public HankkiResponse createFavoriteStore( + @UserId final Long userId, + @PathVariable("favoriteId") final Long favoriteId, + @PathVariable("storeId") final Long storeId + ) { + favoriteCommandService.createFavoriteStore(FavoriteStorePostCommand.of(userId, favoriteId, storeId)); + return HankkiResponse.success(CommonSuccessCode.CREATED); + } + + @DeleteMapping("/favorites/{favoriteId}/stores/{storeId}") + public HankkiResponse deleteFavoriteStore( + @UserId final Long userId, + @PathVariable("favoriteId") final Long favoriteId, + @PathVariable("storeId") final Long storeId + ) { + favoriteCommandService.deleteFavoriteStore(FavoriteStoreDeleteCommand.of(userId, favoriteId, storeId)); + return HankkiResponse.success(CommonSuccessCode.NO_CONTENT); } } \ No newline at end of file diff --git a/src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteCommandService.java b/src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteCommandService.java index 5e827ee1..adce2e2a 100644 --- a/src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteCommandService.java +++ b/src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteCommandService.java @@ -4,12 +4,21 @@ import java.util.List; import lombok.RequiredArgsConstructor; import org.hankki.hankkiserver.api.auth.service.UserFinder; +import org.hankki.hankkiserver.api.favorite.service.command.FavoriteStoreDeleteCommand; +import org.hankki.hankkiserver.api.favorite.service.command.FavoriteStorePostCommand; import org.hankki.hankkiserver.api.favorite.service.command.FavoritesDeleteCommand; import org.hankki.hankkiserver.api.favoritestore.service.FavoriteStoreDeleter; +import org.hankki.hankkiserver.api.favoritestore.service.FavoriteStoreFinder; +import org.hankki.hankkiserver.api.favoritestore.service.FavoriteStoreUpdater; +import org.hankki.hankkiserver.api.store.service.StoreFinder; +import org.hankki.hankkiserver.common.code.FavoriteStoreErrorCode; import org.hankki.hankkiserver.common.code.UserErrorCode; +import org.hankki.hankkiserver.common.exception.ConflictException; import org.hankki.hankkiserver.common.exception.UnauthorizedException; import org.hankki.hankkiserver.domain.favorite.model.Favorite; import org.hankki.hankkiserver.api.favorite.service.command.FavoritePostCommand; +import org.hankki.hankkiserver.domain.favoritestore.model.FavoriteStore; +import org.hankki.hankkiserver.domain.store.model.Store; import org.hankki.hankkiserver.domain.user.model.User; import org.springframework.stereotype.Service; @@ -19,8 +28,11 @@ public class FavoriteCommandService { private final UserFinder userFinder; private final FavoriteFinder favoriteFinder; + private final StoreFinder storeFinder; private final FavoriteUpdater favoriteUpdater; + private final FavoriteStoreUpdater favoriteStoreUpdater; private final FavoriteStoreDeleter favoriteStoreDeleter; + private final FavoriteStoreFinder favoriteStoreFinder; private final FavoriteDeleter favoriteDeleter; @Transactional @@ -39,12 +51,45 @@ public void deleteFavorites(final FavoritesDeleteCommand command) { List favorites = favoriteFinder.findAllByIds(command.favoriteIds()); favorites.forEach(favorite -> { - if (!favorite.getUser().getId().equals(command.userId())) { - throw new UnauthorizedException(UserErrorCode.USER_FORBIDDEN); - } + validateUserAuthorization(favorite.getUser(), userFinder.getUser(command.userId())); }); favoriteStoreDeleter.deleteAllByFavorites(favorites); favoriteDeleter.deleteAll(favorites); } + + @Transactional + public Long createFavoriteStore(final FavoriteStorePostCommand command) { + + Favorite favorite = favoriteFinder.findByIdWithUser(command.favoriteId()); + Store store = storeFinder.findByIdWhereDeletedIsFalse(command.storeId()); + + if (isAlreadyExistsFavoriteStore(favorite.getId(), store.getId())){ + throw new ConflictException(FavoriteStoreErrorCode.FAVORITE_STORE_ALREADY_EXISTED); + }; + + FavoriteStore favoriteStore = favoriteStoreUpdater.save(FavoriteStore.create(store, favorite)); + favorite.updateImageByFavoriteStoreCount(favoriteStoreFinder.countByFavorite(favorite)); + + return favoriteStore.getId(); + } + + @Transactional + public void deleteFavoriteStore(final FavoriteStoreDeleteCommand command) { + + Favorite favorite = favoriteFinder.findByIdWithUser(command.favoriteId()); + favoriteStoreDeleter.delete(favoriteStoreFinder.findByFavoriteIdAndStoreId(favorite.getId(), command.storeId())); + + favorite.updateImageByFavoriteStoreCount(favoriteStoreFinder.countByFavorite(favorite)); + } + + private void validateUserAuthorization(final User findUser, final User commandUser) { + if (!findUser.equals(commandUser)) { + throw new UnauthorizedException(UserErrorCode.USER_FORBIDDEN); + } + } + + private boolean isAlreadyExistsFavoriteStore(final Long favoriteId, final Long storeId){ + return favoriteStoreFinder.isExist(favoriteId, storeId); + } } \ No newline at end of file diff --git a/src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteDeleter.java b/src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteDeleter.java index 4389ff88..fef0d62e 100644 --- a/src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteDeleter.java +++ b/src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteDeleter.java @@ -6,6 +6,7 @@ import org.hankki.hankkiserver.common.exception.InternalServerException; import org.hankki.hankkiserver.domain.favorite.model.Favorite; import org.hankki.hankkiserver.domain.favorite.repository.FavoriteRepository; +import org.hankki.hankkiserver.domain.favoritestore.model.FavoriteStore; import org.springframework.stereotype.Component; @Component @@ -14,12 +15,7 @@ public class FavoriteDeleter { private final FavoriteRepository favoriteRepository; - protected void deleteAll(List favorites) { - - try { - favoriteRepository.deleteAll(favorites); - } catch (Exception e) { - throw new InternalServerException(BusinessErrorCode.INTERNAL_SERVER_ERROR); - } + protected void deleteAll(final List favorites) { + favoriteRepository.deleteAll(favorites); } } 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 bba401b5..5296a578 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 @@ -25,4 +25,8 @@ public List findAllByIds(final List ids) { public List findAllByUserId(final Long userId) { return favoriteRepository.findByUserIdOrderByCreatedAtDesc(userId); } + + protected Favorite findByIdWithUser(final Long id) { + return favoriteRepository.findByIdWithUser(id).orElseThrow(() -> new NotFoundException(FavoriteErrorCode.FAVORITE_NOT_FOUND)); + } } diff --git a/src/main/java/org/hankki/hankkiserver/api/favorite/service/command/FavoritePostCommand.java b/src/main/java/org/hankki/hankkiserver/api/favorite/service/command/FavoritePostCommand.java index f85fbd37..05c31bcb 100644 --- a/src/main/java/org/hankki/hankkiserver/api/favorite/service/command/FavoritePostCommand.java +++ b/src/main/java/org/hankki/hankkiserver/api/favorite/service/command/FavoritePostCommand.java @@ -10,8 +10,6 @@ public record FavoritePostCommand( ) { public static FavoritePostCommand of(final Long userId, final FavoritePostRequest favoritePostRequest) { - return new FavoritePostCommand(userId, favoritePostRequest.title(), favoritePostRequest.details()); - } } diff --git a/src/main/java/org/hankki/hankkiserver/api/favorite/service/command/FavoriteStoreDeleteCommand.java b/src/main/java/org/hankki/hankkiserver/api/favorite/service/command/FavoriteStoreDeleteCommand.java new file mode 100644 index 00000000..096539c9 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/favorite/service/command/FavoriteStoreDeleteCommand.java @@ -0,0 +1,12 @@ +package org.hankki.hankkiserver.api.favorite.service.command; + +public record FavoriteStoreDeleteCommand( + Long userId, + Long favoriteId, + Long storeId +) { + + public static FavoriteStoreDeleteCommand of(Long userId, Long favoriteId, Long storeId) { + return new FavoriteStoreDeleteCommand(userId, favoriteId, storeId); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/api/favorite/service/command/FavoriteStorePostCommand.java b/src/main/java/org/hankki/hankkiserver/api/favorite/service/command/FavoriteStorePostCommand.java new file mode 100644 index 00000000..31133d99 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/favorite/service/command/FavoriteStorePostCommand.java @@ -0,0 +1,15 @@ +package org.hankki.hankkiserver.api.favorite.service.command; + +import org.hankki.hankkiserver.auth.UserId; +import org.springframework.web.bind.annotation.PathVariable; + +public record FavoriteStorePostCommand( + Long userId, + Long favoriteId, + Long storeId +) { + + public static FavoriteStorePostCommand of(Long userId, Long favoriteId, Long storeId) { + return new FavoriteStorePostCommand(userId, favoriteId, storeId); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/api/favorite/service/response/FavoriteStoreFindResponse.java b/src/main/java/org/hankki/hankkiserver/api/favorite/service/response/FavoriteStoreFindResponse.java index 1aa084cf..aad3a46d 100644 --- a/src/main/java/org/hankki/hankkiserver/api/favorite/service/response/FavoriteStoreFindResponse.java +++ b/src/main/java/org/hankki/hankkiserver/api/favorite/service/response/FavoriteStoreFindResponse.java @@ -1,19 +1,17 @@ package org.hankki.hankkiserver.api.favorite.service.response; import org.hankki.hankkiserver.domain.store.model.Store; -import org.hankki.hankkiserver.domain.store.model.StoreCategory; public record FavoriteStoreFindResponse( Long id, String name, - String imageUrl, + String imageType, String category, int lowestPrice, int heartCount ) { public static FavoriteStoreFindResponse of(Store store) { - return new FavoriteStoreFindResponse( store.getId(), store.getName(), diff --git a/src/main/java/org/hankki/hankkiserver/api/favoritestore/service/FavoriteStoreDeleter.java b/src/main/java/org/hankki/hankkiserver/api/favoritestore/service/FavoriteStoreDeleter.java index f9f09006..7ddd895a 100644 --- a/src/main/java/org/hankki/hankkiserver/api/favoritestore/service/FavoriteStoreDeleter.java +++ b/src/main/java/org/hankki/hankkiserver/api/favoritestore/service/FavoriteStoreDeleter.java @@ -5,6 +5,7 @@ import org.hankki.hankkiserver.common.code.BusinessErrorCode; import org.hankki.hankkiserver.common.exception.InternalServerException; import org.hankki.hankkiserver.domain.favorite.model.Favorite; +import org.hankki.hankkiserver.domain.favoritestore.model.FavoriteStore; import org.hankki.hankkiserver.domain.favoritestore.repository.FavoriteStoreRepository; import org.springframework.stereotype.Component; @@ -14,12 +15,11 @@ public class FavoriteStoreDeleter { private final FavoriteStoreRepository favoriteStoreRepository; - public void deleteAllByFavorites(List favorites) { + public void deleteAllByFavorites(final List favorites) { + favoriteStoreRepository.deleteAllByFavorites(favorites); + } - try { - favoriteStoreRepository.deleteAllByFavorites(favorites); - } catch (Exception e) { - throw new InternalServerException(BusinessErrorCode.INTERNAL_SERVER_ERROR); - } + public void delete(final FavoriteStore favoriteStore) { + favoriteStoreRepository.delete(favoriteStore); } } diff --git a/src/main/java/org/hankki/hankkiserver/api/favoritestore/service/FavoriteStoreFinder.java b/src/main/java/org/hankki/hankkiserver/api/favoritestore/service/FavoriteStoreFinder.java new file mode 100644 index 00000000..aeffc98a --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/favoritestore/service/FavoriteStoreFinder.java @@ -0,0 +1,31 @@ +package org.hankki.hankkiserver.api.favoritestore.service; + +import java.util.Optional; +import lombok.RequiredArgsConstructor; +import org.hankki.hankkiserver.common.code.FavoriteStoreErrorCode; +import org.hankki.hankkiserver.common.code.StoreErrorCode; +import org.hankki.hankkiserver.common.exception.NotFoundException; +import org.hankki.hankkiserver.domain.favorite.model.Favorite; +import org.hankki.hankkiserver.domain.favoritestore.model.FavoriteStore; +import org.hankki.hankkiserver.domain.favoritestore.repository.FavoriteStoreRepository; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class FavoriteStoreFinder { + + private final FavoriteStoreRepository favoriteStoreRepository; + + public int countByFavorite(final Favorite favorite) { + return favoriteStoreRepository.countByFavorite(favorite); + } + + public FavoriteStore findByFavoriteIdAndStoreId(final Long favoriteId, final Long storeId) { + return favoriteStoreRepository.findByFavoriteIdAndStoreId(favoriteId, storeId) + .orElseThrow(() -> new NotFoundException(FavoriteStoreErrorCode.FAVORITE_STORE_NOT_FOUND)); + } + + public boolean isExist(final Long favoriteId, final Long storeId) { + return favoriteStoreRepository.isExist(favoriteId, storeId); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/api/favoritestore/service/FavoriteStoreUpdater.java b/src/main/java/org/hankki/hankkiserver/api/favoritestore/service/FavoriteStoreUpdater.java new file mode 100644 index 00000000..c705e52c --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/favoritestore/service/FavoriteStoreUpdater.java @@ -0,0 +1,17 @@ +package org.hankki.hankkiserver.api.favoritestore.service; + +import lombok.RequiredArgsConstructor; +import org.hankki.hankkiserver.domain.favoritestore.model.FavoriteStore; +import org.hankki.hankkiserver.domain.favoritestore.repository.FavoriteStoreRepository; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class FavoriteStoreUpdater { + + private final FavoriteStoreRepository favoriteStoreRepository; + + public FavoriteStore save(FavoriteStore favoriteStore) { + return favoriteStoreRepository.save(favoriteStore); + } +} \ No newline at end of file diff --git a/src/main/java/org/hankki/hankkiserver/api/store/service/StoreFinder.java b/src/main/java/org/hankki/hankkiserver/api/store/service/StoreFinder.java index dcaedf4b..27710468 100644 --- a/src/main/java/org/hankki/hankkiserver/api/store/service/StoreFinder.java +++ b/src/main/java/org/hankki/hankkiserver/api/store/service/StoreFinder.java @@ -17,7 +17,8 @@ public Store getStoreReference(final Long id) { return storeRepository.getReferenceById(id); } - protected Store findByIdWhereDeletedIsFalse(final Long id) { + + public Store findByIdWhereDeletedIsFalse(final Long id) { return storeRepository.findByIdAndIsDeletedIsFalse(id) .orElseThrow(() -> new NotFoundException(StoreErrorCode.STORE_NOT_FOUND)); } 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 index 2748fa9c..cbe04afb 100644 --- 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 @@ -1,14 +1,15 @@ package org.hankki.hankkiserver.api.user.service.response; import org.hankki.hankkiserver.domain.favorite.model.Favorite; +import org.hankki.hankkiserver.domain.favorite.model.FavoriteImage; public record UserFavoriteResponse( Long id, String title, - String imageUrl + FavoriteImage imageUrl ) { public static UserFavoriteResponse of(Favorite favorite) { - return new UserFavoriteResponse(favorite.getId(), favorite.getName(), favorite.getImageUrl()); + return new UserFavoriteResponse(favorite.getId(), favorite.getName(), favorite.getImageType()); } } diff --git a/src/main/java/org/hankki/hankkiserver/common/code/FavoriteStoreErrorCode.java b/src/main/java/org/hankki/hankkiserver/common/code/FavoriteStoreErrorCode.java index 36f6caed..26b731e5 100644 --- a/src/main/java/org/hankki/hankkiserver/common/code/FavoriteStoreErrorCode.java +++ b/src/main/java/org/hankki/hankkiserver/common/code/FavoriteStoreErrorCode.java @@ -8,7 +8,8 @@ @RequiredArgsConstructor public enum FavoriteStoreErrorCode implements ErrorCode { - FAVORITE_STORE_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 데이터입니다."); + FAVORITE_STORE_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 데이터입니다."), + FAVORITE_STORE_ALREADY_EXISTED(HttpStatus.CONFLICT, "이미 족보에 추가된 가게입니다."); private final HttpStatus httpStatus; private final String message; diff --git a/src/main/java/org/hankki/hankkiserver/common/exception/NotFoundException.java b/src/main/java/org/hankki/hankkiserver/common/exception/NotFoundException.java index a6db85a7..d85c9ae6 100644 --- a/src/main/java/org/hankki/hankkiserver/common/exception/NotFoundException.java +++ b/src/main/java/org/hankki/hankkiserver/common/exception/NotFoundException.java @@ -12,4 +12,4 @@ public NotFoundException(ErrorCode errorCode) { super(errorCode.getMessage()); this.errorCode = errorCode; } -} +} \ 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 47c5a91c..b46227ed 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 @@ -31,13 +31,18 @@ public class Favorite extends BaseTimeEntity { private String detail; + @Enumerated(EnumType.STRING) @Column(nullable = false) - private String imageUrl; + private FavoriteImage imageType; @OneToMany(mappedBy = "favorite") @BatchSize(size = 100) private List favoriteStores = new ArrayList<>(); + public void updateImageByFavoriteStoreCount(int count) { + this.imageType = FavoriteImage.getType(count); + } + public static Favorite create(User user, String name, String detail) { return Favorite.builder() .user(user) @@ -51,6 +56,6 @@ private Favorite(User user, String name, String detail) { this.user = user; this.name = name; this.detail = detail; - this.imageUrl = "default.com"; + this.imageType = FavoriteImage.BASE_IMAGE; } } \ No newline at end of file diff --git a/src/main/java/org/hankki/hankkiserver/domain/favorite/model/FavoriteImage.java b/src/main/java/org/hankki/hankkiserver/domain/favorite/model/FavoriteImage.java new file mode 100644 index 00000000..a8e4d95e --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/domain/favorite/model/FavoriteImage.java @@ -0,0 +1,25 @@ +package org.hankki.hankkiserver.domain.favorite.model; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public enum FavoriteImage { + + BASE_IMAGE, + ONE_TO_FIVE, + SIX_TO_TEN, + ELEVEN_AND_ABOVE; + + public static FavoriteImage getType(final int countFavoriteStore) { + if (countFavoriteStore == 0) { + return BASE_IMAGE; + } else if (countFavoriteStore >= 1 && countFavoriteStore <= 5) { + return ONE_TO_FIVE; + } else if (countFavoriteStore >= 6 && countFavoriteStore <= 10) { + return SIX_TO_TEN; + } + return ELEVEN_AND_ABOVE; + } +} 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 9df367a4..f858562b 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 @@ -1,6 +1,7 @@ package org.hankki.hankkiserver.domain.favorite.repository; import java.util.List; +import java.util.Optional; import org.hankki.hankkiserver.domain.favorite.model.Favorite; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; @@ -16,4 +17,7 @@ public interface FavoriteRepository extends JpaRepository { List findByIdIn(@Param("favoriteId") List favoriteId); List findByUserIdOrderByCreatedAtDesc(Long userId); -} \ No newline at end of file + + @Query("select f from Favorite f join fetch f.user where f.id = :favoriteId") + Optional findByIdWithUser(@Param("favoriteId") Long favoriteId); +} diff --git a/src/main/java/org/hankki/hankkiserver/domain/favoritestore/model/FavoriteStore.java b/src/main/java/org/hankki/hankkiserver/domain/favoritestore/model/FavoriteStore.java index e87c5b9e..a44d9963 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/favoritestore/model/FavoriteStore.java +++ b/src/main/java/org/hankki/hankkiserver/domain/favoritestore/model/FavoriteStore.java @@ -2,6 +2,7 @@ import jakarta.persistence.*; import lombok.AccessLevel; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import org.hankki.hankkiserver.domain.favorite.model.Favorite; @@ -27,4 +28,17 @@ public class FavoriteStore { @JoinColumn(name = "favorite_id") private Favorite favorite; + public static FavoriteStore create(Store store, Favorite favorite) { + return FavoriteStore.builder() + .store(store) + .favorite(favorite) + .build(); + } + + @Builder + private FavoriteStore(Store store, Favorite favorite) { + this.store = store; + this.favorite = favorite; + } + } diff --git a/src/main/java/org/hankki/hankkiserver/domain/favoritestore/repository/FavoriteStoreRepository.java b/src/main/java/org/hankki/hankkiserver/domain/favoritestore/repository/FavoriteStoreRepository.java index 808d1ba5..239de7de 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/favoritestore/repository/FavoriteStoreRepository.java +++ b/src/main/java/org/hankki/hankkiserver/domain/favoritestore/repository/FavoriteStoreRepository.java @@ -1,6 +1,7 @@ package org.hankki.hankkiserver.domain.favoritestore.repository; import java.util.List; +import java.util.Optional; import org.hankki.hankkiserver.domain.favorite.model.Favorite; import org.hankki.hankkiserver.domain.favoritestore.model.FavoriteStore; import org.springframework.data.jpa.repository.JpaRepository; @@ -12,5 +13,14 @@ public interface FavoriteStoreRepository extends JpaRepository favorites); -} + void deleteAllByFavorites(@Param("favorites") List favorites); + + @Query("select count(fs) from FavoriteStore fs where fs.favorite = :favorite") + int countByFavorite(@Param("favorite") Favorite favorite); + + @Query("select fs from FavoriteStore fs where fs.favorite.id = :favoriteId and fs.store.id = :storeId") + Optional findByFavoriteIdAndStoreId(@Param("favoriteId") Long favoriteId, @Param("storeId") Long storeId); + + @Query("select exists (select f from FavoriteStore f where f.favorite.id = :favoriteId and f.store.id = :storeId)") + boolean isExist(@Param("favoriteId") Long favoriteId, @Param("storeId") Long storeId); +} \ No newline at end of file