From 86820cdbdf36eea8e7517c63308d66454b52b35f Mon Sep 17 00:00:00 2001 From: Jihwan Jung Date: Thu, 25 Apr 2024 15:43:57 +0900 Subject: [PATCH] =?UTF-8?q?#25=20feat:=20content=EB=8B=A8=EA=B1=B4=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C,=20=EC=A2=85=EB=A5=98=EB=B3=84=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../content/controller/ContentController.java | 18 ++++++++++ .../controller/dto/ContentResponse.java | 22 ++++++++++++ .../capstone/content/domain/Content.java | 4 +++ .../content/domain/ContentRepository.java | 6 +++- .../content/exception/ContentException.java | 10 ++++++ .../exception/ContentExceptionType.java | 34 +++++++++++++++++++ .../content/service/ContentService.java | 29 ++++++++++++++++ 7 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 backend/src/main/java/com/project/capstone/content/controller/dto/ContentResponse.java create mode 100644 backend/src/main/java/com/project/capstone/content/exception/ContentException.java create mode 100644 backend/src/main/java/com/project/capstone/content/exception/ContentExceptionType.java diff --git a/backend/src/main/java/com/project/capstone/content/controller/ContentController.java b/backend/src/main/java/com/project/capstone/content/controller/ContentController.java index 615f658e6a..92b6c3b014 100644 --- a/backend/src/main/java/com/project/capstone/content/controller/ContentController.java +++ b/backend/src/main/java/com/project/capstone/content/controller/ContentController.java @@ -2,12 +2,15 @@ import com.project.capstone.auth.domain.PrincipalDetails; import com.project.capstone.content.controller.dto.ContentCreateRequest; +import com.project.capstone.content.controller.dto.ContentResponse; import com.project.capstone.content.service.ContentService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController @RequiredArgsConstructor @RequestMapping("/content") @@ -15,6 +18,7 @@ public class ContentController { private final ContentService contentService; + // 컨텐츠 생성 @PostMapping("/create") public ResponseEntity createContent(@AuthenticationPrincipal PrincipalDetails details, @RequestBody ContentCreateRequest request, @@ -22,4 +26,18 @@ public ResponseEntity createContent(@AuthenticationPrincipal PrincipalDetails contentService.createContent(details.getUserId(), request, bookId, clubId); return ResponseEntity.ok().body("컨텐츠 생성 완료"); } + + // 단일 컨텐츠 조회 + @GetMapping("/{id}") + public ResponseEntity getContent(@PathVariable Long id) { + ContentResponse contentResponse = contentService.getContent(id); + return ResponseEntity.ok().body(contentResponse); + } + + // 컨텐츠 종류별 조회 + @GetMapping("/get") + public ResponseEntity> getContents(@RequestParam String type) { + List contentResponseList = contentService.getContents(type); + return ResponseEntity.ok().body(contentResponseList); + } } diff --git a/backend/src/main/java/com/project/capstone/content/controller/dto/ContentResponse.java b/backend/src/main/java/com/project/capstone/content/controller/dto/ContentResponse.java new file mode 100644 index 0000000000..13352f609e --- /dev/null +++ b/backend/src/main/java/com/project/capstone/content/controller/dto/ContentResponse.java @@ -0,0 +1,22 @@ +package com.project.capstone.content.controller.dto; + +import com.project.capstone.content.domain.Content; +import com.project.capstone.content.domain.ContentType; + +import java.util.UUID; + +public record ContentResponse( + Long id, + UUID memberId, + Long bookId, + Long clubId, + ContentType type, + String title, + String body, + int likes +) { + public ContentResponse(Content content) { + this(content.getId(), content.getMember().getId(), content.getBook().getId(), + content.getClub() == null ? null : content.getClub().getId(), content.getType(), content.getTitle(), content.getBody(), content.getLikes()); + } +} diff --git a/backend/src/main/java/com/project/capstone/content/domain/Content.java b/backend/src/main/java/com/project/capstone/content/domain/Content.java index 80ce953717..b042f06fe1 100644 --- a/backend/src/main/java/com/project/capstone/content/domain/Content.java +++ b/backend/src/main/java/com/project/capstone/content/domain/Content.java @@ -1,5 +1,6 @@ package com.project.capstone.content.domain; +import com.fasterxml.jackson.annotation.JsonBackReference; import com.project.capstone.book.domain.Book; import com.project.capstone.club.domain.Club; import com.project.capstone.member.domain.Member; @@ -28,12 +29,15 @@ public class Content { private String body; private int likes; + @JsonBackReference @ManyToOne private Member member; + @JsonBackReference @ManyToOne private Book book; + @JsonBackReference @ManyToOne private Club club; } diff --git a/backend/src/main/java/com/project/capstone/content/domain/ContentRepository.java b/backend/src/main/java/com/project/capstone/content/domain/ContentRepository.java index e9e3bb3baf..cf9c9190cc 100644 --- a/backend/src/main/java/com/project/capstone/content/domain/ContentRepository.java +++ b/backend/src/main/java/com/project/capstone/content/domain/ContentRepository.java @@ -2,6 +2,10 @@ import org.springframework.data.jpa.repository.JpaRepository; -public interface ContentRepository extends JpaRepository { +import java.util.List; +import java.util.Optional; +public interface ContentRepository extends JpaRepository { + Optional findContentById(Long id); + List findContentsByType(ContentType type); } diff --git a/backend/src/main/java/com/project/capstone/content/exception/ContentException.java b/backend/src/main/java/com/project/capstone/content/exception/ContentException.java new file mode 100644 index 0000000000..255c7a321a --- /dev/null +++ b/backend/src/main/java/com/project/capstone/content/exception/ContentException.java @@ -0,0 +1,10 @@ +package com.project.capstone.content.exception; + +import com.project.capstone.common.exception.BaseException; +import com.project.capstone.common.exception.ExceptionType; + +public class ContentException extends BaseException { + public ContentException(ExceptionType exceptionType) { + super(exceptionType); + } +} diff --git a/backend/src/main/java/com/project/capstone/content/exception/ContentExceptionType.java b/backend/src/main/java/com/project/capstone/content/exception/ContentExceptionType.java new file mode 100644 index 0000000000..ca4f3e9cae --- /dev/null +++ b/backend/src/main/java/com/project/capstone/content/exception/ContentExceptionType.java @@ -0,0 +1,34 @@ +package com.project.capstone.content.exception; + +import com.project.capstone.common.exception.ExceptionType; +import lombok.AllArgsConstructor; +import org.springframework.http.HttpStatus; + +import static org.springframework.http.HttpStatus.NOT_FOUND; + +@AllArgsConstructor +public enum ContentExceptionType implements ExceptionType { + CONTENT_NOT_FOUND(NOT_FOUND, 901, "해당 컨텐츠를 찾을 수 없습니다."), + TYPE_NOT_FOUND(NOT_FOUND, 902, "해당 타입을 찾을 수 없습니다.") + ; + + + private final HttpStatus status; + private final int exceptionCode; + private final String message; + + @Override + public HttpStatus httpStatus() { + return status; + } + + @Override + public int exceptionCode() { + return exceptionCode; + } + + @Override + public String message() { + return message; + } +} diff --git a/backend/src/main/java/com/project/capstone/content/service/ContentService.java b/backend/src/main/java/com/project/capstone/content/service/ContentService.java index 3bd7c76703..3efb4238b3 100644 --- a/backend/src/main/java/com/project/capstone/content/service/ContentService.java +++ b/backend/src/main/java/com/project/capstone/content/service/ContentService.java @@ -9,8 +9,12 @@ import com.project.capstone.club.exception.ClubException; import com.project.capstone.club.exception.ClubExceptionType; import com.project.capstone.content.controller.dto.ContentCreateRequest; +import com.project.capstone.content.controller.dto.ContentResponse; import com.project.capstone.content.domain.Content; import com.project.capstone.content.domain.ContentRepository; +import com.project.capstone.content.domain.ContentType; +import com.project.capstone.content.exception.ContentException; +import com.project.capstone.content.exception.ContentExceptionType; import com.project.capstone.member.domain.Member; import com.project.capstone.member.domain.MemberRepository; import com.project.capstone.member.exception.MemberException; @@ -19,10 +23,15 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; import static com.project.capstone.book.exception.BookExceptionType.BOOK_NOT_FOUND; import static com.project.capstone.club.exception.ClubExceptionType.CLUB_NOT_FOUND; +import static com.project.capstone.content.exception.ContentExceptionType.CONTENT_NOT_FOUND; +import static com.project.capstone.content.exception.ContentExceptionType.TYPE_NOT_FOUND; import static com.project.capstone.member.exception.MemberExceptionType.MEMBER_NOT_FOUND; @Service @@ -69,4 +78,24 @@ public void createContent(String userId, ContentCreateRequest request, Long book club.getContents().add(saved); } } + + public ContentResponse getContent(Long id) { + Content content = contentRepository.findContentById(id).orElseThrow( + () -> new ContentException(CONTENT_NOT_FOUND) + ); + + return new ContentResponse(content); + } + + public List getContents(String type) { + for (ContentType contentType : ContentType.values()) { + if (contentType.equals(ContentType.valueOf(type))) { + List contentsByType = contentRepository.findContentsByType(ContentType.valueOf(type)); + return contentsByType.stream() + .map(ContentResponse::new) + .toList(); + } + } + throw new ContentException(TYPE_NOT_FOUND); + } }