-
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.
Browse files
Browse the repository at this point in the history
완독한 책에 평가를 할 수 있다.
- Loading branch information
Showing
11 changed files
with
260 additions
and
5 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
api/src/main/java/com/dnd/sbooky/api/docs/spec/RegisterEvaluationApiSpec.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 com.dnd.sbooky.api.docs.spec; | ||
|
||
import com.dnd.sbooky.api.evaluation.request.RegisterEvaluationRequest; | ||
import com.dnd.sbooky.api.support.response.ApiResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
@Tag(name = "[Evaluation API]", description = "평가에 관련된 API") | ||
public interface RegisterEvaluationApiSpec { | ||
|
||
@Operation(summary = "평가 등록", description = "완독한 책에 대해 평가를 등록한다.") | ||
ApiResponse<?> registerEvaluation( | ||
Long memberBookId, RegisterEvaluationRequest request, UserDetails userDetails); | ||
} |
40 changes: 40 additions & 0 deletions
40
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,40 @@ | ||
package com.dnd.sbooky.api.evaluation; | ||
|
||
import com.dnd.sbooky.api.docs.spec.RegisterEvaluationApiSpec; | ||
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.http.HttpStatus; | ||
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.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api") | ||
@RequiredArgsConstructor | ||
public class RegisterEvaluationController implements RegisterEvaluationApiSpec { | ||
|
||
private final RegisterEvaluationUseCase registerEvaluationUseCase; | ||
|
||
@PutMapping("/books/{memberBookId}/evaluation") | ||
@ResponseStatus(HttpStatus.CREATED) | ||
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()); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
package com.dnd.sbooky.api.evaluation; | ||
|
||
import com.dnd.sbooky.api.book.exception.BookForbiddenException; | ||
import com.dnd.sbooky.api.book.exception.BookNotFoundException; | ||
import com.dnd.sbooky.api.book.exception.BookReadStatusException; | ||
import com.dnd.sbooky.api.evaluation.exception.EvaluationNotFoundException; | ||
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.book.ReadStatus; | ||
import com.dnd.sbooky.core.evaluation.BookEvaluationEntity; | ||
import com.dnd.sbooky.core.evaluation.BookEvaluationRepository; | ||
import com.dnd.sbooky.core.evaluation.EvaluationEntity; | ||
import com.dnd.sbooky.core.evaluation.EvaluationRepository; | ||
import java.util.List; | ||
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 = validateAndGetMemberBook(memberBookId, memberId); | ||
|
||
bookEvaluationRepository.deleteAllByMemberBookId(memberBookId); | ||
|
||
List<EvaluationEntity> evaluations = getValidatedEvaluations(request.keywordIds()); | ||
|
||
List<BookEvaluationEntity> bookEvaluations = | ||
evaluations.stream() | ||
.map(evaluation -> BookEvaluationEntity.newInstance(memberBook, evaluation)) | ||
.toList(); | ||
|
||
bookEvaluationRepository.saveAll(bookEvaluations); | ||
} | ||
|
||
private MemberBookEntity validateAndGetMemberBook(Long memberBookId, Long memberId) { | ||
MemberBookEntity memberBook = | ||
memberBookRepository | ||
.findById(memberBookId) | ||
.orElseThrow(() -> new BookNotFoundException(ErrorType.BOOK_NOT_FOUND)); | ||
|
||
if (!memberBook.isSameMember(memberId)) { | ||
throw new BookForbiddenException(ErrorType.BOOK_ACCESS_FORBIDDEN); | ||
} | ||
|
||
if (memberBook.getReadStatus() != ReadStatus.COMPLETED) { | ||
throw new BookReadStatusException(ErrorType.BOOK_READ_STATUS_NOT_COMPLETED); | ||
} | ||
|
||
return memberBook; | ||
} | ||
|
||
private List<EvaluationEntity> getValidatedEvaluations(List<Long> keywordIds) { | ||
return keywordIds.stream() | ||
.map( | ||
id -> | ||
evaluationRepository | ||
.findById(id) | ||
.orElseThrow( | ||
() -> | ||
new EvaluationNotFoundException( | ||
ErrorType.EVALUATION_KEYWORD_NOT_FOUND))) | ||
.toList(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
api/src/main/java/com/dnd/sbooky/api/evaluation/exception/EvaluationNotFoundException.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,11 @@ | ||
package com.dnd.sbooky.api.evaluation.exception; | ||
|
||
import com.dnd.sbooky.api.support.error.ApiException; | ||
import com.dnd.sbooky.api.support.error.ErrorType; | ||
|
||
public class EvaluationNotFoundException extends ApiException { | ||
|
||
public EvaluationNotFoundException(ErrorType errorType) { | ||
super(errorType); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
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,11 @@ | ||
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) {} |
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
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
43 changes: 43 additions & 0 deletions
43
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,43 @@ | ||
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 EvaluationEntity evaluation; | ||
|
||
private BookEvaluationEntity(MemberBookEntity memberBookEntity, EvaluationEntity evaluation) { | ||
this.memberBook = memberBookEntity; | ||
this.evaluation = evaluation; | ||
} | ||
|
||
public static BookEvaluationEntity newInstance( | ||
MemberBookEntity memberBookEntity, EvaluationEntity evaluation) { | ||
return new BookEvaluationEntity(memberBookEntity, evaluation); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
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,13 @@ | ||
package com.dnd.sbooky.core.evaluation; | ||
|
||
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 BookEvaluationRepository extends JpaRepository<BookEvaluationEntity, Long> { | ||
|
||
@Modifying | ||
@Query("DELETE FROM BookEvaluationEntity be WHERE be.memberBook.id = :memberBookId") | ||
void deleteAllByMemberBookId(@Param("memberBookId") Long memberBookId); | ||
} |
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