Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 족보 주인인지 여부 리턴하는 API 구현 #229

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}