forked from kookmin-sw/cap-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from kookmin-sw/BE
- Loading branch information
Showing
46 changed files
with
1,026 additions
and
16 deletions.
There are no files selected for viewing
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
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/com/project/capstone/book/domain/BookRepository.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,9 @@ | ||
package com.project.capstone.book.domain; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface BookRepository extends JpaRepository<Book, Long> { | ||
Optional<Book> findBookById(Long id); | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/com/project/capstone/book/exception/BookException.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,10 @@ | ||
package com.project.capstone.book.exception; | ||
|
||
import com.project.capstone.common.exception.BaseException; | ||
import com.project.capstone.common.exception.ExceptionType; | ||
|
||
public class BookException extends BaseException { | ||
public BookException(ExceptionType exceptionType) { | ||
super(exceptionType); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
backend/src/main/java/com/project/capstone/book/exception/BookExceptionType.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,33 @@ | ||
package com.project.capstone.book.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 BookExceptionType implements ExceptionType { | ||
|
||
BOOK_NOT_FOUND(NOT_FOUND, 801, "해당 책을 찾을 수 없습니다.") | ||
; | ||
|
||
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; | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
backend/src/main/java/com/project/capstone/comment/controller/CommentController.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,33 @@ | ||
package com.project.capstone.comment.controller; | ||
|
||
import com.project.capstone.auth.domain.PrincipalDetails; | ||
import com.project.capstone.comment.controller.dto.CommentResponse; | ||
import com.project.capstone.comment.controller.dto.CreateCommentRequest; | ||
import com.project.capstone.comment.service.CommentService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/comment") | ||
public class CommentController { | ||
|
||
private final CommentService commentService; | ||
|
||
@PostMapping("create") | ||
public ResponseEntity<?> createComment(@AuthenticationPrincipal PrincipalDetails details, | ||
@RequestParam Long postId, @RequestBody CreateCommentRequest request) { | ||
commentService.createPost(details.getUserId(), postId, request); | ||
return ResponseEntity.ok().body("댓글 생성 완료"); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<?> getComment(@AuthenticationPrincipal PrincipalDetails details, | ||
@PathVariable Long id) { | ||
CommentResponse commentResponse = commentService.getComment(details.getUserId(), id); | ||
return ResponseEntity.ok().body(commentResponse); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
backend/src/main/java/com/project/capstone/comment/controller/dto/CommentResponse.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,18 @@ | ||
package com.project.capstone.comment.controller.dto; | ||
|
||
import com.project.capstone.comment.domain.Comment; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.UUID; | ||
|
||
public record CommentResponse( | ||
Long id, | ||
Long postId, | ||
UUID memberId, | ||
String body, | ||
LocalDateTime createdAt | ||
) { | ||
public CommentResponse(Comment comment) { | ||
this(comment.getId(), comment.getPost().getId(), comment.getMember().getId(), comment.getBody(), comment.getCreatedAt()); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
backend/src/main/java/com/project/capstone/comment/controller/dto/CreateCommentRequest.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,6 @@ | ||
package com.project.capstone.comment.controller.dto; | ||
|
||
public record CreateCommentRequest ( | ||
String body | ||
) { | ||
} |
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
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/com/project/capstone/comment/domain/CommentRepository.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,9 @@ | ||
package com.project.capstone.comment.domain; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
Optional<Comment> findCommentById(Long id); | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/com/project/capstone/comment/exception/CommentException.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,10 @@ | ||
package com.project.capstone.comment.exception; | ||
|
||
import com.project.capstone.common.exception.BaseException; | ||
import com.project.capstone.common.exception.ExceptionType; | ||
|
||
public class CommentException extends BaseException { | ||
public CommentException(ExceptionType exceptionType) { | ||
super(exceptionType); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
backend/src/main/java/com/project/capstone/comment/exception/CommentExceptionType.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,32 @@ | ||
package com.project.capstone.comment.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 CommentExceptionType implements ExceptionType { | ||
COMMENT_NOT_FOUND(NOT_FOUND, 501, "해당 댓글을 찾을 수 없습니다."); | ||
; | ||
|
||
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; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
backend/src/main/java/com/project/capstone/comment/service/CommentService.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,62 @@ | ||
package com.project.capstone.comment.service; | ||
|
||
import com.project.capstone.comment.controller.dto.CommentResponse; | ||
import com.project.capstone.comment.controller.dto.CreateCommentRequest; | ||
import com.project.capstone.comment.domain.Comment; | ||
import com.project.capstone.comment.domain.CommentRepository; | ||
import com.project.capstone.comment.exception.CommentException; | ||
import com.project.capstone.member.domain.Member; | ||
import com.project.capstone.member.domain.MemberRepository; | ||
import com.project.capstone.member.exception.MemberException; | ||
import com.project.capstone.post.domain.Post; | ||
import com.project.capstone.post.domain.PostRepository; | ||
import com.project.capstone.post.exception.PostException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.UUID; | ||
|
||
import static com.project.capstone.comment.exception.CommentExceptionType.COMMENT_NOT_FOUND; | ||
import static com.project.capstone.member.exception.MemberExceptionType.MEMBER_NOT_FOUND; | ||
import static com.project.capstone.post.exception.PostExceptionType.POST_NOT_FOUND; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class CommentService { | ||
|
||
private final CommentRepository commentRepository; | ||
private final MemberRepository memberRepository; | ||
private final PostRepository postRepository; | ||
|
||
public void createPost(String userId, Long postId, CreateCommentRequest request) { | ||
Member member = memberRepository.findMemberById(UUID.fromString(userId)).orElseThrow( | ||
() -> new MemberException(MEMBER_NOT_FOUND) | ||
); | ||
|
||
Post post = postRepository.findPostById(postId).orElseThrow( | ||
() -> new PostException(POST_NOT_FOUND) | ||
); | ||
|
||
Comment saved = commentRepository.save(Comment.builder() | ||
.body(request.body()) | ||
.member(member) | ||
.post(post) | ||
.build()); | ||
|
||
member.getComments().add(saved); | ||
post.getComments().add(saved); | ||
} | ||
|
||
|
||
public CommentResponse getComment(String userId, Long id) { | ||
if (memberRepository.findMemberById(UUID.fromString(userId)).isEmpty()) { | ||
throw new MemberException(MEMBER_NOT_FOUND); | ||
} | ||
Comment comment = commentRepository.findCommentById(id).orElseThrow( | ||
() -> new CommentException(COMMENT_NOT_FOUND) | ||
); | ||
return new CommentResponse(comment); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
backend/src/main/java/com/project/capstone/content/controller/ContentController.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.project.capstone.content.controller; | ||
|
||
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); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/com/project/capstone/content/controller/dto/ContentCreateRequest.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,10 @@ | ||
package com.project.capstone.content.controller.dto; | ||
|
||
import com.project.capstone.content.domain.ContentType; | ||
|
||
public record ContentCreateRequest( | ||
ContentType contentType, | ||
String title, | ||
String body | ||
) { | ||
} |
Oops, something went wrong.