Skip to content

Commit

Permalink
[feat] add login in service and controller
Browse files Browse the repository at this point in the history
  • Loading branch information
PicturePark1101 committed Jul 12, 2024
1 parent aa16173 commit aefcdad
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
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.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.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -35,4 +41,24 @@ public HankkiResponse<Void> deleteFavorite(@UserId final Long userId, @RequestBo
favoriteCommandService.deleteFavorites(FavoritesDeleteCommand.of(userId, request));
return HankkiResponse.success(CommonSuccessCode.NO_CONTENT);
}

@PostMapping("/favorites/{favoriteId}/stores/{storeId}")
public HankkiResponse<Void> 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<Void> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.NotFoundException;
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;

Expand All @@ -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
Expand All @@ -39,12 +51,43 @@ public void deleteFavorites(final FavoritesDeleteCommand command) {
List<Favorite> favorites = favoriteFinder.findAllByIds(command.favoriteIds());

favorites.forEach(favorite -> {
if (!favorite.getUser().getId().equals(command.userId())) {
throw new UnauthorizedException(UserErrorCode.USER_FORBIDDEN);
}
validateUserAuthorization(userFinder.getUser(command.userId()), favorite.getUser());
});

favoriteStoreDeleter.deleteAllByFavorites(favorites);
favoriteDeleter.deleteAll(favorites);
}

@Transactional
public Long createFavoriteStore(final FavoriteStorePostCommand command) {

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

validateUserAuthorization(userFinder.getUser(command.userId()), favorite.getUser());

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.findById(command.favoriteId());

validateUserAuthorization(userFinder.getUser(command.userId()), favorite.getUser());

favoriteStoreDeleter.delete(favoriteStoreFinder.findByFavoriteIdAndStoreId(favorite.getId(), command.storeId())
.orElseThrow(() -> new NotFoundException(FavoriteStoreErrorCode.FAVORITE_STORE_NOT_FOUND)));

favorite.updateImageByFavoriteStoreCount(favoriteStoreFinder.countByFavorite(favorite));
}

private void validateUserAuthorization(User user, User commandUser) {
if (!user.equals(commandUser)) {
throw new UnauthorizedException(UserErrorCode.USER_FORBIDDEN);
}
}
}

0 comments on commit aefcdad

Please sign in to comment.