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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
12 changed files
with
214 additions
and
3 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
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
36 changes: 36 additions & 0 deletions
36
backend/src/main/java/com/project/capstone/post/controller/PostController.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,36 @@ | ||
package com.project.capstone.post.controller; | ||
|
||
import com.project.capstone.auth.domain.PrincipalDetails; | ||
import com.project.capstone.post.controller.dto.PostCreateRequest; | ||
import com.project.capstone.post.controller.dto.PostResponse; | ||
import com.project.capstone.post.service.PostService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/post") | ||
public class PostController { | ||
|
||
private final PostService postService; | ||
|
||
// 게시글 작성하기 | ||
@PostMapping("/create") | ||
public ResponseEntity<?> createPost(@AuthenticationPrincipal PrincipalDetails details, | ||
@RequestBody PostCreateRequest request, @RequestParam Long clubId) { | ||
postService.createPost(details.getUserId(), request, clubId); | ||
return ResponseEntity.ok().body("게시글 생성"); | ||
} | ||
|
||
// 게시글 조회 | ||
@GetMapping("/{postId}") | ||
public ResponseEntity<PostResponse> getPost(@AuthenticationPrincipal PrincipalDetails details, | ||
@PathVariable Long postId, @RequestParam Long clubId) { | ||
PostResponse postResponse = postService.getPost(details.getUserId(), postId, clubId); | ||
return ResponseEntity.ok().body(postResponse); | ||
} | ||
|
||
|
||
} |
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/com/project/capstone/post/controller/dto/PostCreateRequest.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.post.controller.dto; | ||
|
||
import java.util.UUID; | ||
|
||
public record PostCreateRequest( | ||
String title, | ||
String body, | ||
boolean isSticky | ||
) { | ||
} |
22 changes: 22 additions & 0 deletions
22
backend/src/main/java/com/project/capstone/post/controller/dto/PostResponse.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,22 @@ | ||
package com.project.capstone.post.controller.dto; | ||
|
||
import com.project.capstone.comment.domain.Comment; | ||
import com.project.capstone.post.domain.Post; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
public record PostResponse( | ||
Long id, | ||
UUID memberId, | ||
Long clubId, | ||
String title, | ||
String body, | ||
boolean isSticky, | ||
List<Comment> comments | ||
|
||
) { | ||
public PostResponse(Post post) { | ||
this(post.getId(), post.getMember().getId(), post.getClub().getId(), post.getTitle(), post.getBody(), post.isSticky(), post.getComments()); | ||
} | ||
} |
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/post/domain/PostRepository.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.post.domain; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface PostRepository extends JpaRepository<Post, Long> { | ||
Optional<Post> findPostById(Long id); | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/com/project/capstone/post/exception/PostException.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.post.exception; | ||
|
||
import com.project.capstone.common.exception.BaseException; | ||
import com.project.capstone.common.exception.ExceptionType; | ||
|
||
public class PostException extends BaseException { | ||
public PostException(ExceptionType exceptionType) { | ||
super(exceptionType); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
backend/src/main/java/com/project/capstone/post/exception/PostExceptionType.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.post.exception; | ||
|
||
import com.project.capstone.common.exception.ExceptionType; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
@AllArgsConstructor | ||
public enum PostExceptionType implements ExceptionType { | ||
POST_NOT_FOUND(NOT_FOUND, 401, "해당 게시글을 찾을 수 없습니다.") | ||
; | ||
|
||
|
||
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; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
backend/src/main/java/com/project/capstone/post/service/PostService.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,71 @@ | ||
package com.project.capstone.post.service; | ||
|
||
import com.project.capstone.club.domain.Club; | ||
import com.project.capstone.club.domain.ClubRepository; | ||
import com.project.capstone.club.exception.ClubException; | ||
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.memberclub.domain.MemberClubRepository; | ||
import com.project.capstone.memberclub.exception.MemberClubException; | ||
import com.project.capstone.memberclub.exception.MemberClubExceptionType; | ||
import com.project.capstone.post.controller.dto.PostCreateRequest; | ||
import com.project.capstone.post.controller.dto.PostResponse; | ||
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.club.exception.ClubExceptionType.*; | ||
import static com.project.capstone.member.exception.MemberExceptionType.MEMBER_NOT_FOUND; | ||
import static com.project.capstone.memberclub.exception.MemberClubExceptionType.*; | ||
import static com.project.capstone.post.exception.PostExceptionType.POST_NOT_FOUND; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class PostService { | ||
|
||
private final PostRepository postRepository; | ||
private final MemberRepository memberRepository; | ||
private final ClubRepository clubRepository; | ||
private final MemberClubRepository memberClubRepository; | ||
|
||
public void createPost(String memberId, PostCreateRequest request, Long clubId) { | ||
Member member = memberRepository.findMemberById(UUID.fromString(memberId)).orElseThrow( | ||
() -> new MemberException(MEMBER_NOT_FOUND) | ||
); | ||
Club club = clubRepository.findClubById(clubId).orElseThrow( | ||
() -> new ClubException(CLUB_NOT_FOUND) | ||
); | ||
|
||
if (memberClubRepository.findMemberClubByMember_IdAndClub_Id(UUID.fromString(memberId), clubId).isEmpty()) { | ||
throw new MemberClubException(MEMBERCLUB_NOT_FOUND); | ||
} | ||
|
||
Post saved = postRepository.save(Post.builder() | ||
.title(request.title()) | ||
.body(request.body()) | ||
.isSticky(request.isSticky()) | ||
.member(member) | ||
.club(club) | ||
.build()); | ||
|
||
member.getPosts().add(saved); | ||
club.getPosts().add(saved); | ||
} | ||
|
||
public PostResponse getPost(String memberId, Long id, Long clubId) { | ||
Post post = postRepository.findPostById(id).orElseThrow( | ||
() -> new PostException(POST_NOT_FOUND) | ||
); | ||
if (memberClubRepository.findMemberClubByMember_IdAndClub_Id(UUID.fromString(memberId), clubId).isEmpty()) { | ||
throw new MemberClubException(MEMBERCLUB_NOT_FOUND); | ||
} | ||
return new PostResponse(post); | ||
} | ||
} |
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