Skip to content

Commit

Permalink
#25 feat: content단건 조회, 종류별 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
wjdwlghks committed Apr 25, 2024
1 parent 593b454 commit 86820cd
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,42 @@

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")
public class ContentController {

private final ContentService contentService;

// 컨텐츠 생성
@PostMapping("/create")
public ResponseEntity<?> createContent(@AuthenticationPrincipal PrincipalDetails details,
@RequestBody ContentCreateRequest request,
@RequestParam Long bookId, @RequestParam(required = false) Long clubId) {
contentService.createContent(details.getUserId(), request, bookId, clubId);
return ResponseEntity.ok().body("컨텐츠 생성 완료");
}

// 단일 컨텐츠 조회
@GetMapping("/{id}")
public ResponseEntity<ContentResponse> getContent(@PathVariable Long id) {
ContentResponse contentResponse = contentService.getContent(id);
return ResponseEntity.ok().body(contentResponse);
}

// 컨텐츠 종류별 조회
@GetMapping("/get")
public ResponseEntity<List<ContentResponse>> getContents(@RequestParam String type) {
List<ContentResponse> contentResponseList = contentService.getContents(type);
return ResponseEntity.ok().body(contentResponseList);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import org.springframework.data.jpa.repository.JpaRepository;

public interface ContentRepository extends JpaRepository<Content, Long> {
import java.util.List;
import java.util.Optional;

public interface ContentRepository extends JpaRepository<Content, Long> {
Optional<Content> findContentById(Long id);
List<Content> findContentsByType(ContentType type);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<ContentResponse> getContents(String type) {
for (ContentType contentType : ContentType.values()) {
if (contentType.equals(ContentType.valueOf(type))) {
List<Content> contentsByType = contentRepository.findContentsByType(ContentType.valueOf(type));
return contentsByType.stream()
.map(ContentResponse::new)
.toList();
}
}
throw new ContentException(TYPE_NOT_FOUND);
}
}

0 comments on commit 86820cd

Please sign in to comment.