-
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.
- Loading branch information
Showing
6 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
api/src/main/java/com/dnd/sbooky/api/evaluation/RegisterEvaluationController.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,39 @@ | ||
package com.dnd.sbooky.api.evaluation; | ||
|
||
import com.dnd.sbooky.api.evaluation.request.RegisterEvaluationRequest; | ||
import com.dnd.sbooky.api.support.response.ApiResponse; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
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.RestController; | ||
|
||
|
||
@RestController | ||
@RequestMapping("/api") | ||
@RequiredArgsConstructor | ||
public class RegisterEvaluationController { | ||
|
||
private final RegisterEvaluationUseCase registerEvaluationUseCase; | ||
|
||
@PostMapping("/books/{memberBookId}/evaluation") | ||
public ApiResponse<?> registerEvaluation( | ||
@PathVariable Long memberBookId, | ||
@Valid @RequestBody RegisterEvaluationRequest request, | ||
@Parameter(hidden = true) @AuthenticationPrincipal UserDetails user) { | ||
|
||
registerEvaluationUseCase.register(memberBookId, extractMemberId(user), request); | ||
return ApiResponse.success(); | ||
} | ||
|
||
|
||
private Long extractMemberId(UserDetails user) { | ||
return Long.valueOf(user.getUsername()); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
api/src/main/java/com/dnd/sbooky/api/evaluation/RegisterEvaluationUseCase.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 com.dnd.sbooky.api.evaluation; | ||
|
||
import com.dnd.sbooky.api.book.exception.BookNotFoundException; | ||
import com.dnd.sbooky.api.evaluation.request.RegisterEvaluationRequest; | ||
import com.dnd.sbooky.api.support.error.ErrorType; | ||
import com.dnd.sbooky.core.book.MemberBookEntity; | ||
import com.dnd.sbooky.core.book.MemberBookRepository; | ||
import com.dnd.sbooky.core.evaluation.BookEvaluationEntity; | ||
import com.dnd.sbooky.core.evaluation.BookEvaluationRepository; | ||
import com.dnd.sbooky.core.evaluation.EvaluationRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class RegisterEvaluationUseCase { | ||
|
||
private final MemberBookRepository memberBookRepository; | ||
private final BookEvaluationRepository bookEvaluationRepository; | ||
private final EvaluationRepository evaluationRepository; | ||
|
||
public void register(Long memberBookId, Long memberId, RegisterEvaluationRequest request) { | ||
|
||
MemberBookEntity memberBook = | ||
memberBookRepository | ||
.findById(memberBookId) | ||
.orElseThrow(() -> new BookNotFoundException(ErrorType.BOOK_NOT_FOUND)); | ||
|
||
request.keywordIds().forEach(keywordId -> { | ||
EvaluationevaluationRepository.findById(keywordId) | ||
.orElseThrow(() -> new EvaluationKeywordNotFoundException(ErrorType.EVALUATION_KEYWORD_NOT_FOUND)); | ||
bookEvaluationRepository.save(BookEvaluationEntity.newInstance(memberBook, )) | ||
}); | ||
|
||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
api/src/main/java/com/dnd/sbooky/api/evaluation/request/RegisterEvaluationRequest.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,16 @@ | ||
package com.dnd.sbooky.api.evaluation.request; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Size; | ||
import java.util.List; | ||
|
||
@Schema(description = "평가 등록 요청 DTO") | ||
public record RegisterEvaluationRequest( | ||
@Schema(description = "평가 키워드 아이디 리스트") | ||
@NotNull | ||
@Size(min = 1, max = 6, message = "평가 키워드는 1개 이상 6개 이하로 등록해주세요.") | ||
List<Long> keywordIds | ||
) { | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
core/src/main/java/com/dnd/sbooky/core/evaluation/BookEvaluationEntity.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,44 @@ | ||
package com.dnd.sbooky.core.evaluation; | ||
|
||
import com.dnd.sbooky.core.book.MemberBookEntity; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
|
||
@Getter | ||
@Entity | ||
@Table(name = "book_evaluation") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class BookEvaluationEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_book_id") | ||
private MemberBookEntity memberBook; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "evaluation_id") | ||
private Evaluation evaluation; | ||
|
||
private BookEvaluationEntity(MemberBookEntity memberBookEntity, Evaluation evaluation) { | ||
this.memberBook = memberBookEntity; | ||
this.evaluation = evaluation; | ||
} | ||
|
||
public static BookEvaluationEntity newInstance(MemberBookEntity memberBookEntity, Evaluation evaluation) { | ||
return new BookEvaluationEntity(memberBookEntity, evaluation); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
core/src/main/java/com/dnd/sbooky/core/evaluation/BookEvaluationRepository.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 com.dnd.sbooky.core.evaluation; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
|
||
public interface BookEvaluationRepository extends JpaRepository<BookEvaluationEntity, Long> { | ||
|
||
} |
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