-
Notifications
You must be signed in to change notification settings - Fork 0
[#30] 관심 작품 등록/해제/목록보기 #33
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
Open
hado0123
wants to merge
1
commit into
feature/22
Choose a base branch
from
feature/30
base: feature/22
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
64 changes: 64 additions & 0 deletions
64
src/main/java/com/creatorhub/controller/CreationFavoriteController.java
This file contains hidden or 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,64 @@ | ||
| package com.creatorhub.controller; | ||
|
|
||
|
|
||
| import com.creatorhub.dto.CreationFavoriteRequest; | ||
| import com.creatorhub.dto.CreationFavoriteResponse; | ||
| import com.creatorhub.dto.FavoriteCreationItem; | ||
| import com.creatorhub.security.auth.CustomUserPrincipal; | ||
| import com.creatorhub.service.CreationFavoriteService; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/api/creations/favorites") | ||
| public class CreationFavoriteController { | ||
| private final CreationFavoriteService creationFavoriteService; | ||
|
|
||
| /** | ||
| * 관심 등록 | ||
| */ | ||
| @PostMapping("/create") | ||
| public ResponseEntity<CreationFavoriteResponse> favorite( | ||
| @AuthenticationPrincipal CustomUserPrincipal principal, | ||
| @Valid @RequestBody CreationFavoriteRequest req | ||
| ) { | ||
| CreationFavoriteResponse creationFavoriteResponse = | ||
| creationFavoriteService.favorite(principal.id(), req.creationId()); | ||
|
|
||
| return ResponseEntity.ok(creationFavoriteResponse); | ||
| } | ||
|
|
||
| /** | ||
| * 관심 해제 | ||
| */ | ||
| @DeleteMapping("/delete/{creationId}") | ||
| public ResponseEntity<CreationFavoriteResponse> unfavorite( | ||
| @AuthenticationPrincipal CustomUserPrincipal principal, | ||
| @PathVariable Long creationId | ||
| ) { | ||
| CreationFavoriteResponse creationFavoriteResponse = | ||
| creationFavoriteService.unfavorite(principal.id(), creationId); | ||
|
|
||
| return ResponseEntity.ok(creationFavoriteResponse); | ||
| } | ||
|
|
||
| /** | ||
| * 내 관심작품 목록 | ||
| */ | ||
| @GetMapping("/me") | ||
| public ResponseEntity<Page<FavoriteCreationItem>> myFavorites( | ||
| @AuthenticationPrincipal CustomUserPrincipal principal, | ||
| Pageable pageable | ||
| ) { | ||
| Page<FavoriteCreationItem> favoriteCreationItem = | ||
| creationFavoriteService.getMyFavorites(principal.id(), pageable); | ||
|
|
||
| return ResponseEntity.ok(favoriteCreationItem); | ||
| } | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
src/main/java/com/creatorhub/dto/CreationFavoriteRequest.java
This file contains hidden or 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,9 @@ | ||
| package com.creatorhub.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record CreationFavoriteRequest( | ||
| @NotNull(message = "creationId가 존재하지 않습니다.") | ||
| Long creationId | ||
| ) { | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/creatorhub/dto/CreationFavoriteResponse.java
This file contains hidden or 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,11 @@ | ||
| package com.creatorhub.dto; | ||
|
|
||
| public record CreationFavoriteResponse( | ||
| Long creationId, | ||
| Integer favoriteCount, | ||
| boolean favorited | ||
| ) { | ||
| public static CreationFavoriteResponse of(Long creationId, Integer favoriteCount, boolean favorited) { | ||
| return new CreationFavoriteResponse(creationId, favoriteCount, favorited); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/creatorhub/dto/FavoriteCreationItem.java
This file contains hidden or 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,13 @@ | ||
| package com.creatorhub.dto; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public record FavoriteCreationItem( | ||
| Long creationId, | ||
| String title, | ||
| String plot, | ||
| boolean isPublic, | ||
| Integer favoriteCount, | ||
| LocalDateTime favoritedAt | ||
| ) { | ||
| } |
This file contains hidden or 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 hidden or 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,57 @@ | ||
| package com.creatorhub.entity; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import org.hibernate.annotations.SQLDelete; | ||
| import org.hibernate.annotations.SQLRestriction; | ||
|
|
||
| @Entity | ||
| @Table( | ||
| name = "creation_favorite", | ||
| indexes = { | ||
| // 내 관심작품 목록 조회 (최신순) | ||
| @Index( | ||
| name = "idx_creation_favorite_member_created_at", | ||
| columnList = "member_id, created_at" | ||
| ), | ||
| // 작품별 관심자 조회 (알림 대상 추출) | ||
| @Index( | ||
| name = "idx_creation_favorite_creation", | ||
| columnList = "creation_id" | ||
| ) | ||
| } | ||
| ) | ||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @SQLDelete(sql = "UPDATE creation_favorite SET deleted_at = NOW() WHERE id = ?") | ||
| @SQLRestriction("deleted_at IS NULL") | ||
| public class CreationFavorite extends BaseTimeEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY, optional = false) | ||
| @JoinColumn(name = "member_id", nullable = false) | ||
| private Member member; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY, optional = false) | ||
| @JoinColumn(name = "creation_id", nullable = false) | ||
| private Creation creation; | ||
|
|
||
| @Builder(access = AccessLevel.PRIVATE) | ||
| private CreationFavorite(Member member, Creation creation) { | ||
| this.member = member; | ||
| this.creation = creation; | ||
| } | ||
|
|
||
| public static CreationFavorite create(Member member, Creation creation) { | ||
| return CreationFavorite.builder() | ||
| .member(member) | ||
| .creation(creation) | ||
| .build(); | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/creatorhub/exception/CreationException.java
This file contains hidden or 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,19 @@ | ||
| package com.creatorhub.exception; | ||
|
|
||
| import com.creatorhub.constant.ErrorCode; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class CreationException extends RuntimeException { | ||
| private final ErrorCode errorCode; | ||
|
|
||
| public CreationException(ErrorCode errorCode) { | ||
| super(errorCode.getMessage()); | ||
| this.errorCode = errorCode; | ||
| } | ||
|
|
||
| public CreationException(ErrorCode errorCode, String message) { | ||
| super(message); | ||
| this.errorCode = errorCode; | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/creatorhub/exception/CreationFavoriteNotFoundException.java
This file contains hidden or 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 com.creatorhub.exception; | ||
|
|
||
| import com.creatorhub.constant.ErrorCode; | ||
|
|
||
| public class CreationFavoriteNotFoundException extends CreatorException { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CreationException 을 상속하는게 맞을 것 같아요 🙂 |
||
|
|
||
| public CreationFavoriteNotFoundException() { | ||
| super(ErrorCode.CREATION_FAVORITE_NOT_FOUND); | ||
| } | ||
|
|
||
| public CreationFavoriteNotFoundException(String message) { | ||
| super(ErrorCode.CREATION_FAVORITE_NOT_FOUND, message); | ||
| } | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
src/main/java/com/creatorhub/exception/CreationNotFoundException.java
This file contains hidden or 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 com.creatorhub.exception; | ||
|
|
||
| import com.creatorhub.constant.ErrorCode; | ||
|
|
||
| public class CreationNotFoundException extends CreatorException { | ||
|
|
||
| public CreationNotFoundException() { | ||
| super(ErrorCode.CREATION_NOT_FOUND); | ||
| } | ||
|
|
||
| public CreationNotFoundException(String message) { | ||
| super(ErrorCode.CREATION_NOT_FOUND, message); | ||
| } | ||
| } |
This file contains hidden or 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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/creatorhub/repository/CreationFavoriteRepository.java
This file contains hidden or 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 com.creatorhub.repository; | ||
|
|
||
| import com.creatorhub.entity.CreationFavorite; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface CreationFavoriteRepository extends JpaRepository<CreationFavorite, Long> { | ||
|
|
||
| Optional<CreationFavorite> findByMemberIdAndCreationId(Long memberId, Long creationId); | ||
|
|
||
| boolean existsByMemberIdAndCreationId(Long memberId, Long creationId); | ||
|
|
||
| Page<CreationFavorite> findByMemberIdOrderByCreatedAtDesc(Long memberId, Pageable pageable); | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,6 +2,25 @@ | |
|
|
||
| import com.creatorhub.entity.Creation; | ||
| 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 CreationRepository extends JpaRepository<Creation, Long> { | ||
| public interface CreationRepository extends JpaRepository<Creation, Long> { | ||
|
|
||
| // favoriteCount값이 0 아래로 더 이상 감소되지 않도록 처리 | ||
| @Modifying(clearAutomatically = true, flushAutomatically = true) | ||
| @Query(""" | ||
| UPDATE Creation c | ||
| SET c.favoriteCount = | ||
| CASE | ||
| WHEN :delta < 0 AND COALESCE(c.favoriteCount, 0) <= 0 | ||
| THEN 0 | ||
| ELSE COALESCE(c.favoriteCount, 0) + :delta | ||
| END | ||
| WHERE c.id = :creationId | ||
| """) | ||
|
Comment on lines
+12
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분은 왜 이렇게 했는지 한 번 설명해주시면 좋을 것 같아요! |
||
| void updateFavoriteCountSafely(@Param("creationId") Long creationId, | ||
| @Param("delta") int delta); | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
url 명에 create 를 빼고, 대신 POST "/api/creations/favorites" 이면 생성으로 약속하는게 어떨까요?
(보통 관례)
아래 delete 도 마찬가지 입니다!