Skip to content

Commit

Permalink
[feat] 족보 공유하기 기능 구현 (#227)
Browse files Browse the repository at this point in the history
* [feat] add request, command dto

* [feat] imple repository logic

* [feat] imple service, refac origin functions

* [feat] imple controller

* [refac] Applying feedback

* [feat] Applying feedback
  • Loading branch information
PicturePark1101 authored Dec 12, 2024
1 parent ee814a3 commit c833c74
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.hankki.hankkiserver.api.dto.HankkiResponse;
import org.hankki.hankkiserver.api.favorite.controller.request.FavoriteDeleteRequest;
import org.hankki.hankkiserver.api.favorite.controller.request.FavoritePostRequest;
import org.hankki.hankkiserver.api.favorite.controller.request.FavoriteSharedPostRequest;
import org.hankki.hankkiserver.api.favorite.service.FavoriteCommandService;
import org.hankki.hankkiserver.api.favorite.service.FavoriteQueryService;
import org.hankki.hankkiserver.api.favorite.service.command.*;
Expand Down Expand Up @@ -65,4 +66,10 @@ public HankkiResponse<FavoriteGetResponse> getFavorite(@UserId final Long userId
public HankkiResponse<FavoritesWithStatusGetResponse> getFavoritesWithStatus(@UserId Long id, @RequestParam("candidate") final Long storeId) {
return HankkiResponse.success(CommonSuccessCode.OK, favoriteQueryService.findFavoritesWithStatus(FavoritesWithStatusGetCommand.of(id, storeId)));
}
}

@PostMapping("/favorites/{favoriteId}/shared")
public HankkiResponse<Void> createSharedFavorite(@UserId final Long userId, @PathVariable(name = "favoriteId") long favoriteId, @RequestBody @Valid final FavoriteSharedPostRequest request) {
favoriteCommandService.createSharedFavorite(FavoriteSharedPostCommand.of(userId, favoriteId, request.title(), request.details()));
return HankkiResponse.success(CommonSuccessCode.CREATED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.hankki.hankkiserver.api.favorite.controller.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.util.List;

public record FavoriteSharedPostRequest (
@NotBlank(message = "제목이 비었습니다.")
@Size(max = 18, message = "제목 길이가 18자를 초과했습니다.")
String title,
@Size(min = 1, max = 2, message = "해시태그 리스트 size가 1 이상 2 이하가 아닙니다.")
List<@Size(min = 2, max= 9, message = "해시태그가 # 포함 9자를 초과했습니다.") String> details
) { }
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.api.auth.service.UserFinder;
import org.hankki.hankkiserver.api.favorite.service.command.FavoriteSharedPostCommand;
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;
Expand Down Expand Up @@ -37,17 +38,12 @@ public class FavoriteCommandService {
private final FavoriteDeleter favoriteDeleter;

@Transactional
public Long createFavorite(final FavoritePostCommand command) {

User findUser = userFinder.getUser(command.userId());
String title = command.title();
checkTitleExists(title, findUser);
return favoriteUpdater.save(Favorite.create(findUser, title, String.join(" ", command.details())));
public void createFavorite(final FavoritePostCommand command) {
favoriteUpdater.save(makeFavoriteByTitleAndDetails(command.userId(), command.title(), command.details()));
}

@Transactional
public void deleteFavorites(final FavoritesDeleteCommand command) {

List<Favorite> favorites = favoriteFinder.findAllByIds(command.favoriteIds());

favorites.forEach(favorite -> {
Expand All @@ -60,7 +56,6 @@ public void deleteFavorites(final FavoritesDeleteCommand command) {

@Transactional
public Long createFavoriteStore(final FavoriteStorePostCommand command) {

Favorite favorite = favoriteFinder.findByIdWithUser(command.favoriteId());
Store store = storeFinder.findByIdWhereDeletedIsFalse(command.storeId());

Expand All @@ -76,13 +71,18 @@ public Long createFavoriteStore(final FavoriteStorePostCommand command) {

@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));
}

@Transactional
public void createSharedFavorite(final FavoriteSharedPostCommand command) {
Favorite favorite = makeFavoriteByTitleAndDetails(command.userId(), command.title(), command.details());
favoriteUpdater.save(favorite);
copySharedFavoriteStore(command.sharedFavoriteId(), favorite);
}

private void validateUserAuthorization(final User findUser, final User commandUser) {
if (!findUser.equals(commandUser)) {
throw new UnauthorizedException(UserErrorCode.USER_FORBIDDEN);
Expand All @@ -98,4 +98,18 @@ private void checkTitleExists(final String title, final User user){
throw new ConflictException(FavoriteErrorCode.FAVORITE_TITLE_EXISTS);
});
}
}

private void copySharedFavoriteStore(final long sharedId, final Favorite myFavorite) {
findStoresById(sharedId).forEach(it -> favoriteStoreUpdater.save(FavoriteStore.create(it, myFavorite)));
}

private List<Store> findStoresById(final long id) {
return favoriteStoreFinder.findByFavoriteId(id).stream().map(FavoriteStore::getStore).toList();
}

private Favorite makeFavoriteByTitleAndDetails(final long userId, final String title, List<String> details) {
User findUser = userFinder.getUser(userId);
checkTitleExists(title, findUser);
return Favorite.create(findUser, title, String.join(" ", details));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.hankki.hankkiserver.api.favorite.service.command;

import java.util.List;

public record FavoriteSharedPostCommand (
long userId,
long sharedFavoriteId,
String title,
List<String> details
) {
public static FavoriteSharedPostCommand of(final long userId, final long sharedFavoriteId, final String title, final List<String> details) {
return new FavoriteSharedPostCommand(userId, sharedFavoriteId, title, details);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public FavoriteStore findByFavoriteIdAndStoreId(final Long favoriteId, final Lon
public boolean isExist(final Long favoriteId, final Long storeId) {
return favoriteStoreRepository.isExist(favoriteId, storeId);
}
}

public List<FavoriteStore> findByFavoriteId(final long favoriteId) {
return favoriteStoreRepository.findByFavoriteId(favoriteId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public interface FavoriteStoreRepository extends JpaRepository<FavoriteStore, Lo

@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);
}

List<FavoriteStore> findByFavoriteId(Long favoriteId);
}

0 comments on commit c833c74

Please sign in to comment.