-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [feat] add create static method and remove unnecessary filed * [feat] create FavoriteCommandService (before add login logic) * [feat] create request, command dto * [fix] change FavoriteController file name * [refactor] change dto name * [refactor] change code style * [refactor] change code style * [refactor] change code style in commandservice * [refactor] refactor createFavorite return format * [feat] create findById in FavoriteAdpater * [refactor] change package structure * [fix] fix import error occurring after a rebase * [feat] add login * [refactor] reflecting review * [feat] implementation multi delete * [refactor] change name finder * [feat] add exception * [feat] add batch FavoriteStore * [feat] add FavoriteStoreDeleter * [feat] add Favorite @onetomany * [feat] feat delete FavoriteStore * [feat] create ErrorCode * [feat] add delete query * [feat] add Favorite Deleter, Finder and FavoriteStore Deleter * [refactor] refactor response, request dto * [feat] add delete and set response * [feat] add ErrorCode * [refactor] refactor with UnauthorizedException * [refactor] Change to in query * [feat] add Column * [feat] add image * [refactor] remove fetch join * [refactor] apply code review
- Loading branch information
1 parent
f67d575
commit 71d52d2
Showing
17 changed files
with
313 additions
and
3 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/org/hankki/hankkiserver/api/favorite/controller/FavoriteController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.hankki.hankkiserver.api.favorite.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.hankki.hankkiserver.api.dto.HankkiResponse; | ||
import org.hankki.hankkiserver.api.favorite.controller.request.FavoriteDeleteRequest; | ||
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; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1") | ||
public class FavoriteController { | ||
|
||
private final FavoriteCommandService favoriteCommandService; | ||
|
||
@PostMapping("/favorites") | ||
public HankkiResponse<Void> createFavorite(@UserId final Long userId, @RequestBody @Valid final FavoritePostRequest request) { | ||
|
||
favoriteCommandService.create(FavoritePostCommand.of(userId, request)); | ||
return HankkiResponse.success(CommonSuccessCode.CREATED); | ||
} | ||
|
||
@PostMapping("/favorites/batch-delete") | ||
public HankkiResponse<Void> deleteFavorite(@UserId final Long userId, @RequestBody final FavoriteDeleteRequest request) { | ||
|
||
favoriteCommandService.deleteFavorites(FavoritesDeleteCommand.of(userId, request)); | ||
return HankkiResponse.success(CommonSuccessCode.NO_CONTENT); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...n/java/org/hankki/hankkiserver/api/favorite/controller/request/FavoriteDeleteRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.hankki.hankkiserver.api.favorite.controller.request; | ||
|
||
import java.util.List; | ||
|
||
public record FavoriteDeleteRequest( | ||
List<Long> favoriteIds | ||
) { | ||
} |
14 changes: 14 additions & 0 deletions
14
...ain/java/org/hankki/hankkiserver/api/favorite/controller/request/FavoritePostRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.hankki.hankkiserver.api.favorite.controller.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Size; | ||
import java.util.List; | ||
|
||
public record FavoritePostRequest( | ||
@NotBlank(message = "제목이 비었습니다.") | ||
@Size(max = 18, message = "제목 길이가 18자를 초과했습니다.") | ||
String title, | ||
@Size(min = 1, max = 2, message = "해시태그 리스트 size가 1 이상 2 이하가 아닙니다.") | ||
List<@Size(min = 2, max= 10, message = "해시태그가 # 포함 10자를 초과했습니다.") String> details | ||
) { | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteCommandService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.hankki.hankkiserver.api.favorite.service; | ||
|
||
import jakarta.transaction.Transactional; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.hankki.hankkiserver.api.auth.service.UserFinder; | ||
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.api.favorite.service.command.FavoritePostCommand; | ||
import org.hankki.hankkiserver.domain.user.model.User; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class FavoriteCommandService { | ||
|
||
private final UserFinder userFinder; | ||
private final FavoriteFinder favoriteFinder; | ||
private final FavoriteUpdater favoriteUpdater; | ||
private final FavoriteStoreDeleter favoriteStoreDeleter; | ||
private final FavoriteDeleter favoriteDeleter; | ||
|
||
@Transactional | ||
public Long create(final FavoritePostCommand command) { | ||
|
||
User findUser = userFinder.getUser(command.userId()); | ||
String title = command.title(); | ||
String details = String.join(" ", command.details()); | ||
|
||
return favoriteUpdater.save(Favorite.create(findUser, title, details)); | ||
} | ||
|
||
@Transactional | ||
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); | ||
} | ||
}); | ||
|
||
favoriteStoreDeleter.deleteAllByFavorites(favorites); | ||
favoriteDeleter.deleteAll(favorites); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteDeleter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
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 FavoriteDeleter { | ||
|
||
private final FavoriteRepository favoriteRepository; | ||
|
||
protected void deleteAll(List<Favorite> favorites) { | ||
|
||
try { | ||
favoriteRepository.deleteAll(favorites); | ||
} catch (Exception e) { | ||
throw new InternalServerException(BusinessErrorCode.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.hankki.hankkiserver.api.favorite.service; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.hankki.hankkiserver.common.code.FavoriteErrorCode; | ||
import org.hankki.hankkiserver.common.exception.NotFoundException; | ||
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 FavoriteFinder { | ||
|
||
private final FavoriteRepository favoriteRepository; | ||
|
||
public Favorite findById(final Long id) { | ||
return favoriteRepository.findById(id).orElseThrow(() -> new NotFoundException(FavoriteErrorCode.FAVORITE_NOT_FOUND)); | ||
} | ||
|
||
public List<Favorite> findAllByIds(final List<Long> ids) { | ||
return favoriteRepository.findAllByIds(ids); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/hankki/hankkiserver/api/favorite/service/FavoriteUpdater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/org/hankki/hankkiserver/api/favorite/service/command/FavoritePostCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.hankki.hankkiserver.api.favorite.service.command; | ||
|
||
import java.util.List; | ||
import org.hankki.hankkiserver.api.favorite.controller.request.FavoritePostRequest; | ||
|
||
public record FavoritePostCommand( | ||
Long userId, | ||
String title, | ||
List<String> details | ||
) { | ||
|
||
public static FavoritePostCommand of(final Long userId, final FavoritePostRequest favoritePostRequest) { | ||
|
||
return new FavoritePostCommand(userId, favoritePostRequest.title(), favoritePostRequest.details()); | ||
|
||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ain/java/org/hankki/hankkiserver/api/favorite/service/command/FavoritesDeleteCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/org/hankki/hankkiserver/api/favoritestore/service/FavoriteStoreDeleter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.hankki.hankkiserver.api.favoritestore.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.favoritestore.repository.FavoriteStoreRepository; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class FavoriteStoreDeleter { | ||
|
||
private final FavoriteStoreRepository favoriteStoreRepository; | ||
|
||
public void deleteAllByFavorites(List<Favorite> favorites) { | ||
|
||
try { | ||
favoriteStoreRepository.deleteAllByFavorites(favorites); | ||
} catch (Exception e) { | ||
throw new InternalServerException(BusinessErrorCode.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/hankki/hankkiserver/common/code/FavoriteErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.hankki.hankkiserver.common.code; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum FavoriteErrorCode implements ErrorCode { | ||
|
||
FAVORITE_NOT_FOUND(HttpStatus.NOT_FOUND, "등록되지 않은 족보입니다."); | ||
|
||
private final HttpStatus httpStatus; | ||
private final String message; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/hankki/hankkiserver/common/code/FavoriteStoreErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.hankki.hankkiserver.common.code; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum FavoriteStoreErrorCode implements ErrorCode { | ||
|
||
FAVORITE_STORE_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 데이터입니다."); | ||
|
||
private final HttpStatus httpStatus; | ||
private final String message; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/org/hankki/hankkiserver/domain/favorite/repository/FavoriteRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
package org.hankki.hankkiserver.domain.favorite.repository; | ||
|
||
import java.util.List; | ||
import org.hankki.hankkiserver.domain.favorite.model.Favorite; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
public interface FavoriteRepository extends JpaRepository<Favorite, Long> { | ||
|
||
@Modifying | ||
@Query("delete from Favorite f where f in :favorites") | ||
void deleteAll(@Param("favorites") List<Favorite> favorites); | ||
|
||
@Query("select f from Favorite f where f.id in :favoriteId") | ||
List<Favorite> findAllByIds(@Param("favoriteId") List<Long> favoriteId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...java/org/hankki/hankkiserver/domain/favoritestore/repository/FavoriteStoreRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
package org.hankki.hankkiserver.domain.favoritestore.repository; | ||
|
||
import java.util.List; | ||
import org.hankki.hankkiserver.domain.favorite.model.Favorite; | ||
import org.hankki.hankkiserver.domain.favoritestore.model.FavoriteStore; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
public interface FavoriteStoreRepository extends JpaRepository<FavoriteStore, Long> { | ||
|
||
@Modifying | ||
@Query("delete from FavoriteStore fs where fs.favorite in :favorites") | ||
void deleteAllByFavorites(@Param("favorites")List<Favorite> favorites); | ||
} |