Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.moongeul.backend.api.post.controller;

import com.moongeul.backend.api.post.dto.PostCreateRequestDTO;
import com.moongeul.backend.api.post.dto.PostCreateResponseDTO;
import com.moongeul.backend.api.post.dto.PostRequestDTO;
import com.moongeul.backend.api.post.dto.PostIdResponseDTO;
import com.moongeul.backend.api.post.dto.PostResponseDTO;
import com.moongeul.backend.api.post.service.PostService;
import com.moongeul.backend.common.response.ApiResponse;
import com.moongeul.backend.common.response.SuccessStatus;
Expand Down Expand Up @@ -36,10 +37,59 @@ public class PostController {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "해당 도서를 찾을 수 없습니다.")
})
@PostMapping("/create")
public ResponseEntity<ApiResponse<PostCreateResponseDTO>> createPost(@AuthenticationPrincipal UserDetails userDetails,
@Valid @RequestBody PostCreateRequestDTO postCreateRequestDTO) {
public ResponseEntity<ApiResponse<PostIdResponseDTO>> createPost(@AuthenticationPrincipal UserDetails userDetails,
@Valid @RequestBody PostRequestDTO postRequestDTO) {

PostCreateResponseDTO response = postService.createPost(postCreateRequestDTO, userDetails.getUsername());
PostIdResponseDTO response = postService.createPost(postRequestDTO, userDetails.getUsername());
return ApiResponse.success(SuccessStatus.CREATE_POST_SUCCESS, response);
}

@Operation(
summary = "기록(게시글) 상세 조회 API",
description = "기록(게시글)의 상세 조회 API 입니다."
)
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "기록(게시글) 상세 조회 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "해당 기록(게시글)을 찾을 수 없습니다.")
})
@GetMapping("/{id}")
public ResponseEntity<ApiResponse<PostResponseDTO>> getPost(@PathVariable Long id) {

PostResponseDTO response = postService.getPostDetail(id);
return ApiResponse.success(SuccessStatus.GET_POST_SUCCESS, response);
}

@Operation(
summary = "기록(게시글) 수정 API",
description = "기록(게시글)을 수정하는 API 입니다."
)
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "기록(게시글) 수정 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "수정하려는 회원의 게시글이 아닙니다."),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "해당 기록(게시글)을 찾을 수 없습니다.")
})
@PutMapping("/{id}")
public ResponseEntity<ApiResponse<PostIdResponseDTO>> updatePost(@AuthenticationPrincipal UserDetails userDetails,
@PathVariable Long id,
@Valid @RequestBody PostRequestDTO postRequestDTO) {

PostIdResponseDTO response = postService.updatePost(id, userDetails.getUsername(), postRequestDTO);
return ApiResponse.success(SuccessStatus.UPDATE_POST_SUCCESS, response);
}

@Operation(
summary = "기록(게시글) 삭제 API",
description = "기록(게시글)을 삭제하는 API 입니다."
)
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "기록(게시글) 삭제 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "해당 기록(게시글)을 찾을 수 없습니다.")
})
@DeleteMapping("/{id}")
public ResponseEntity<ApiResponse<Void>> deletePost(@AuthenticationPrincipal UserDetails userDetails,
@PathVariable Long id) {

postService.deletePost(id, userDetails.getUsername());
return ApiResponse.success_only(SuccessStatus.DELETE_POST_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PostCreateResponseDTO {
public class PostIdResponseDTO {

private Long postId; // 생성된 Post id
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@
import com.moongeul.backend.api.category.entity.Category;
import com.moongeul.backend.api.post.entity.Post;
import com.moongeul.backend.api.post.entity.PostVisibility;
import com.moongeul.backend.api.post.entity.Quote;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PostCreateRequestDTO {
public class PostRequestDTO {

/* 드롭다운 - 필수 입력*/
@NotNull(message = "공개여부는 필수입니다")
Expand Down Expand Up @@ -51,8 +49,9 @@ public class PostCreateRequestDTO {
private List<QuoteRequestDTO> quotes; // 인상깊은구절

@Getter
@Builder
public static class QuoteRequestDTO {
private String quote; // 인용문 내용
private String quoteContent; // 인용문 내용
private Integer pageNumber; // 페이지 번호
}

Expand All @@ -62,7 +61,7 @@ public Post toEntity(Category category, Member member, Book book) {
Double finalRating = (this.rating != null) ? this.rating : 5.0;
Integer finalPage = (this.page != null) ? this.page : 300;

Post post = Post.builder()
return Post.builder()
.postVisibility(this.postVisibility)
.category(category)
.readDate(this.readDate)
Expand All @@ -72,22 +71,6 @@ public Post toEntity(Category category, Member member, Book book) {
.member(member)
.book(book)
.build();

// Quote 처리
if (this.quotes != null && !this.quotes.isEmpty()) {
List<Quote> quoteList = new ArrayList<>();
for (QuoteRequestDTO quoteDTO : this.quotes) {
Quote quote = Quote.builder()
.quoteContent(quoteDTO.getQuote())
.pageNumber(quoteDTO.getPageNumber())
.post(post)
.build();
quoteList.add(quote);

post.addQuotes(quoteList);
}
}
return post;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.moongeul.backend.api.post.dto;

import com.moongeul.backend.api.book.dto.BookDTO;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -14,12 +13,27 @@
@AllArgsConstructor
public class PostResponseDTO {

// 필수
private BookDTO bookDTO; // 필요 - 책 제목/작가/출판사
private double rating;

// 선택
private BookInfo bookInfo; // 책 정보
private double rating; // 별점
private String content; // 감상평
private List<String> quotes; // 인상깊은구절 리스트
private List<QuoteDTO> quotes; // 인상깊은구절 리스트

@Getter
@Builder
public static class BookInfo{

private String isbn; // ISBN
private String bookImage; // 표지 이미지
private String title; // 책 제목
private String author; // 저자
private String publisher; // 출판사
}

@Getter
@Builder
public static class QuoteDTO {
private String quoteContent; // 인용문 내용
private Integer pageNumber; // 페이지 번호
}

}
27 changes: 15 additions & 12 deletions src/main/java/com/moongeul/backend/api/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import lombok.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
Expand All @@ -27,10 +25,6 @@ public class Post extends BaseTimeEntity {
private Integer page; // 페이지 수
private String content; // 감상평

@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
@Builder.Default
private List<Quote> quotes = new ArrayList<>(); // 인상깊은구절

@Enumerated(EnumType.STRING)
private PostVisibility postVisibility; // 공개여부

Expand All @@ -46,11 +40,20 @@ public class Post extends BaseTimeEntity {
@JoinColumn(name = "book_isbn", nullable = false)
private Book book;

public void addQuote(Quote quote) {
quotes.add(quote);
}

public void addQuotes(List<Quote> quoteList) {
quoteList.forEach(this::addQuote);
// 게시글 수정
public void update(LocalDate readDate,
Double rating,
Integer page,
String content,
PostVisibility postVisibility,
Category category,
Book book) {
this.readDate = readDate;
this.rating = rating;
this.page = page;
this.content = content;
this.postVisibility = postVisibility;
this.category = category;
this.book = book;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.moongeul.backend.api.post.repository;

import com.moongeul.backend.api.post.entity.Quote;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface QuoteRepository extends JpaRepository<Quote, Long> {

List<Quote> findByPostId(Long postId);

void deleteAllByPostId(Long postId);
}
Loading