Skip to content

Commit 538fef0

Browse files
authored
Merge pull request #31 from kookmin-sw/BE_Feature/#25-content-and-quiz
#25
2 parents d4eb1d8 + 8f0472f commit 538fef0

26 files changed

+627
-12
lines changed

backend/src/main/java/com/project/capstone/book/domain/Book.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.project.capstone.book.domain;
22

33
import com.project.capstone.club.domain.Club;
4+
import com.project.capstone.content.domain.Content;
5+
import com.project.capstone.quiz.domain.Quiz;
46
import jakarta.persistence.*;
57
import lombok.AllArgsConstructor;
68
import lombok.Builder;
@@ -32,4 +34,10 @@ public class Book {
3234

3335
@OneToMany(mappedBy = "book")
3436
private List<Club> clubs = new ArrayList<>();
37+
38+
@OneToMany(mappedBy = "book")
39+
private List<Content> contents = new ArrayList<>();
40+
41+
@OneToMany(mappedBy = "book")
42+
private List<Quiz> quizzes = new ArrayList<>();
3543
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.project.capstone.book.domain;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
import java.util.Optional;
6+
7+
public interface BookRepository extends JpaRepository<Book, Long> {
8+
Optional<Book> findBookById(Long id);
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.project.capstone.book.exception;
2+
3+
import com.project.capstone.common.exception.BaseException;
4+
import com.project.capstone.common.exception.ExceptionType;
5+
6+
public class BookException extends BaseException {
7+
public BookException(ExceptionType exceptionType) {
8+
super(exceptionType);
9+
}
10+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.project.capstone.book.exception;
2+
3+
import com.project.capstone.common.exception.ExceptionType;
4+
import lombok.AllArgsConstructor;
5+
import org.springframework.http.HttpStatus;
6+
7+
import static org.springframework.http.HttpStatus.NOT_FOUND;
8+
9+
@AllArgsConstructor
10+
public enum BookExceptionType implements ExceptionType {
11+
12+
BOOK_NOT_FOUND(NOT_FOUND, 801, "해당 책을 찾을 수 없습니다.")
13+
;
14+
15+
private final HttpStatus status;
16+
private final int exceptionCode;
17+
private final String message;
18+
19+
@Override
20+
public HttpStatus httpStatus() {
21+
return status;
22+
}
23+
24+
@Override
25+
public int exceptionCode() {
26+
return exceptionCode;
27+
}
28+
29+
@Override
30+
public String message() {
31+
return message;
32+
}
33+
}

backend/src/main/java/com/project/capstone/club/domain/Club.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.project.capstone.memberclub.domain.MemberClub;
77
import com.project.capstone.content.domain.Content;
88
import com.project.capstone.post.domain.Post;
9+
import com.project.capstone.quiz.domain.Quiz;
910
import jakarta.persistence.*;
1011
import lombok.AllArgsConstructor;
1112
import lombok.Builder;
@@ -54,6 +55,10 @@ public class Club {
5455
@OneToMany(mappedBy = "club")
5556
private List<Content> contents = new ArrayList<>();
5657

58+
@JsonManagedReference
59+
@OneToMany(mappedBy = "club")
60+
private List<Quiz> quizzes = new ArrayList<>();
61+
5762
@ManyToOne
5863
private Book book;
5964

backend/src/main/java/com/project/capstone/club/domain/PublicStatus.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public enum PublicStatus {
1313
private final String description;
1414

1515

16-
@JsonCreator
16+
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
1717
public static PublicStatus from(String status) {
1818
for (PublicStatus publicStatus : PublicStatus.values()) {
1919
if (publicStatus.getDescription().equals(status)) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.project.capstone.content.controller;
2+
3+
import com.project.capstone.auth.domain.PrincipalDetails;
4+
import com.project.capstone.content.controller.dto.ContentCreateRequest;
5+
import com.project.capstone.content.controller.dto.ContentResponse;
6+
import com.project.capstone.content.service.ContentService;
7+
import lombok.RequiredArgsConstructor;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
10+
import org.springframework.web.bind.annotation.*;
11+
12+
import java.util.List;
13+
14+
@RestController
15+
@RequiredArgsConstructor
16+
@RequestMapping("/content")
17+
public class ContentController {
18+
19+
private final ContentService contentService;
20+
21+
// 컨텐츠 생성
22+
@PostMapping("/create")
23+
public ResponseEntity<?> createContent(@AuthenticationPrincipal PrincipalDetails details,
24+
@RequestBody ContentCreateRequest request,
25+
@RequestParam Long bookId, @RequestParam(required = false) Long clubId) {
26+
contentService.createContent(details.getUserId(), request, bookId, clubId);
27+
return ResponseEntity.ok().body("컨텐츠 생성 완료");
28+
}
29+
30+
// 단일 컨텐츠 조회
31+
@GetMapping("/{id}")
32+
public ResponseEntity<ContentResponse> getContent(@PathVariable Long id) {
33+
ContentResponse contentResponse = contentService.getContent(id);
34+
return ResponseEntity.ok().body(contentResponse);
35+
}
36+
37+
// 컨텐츠 종류별 조회
38+
@GetMapping("/get")
39+
public ResponseEntity<List<ContentResponse>> getContents(@RequestParam String type) {
40+
List<ContentResponse> contentResponseList = contentService.getContents(type);
41+
return ResponseEntity.ok().body(contentResponseList);
42+
}
43+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.project.capstone.content.controller.dto;
2+
3+
import com.project.capstone.content.domain.ContentType;
4+
5+
public record ContentCreateRequest(
6+
ContentType contentType,
7+
String title,
8+
String body
9+
) {
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.project.capstone.content.controller.dto;
2+
3+
import com.project.capstone.content.domain.Content;
4+
import com.project.capstone.content.domain.ContentType;
5+
6+
import java.util.UUID;
7+
8+
public record ContentResponse(
9+
Long id,
10+
UUID memberId,
11+
Long bookId,
12+
Long clubId,
13+
ContentType type,
14+
String title,
15+
String body,
16+
int likes
17+
) {
18+
public ContentResponse(Content content) {
19+
this(content.getId(), content.getMember().getId(), content.getBook().getId(),
20+
content.getClub() == null ? null : content.getClub().getId(), content.getType(), content.getTitle(), content.getBody(), content.getLikes());
21+
}
22+
}

backend/src/main/java/com/project/capstone/content/domain/Content.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.project.capstone.content.domain;
22

3+
import com.fasterxml.jackson.annotation.JsonBackReference;
34
import com.project.capstone.book.domain.Book;
45
import com.project.capstone.club.domain.Club;
56
import com.project.capstone.member.domain.Member;
@@ -20,21 +21,23 @@ public class Content {
2021
@Id
2122
@GeneratedValue(strategy = GenerationType.IDENTITY)
2223
private Long id;
23-
private String type;
24-
@Column(name = "quiz_type")
25-
private String quizType;
26-
@Column(name = "quiz_answer")
27-
private String quizAnswer;
24+
25+
@Enumerated(EnumType.STRING)
26+
@Column(name = "content_type")
27+
private ContentType type;
2828
private String title;
2929
private String body;
30-
private String likes;
30+
private int likes;
3131

32+
@JsonBackReference
3233
@ManyToOne
3334
private Member member;
3435

36+
@JsonBackReference
3537
@ManyToOne
3638
private Book book;
3739

40+
@JsonBackReference
3841
@ManyToOne
3942
private Club club;
4043
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.project.capstone.content.domain;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
import java.util.List;
6+
import java.util.Optional;
7+
8+
public interface ContentRepository extends JpaRepository<Content, Long> {
9+
Optional<Content> findContentById(Long id);
10+
List<Content> findContentsByType(ContentType type);
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.project.capstone.content.domain;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.extern.slf4j.Slf4j;
7+
8+
@AllArgsConstructor
9+
@Getter
10+
@Slf4j
11+
public enum ContentType {
12+
13+
Review("독후감"),
14+
Quotation("인용구"),
15+
ShortReview("한줄평");
16+
17+
private final String type;
18+
19+
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
20+
public static ContentType from(String type) {
21+
for (ContentType contentType : ContentType.values()) {
22+
if (contentType.getType().equals(type)) {
23+
return contentType;
24+
}
25+
}
26+
throw new RuntimeException("잘못된 컨텐츠 타입 입니다.");
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.project.capstone.content.exception;
2+
3+
import com.project.capstone.common.exception.BaseException;
4+
import com.project.capstone.common.exception.ExceptionType;
5+
6+
public class ContentException extends BaseException {
7+
public ContentException(ExceptionType exceptionType) {
8+
super(exceptionType);
9+
}
10+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.project.capstone.content.exception;
2+
3+
import com.project.capstone.common.exception.ExceptionType;
4+
import lombok.AllArgsConstructor;
5+
import org.springframework.http.HttpStatus;
6+
7+
import static org.springframework.http.HttpStatus.NOT_FOUND;
8+
9+
@AllArgsConstructor
10+
public enum ContentExceptionType implements ExceptionType {
11+
CONTENT_NOT_FOUND(NOT_FOUND, 901, "해당 컨텐츠를 찾을 수 없습니다."),
12+
TYPE_NOT_FOUND(NOT_FOUND, 902, "해당 타입을 찾을 수 없습니다.")
13+
;
14+
15+
16+
private final HttpStatus status;
17+
private final int exceptionCode;
18+
private final String message;
19+
20+
@Override
21+
public HttpStatus httpStatus() {
22+
return status;
23+
}
24+
25+
@Override
26+
public int exceptionCode() {
27+
return exceptionCode;
28+
}
29+
30+
@Override
31+
public String message() {
32+
return message;
33+
}
34+
}

0 commit comments

Comments
 (0)