Skip to content

Commit

Permalink
[feat] 족보 주인인지 여부 리턴하는 API 구현 (#229)
Browse files Browse the repository at this point in the history
* [feat] make response, command dto

* [feat] implementing repository func

* [feat] implementing controller func

* [feat] Add Exception Handling

* [refac] Remove Unnecessary Code
  • Loading branch information
PicturePark1101 authored Dec 17, 2024
1 parent c833c74 commit 38b8525
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.hankki.hankkiserver.api.favorite.service.FavoriteQueryService;
import org.hankki.hankkiserver.api.favorite.service.command.*;
import org.hankki.hankkiserver.api.favorite.service.response.FavoriteGetResponse;
import org.hankki.hankkiserver.api.favorite.service.response.FavoriteOwnershipGetResponse;
import org.hankki.hankkiserver.api.favorite.service.response.FavoritesWithStatusGetResponse;
import org.hankki.hankkiserver.auth.UserId;
import org.hankki.hankkiserver.common.code.CommonSuccessCode;
Expand Down Expand Up @@ -72,4 +73,9 @@ public HankkiResponse<Void> createSharedFavorite(@UserId final Long userId, @Pat
favoriteCommandService.createSharedFavorite(FavoriteSharedPostCommand.of(userId, favoriteId, request.title(), request.details()));
return HankkiResponse.success(CommonSuccessCode.CREATED);
}

@GetMapping("/favorites/{favoriteId}/ownership")
public HankkiResponse<FavoriteOwnershipGetResponse> checkFavoriteOwnership(@UserId Long userId, @PathVariable("favoriteId") final long favoriteId) {
return HankkiResponse.success(CommonSuccessCode.OK, favoriteQueryService.checkFavoriteOwnership(FavoriteOwnershipGetCommand.of(userId, favoriteId)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ protected Favorite findByIdWithFavoriteStore(final Long id) {
protected Optional<Favorite> findByNameAndUser(final String name, final User user) {
return favoriteRepository.findByNameAndUser(name, user);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.util.ArrayList;
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.api.favorite.service.command.FavoriteOwnershipGetCommand;
import org.hankki.hankkiserver.api.favorite.service.command.FavoritesGetCommand;
import org.hankki.hankkiserver.api.favorite.service.command.FavoritesWithStatusGetCommand;
import org.hankki.hankkiserver.api.favorite.service.response.FavoriteGetResponse;
import org.hankki.hankkiserver.api.favorite.service.response.FavoriteOwnershipGetResponse;
import org.hankki.hankkiserver.api.favorite.service.response.FavoritesWithStatusGetResponse;
import org.hankki.hankkiserver.api.store.service.StoreFinder;
import org.hankki.hankkiserver.domain.favorite.model.Favorite;
Expand Down Expand Up @@ -56,4 +58,16 @@ private List<Store> findStoresInFavorite(final Favorite favorite){
private boolean favoriteHasNoStore(final Favorite favorite) {
return favorite.getFavoriteStores().isEmpty();
}
}

public FavoriteOwnershipGetResponse checkFavoriteOwnership(final FavoriteOwnershipGetCommand command) {
return FavoriteOwnershipGetResponse.of(isOwner(getOwnerIdById(command.favoriteId()), command.userId()));
}

private long getOwnerIdById(final long id) {
return favoriteFinder.findById(id).getUser().getId();
}

private boolean isOwner(final long ownerId, final long userId) {
return ownerId == userId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.hankki.hankkiserver.api.favorite.service.command;

public record FavoriteOwnershipGetCommand(
long userId,
long favoriteId
) {
public static FavoriteOwnershipGetCommand of(final long userId, final long favoriteId) {
return new FavoriteOwnershipGetCommand(userId, favoriteId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.hankki.hankkiserver.api.favorite.service.response;

public record FavoriteOwnershipGetResponse(
boolean isOwner
) {
public static FavoriteOwnershipGetResponse of(final boolean isOwner) {
return new FavoriteOwnershipGetResponse(isOwner);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ public interface FavoriteRepository extends JpaRepository<Favorite, Long> {
Optional<Favorite> findByIdWithFavoriteStore(@Param("favoriteId") Long favoriteId);

Optional<Favorite> findByNameAndUser(String title, User user);
}
}

0 comments on commit 38b8525

Please sign in to comment.