Skip to content

Commit

Permalink
[refactor] refactor with UnauthorizedException
Browse files Browse the repository at this point in the history
  • Loading branch information
PicturePark1101 committed Jul 10, 2024
1 parent 90a1311 commit 376805d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.hankki.hankkiserver.api.favorite.service.FavoriteCommandService;
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.FavoritesDeleteCommand;
import org.hankki.hankkiserver.auth.UserId;
import org.hankki.hankkiserver.common.code.CommonSuccessCode;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -22,16 +23,16 @@ public class FavoriteController {
private final FavoriteCommandService favoriteCommandService;

@PostMapping("/favorites")
public HankkiResponse<Void> createFavorite(@UserId final Long userId, @RequestBody @Valid final FavoritePostRequest favoritePostRequest) {
public HankkiResponse<Void> createFavorite(@UserId final Long userId, @RequestBody @Valid final FavoritePostRequest request) {

favoriteCommandService.create(FavoritePostCommand.of(userId, favoritePostRequest));
favoriteCommandService.create(FavoritePostCommand.of(userId, request));
return HankkiResponse.success(CommonSuccessCode.CREATED);
}

@PostMapping("/favorites/delete")
public HankkiResponse<Void> deleteFavorite(@RequestBody final FavoriteDeleteRequest favoriteDeleteRequest) {
public HankkiResponse<Void> deleteFavorite(@UserId final Long userId, @RequestBody final FavoriteDeleteRequest request) {

favoriteCommandService.deleteFavorites(favoriteDeleteRequest);
favoriteCommandService.deleteFavorites(FavoritesDeleteCommand.of(userId, request));
return HankkiResponse.success(CommonSuccessCode.NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.api.auth.service.UserFinder;
import org.hankki.hankkiserver.api.favorite.controller.request.FavoriteDeleteRequest;
import org.hankki.hankkiserver.api.favorite.service.command.FavoritesDeleteCommand;
import org.hankki.hankkiserver.api.favoritestore.service.FavoriteStoreDeleter;
import org.hankki.hankkiserver.common.code.UserErrorCode;
import org.hankki.hankkiserver.common.exception.UnauthorizedException;
import org.hankki.hankkiserver.domain.favorite.model.Favorite;
import org.hankki.hankkiserver.domain.favorite.repository.FavoriteRepository;
import org.hankki.hankkiserver.api.favorite.service.command.FavoritePostCommand;
Expand All @@ -23,23 +25,28 @@ public class FavoriteCommandService {
private final FavoriteDeleter favoriteDeleter;

@Transactional
public void create(final FavoritePostCommand favoritePostCommand) {
public void create(final FavoritePostCommand command) {

User findUser = userFinder.getUser(favoritePostCommand.userId());
User findUser = userFinder.getUser(command.userId());

String title = favoritePostCommand.title();
String details = String.join(" ", favoritePostCommand.details());
String title = command.title();
String details = String.join(" ", command.details());

favoriteRepository.save(Favorite.create(findUser, title, details));

}

@Transactional
public void deleteFavorites(final FavoriteDeleteRequest favoriteDeleteRequest) {

List<Favorite> favorites = favoriteDeleteRequest.favoriteIds().stream()
.map(favoriteFinder::findById)
.toList();
public void deleteFavorites(final FavoritesDeleteCommand command) {

List<Favorite> favorites = command.favoriteIds().stream()
.map( favoriteId -> {
Favorite favorite = favoriteFinder.findById(favoriteId);
if (!favorite.getUser().getId().equals(command.userId())) {
throw new UnauthorizedException(UserErrorCode.USER_FORBIDDEN);
}
return favorite;
}).toList();

favoriteStoreDeleter.deleteAllByFavorites(favorites);
favoriteDeleter.deleteAll(favorites);
Expand Down
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;
import org.hankki.hankkiserver.api.favorite.controller.request.FavoriteDeleteRequest;

public record FavoritesDeleteCommand(
long userId,
List<Long> favoriteIds
) {

public static FavoritesDeleteCommand of(final long userId, final FavoriteDeleteRequest favoriteDeleteRequest) {
return new FavoritesDeleteCommand(userId, favoriteDeleteRequest.favoriteIds());
}
}

0 comments on commit 376805d

Please sign in to comment.