Skip to content

Commit

Permalink
[refactor] apply code review
Browse files Browse the repository at this point in the history
  • Loading branch information
PicturePark1101 committed Jul 11, 2024
1 parent 4200074 commit 1e87ceb
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public HankkiResponse<Void> createFavorite(@UserId final Long userId, @RequestBo
return HankkiResponse.success(CommonSuccessCode.CREATED);
}

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

favoriteCommandService.deleteFavorites(FavoritesDeleteCommand.of(userId, request));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

public record FavoritePostRequest(
@NotBlank(message = "제목이 비었습니다.")
@Size(max = 20, message = "제목 길이가 20 이상입니다.")
@Size(max = 18, message = "제목 길이가 18자를 초과했습니다.")
String title,
List<String> details
@Size(min = 1, max = 2, message = "해시태그 리스트 size가 1 이상 2 이하가 아닙니다.")
List<@Size(min = 2, max= 10, message = "해시태그가 # 포함 10자를 초과했습니다.") String> details
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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;
import org.hankki.hankkiserver.domain.user.model.User;
import org.springframework.stereotype.Service;
Expand All @@ -18,30 +17,29 @@
@RequiredArgsConstructor
public class FavoriteCommandService {

private final FavoriteRepository favoriteRepository;
private final UserFinder userFinder;
private final FavoriteFinder favoriteFinder;
private final FavoriteUpdater favoriteUpdater;
private final FavoriteStoreDeleter favoriteStoreDeleter;
private final FavoriteDeleter favoriteDeleter;

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

User findUser = userFinder.getUser(command.userId());
String title = command.title();
String details = String.join(" ", command.details());

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

@Transactional
public void deleteFavorites(final FavoritesDeleteCommand command) {

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

Long userId = command.userId();
favorites.forEach(favorite -> {
if (!favorite.getUser().getId().equals(userId)) {
if (!favorite.getUser().getId().equals(command.userId())) {
throw new UnauthorizedException(UserErrorCode.USER_FORBIDDEN);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
public class FavoriteDeleter {

private final FavoriteRepository favoriteRepository;
public void deleteAll(List<Favorite> favorites) {

protected void deleteAll(List<Favorite> favorites) {

try {
favoriteRepository.deleteAll(favorites);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Favorite findById(final Long id) {
return favoriteRepository.findById(id).orElseThrow(() -> new NotFoundException(FavoriteErrorCode.FAVORITE_NOT_FOUND));
}

public List<Favorite> findByIds(final List<Long> ids) {
return favoriteRepository.findByIds(ids);
public List<Favorite> findAllByIds(final List<Long> ids) {
return favoriteRepository.findAllByIds(ids);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.hankki.hankkiserver.api.favorite.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.common.code.BusinessErrorCode;
import org.hankki.hankkiserver.common.exception.InternalServerException;
import org.hankki.hankkiserver.domain.favorite.model.Favorite;
import org.hankki.hankkiserver.domain.favorite.repository.FavoriteRepository;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class FavoriteUpdater {

private final FavoriteRepository favoriteRepository;

protected Long save(Favorite favorite) {
return favoriteRepository.save(favorite).getId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public record FavoritePostCommand(
List<String> details
) {

public static FavoritePostCommand of(final Long memberId, final FavoritePostRequest favoritePostRequest) {
public static FavoritePostCommand of(final Long userId, final FavoritePostRequest favoritePostRequest) {

return new FavoritePostCommand(memberId, favoritePostRequest.title(), favoritePostRequest.details());
return new FavoritePostCommand(userId, favoritePostRequest.title(), favoritePostRequest.details());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

public interface FavoriteRepository extends JpaRepository<Favorite, Long> {

Expand All @@ -15,5 +14,5 @@ public interface FavoriteRepository extends JpaRepository<Favorite, Long> {
void deleteAll(@Param("favorites") List<Favorite> favorites);

@Query("select f from Favorite f where f.id in :favoriteId")
List<Favorite> findByIds(@Param("favoriteId") List<Long> favoriteId);
List<Favorite> findAllByIds(@Param("favoriteId") List<Long> favoriteId);
}

0 comments on commit 1e87ceb

Please sign in to comment.