Skip to content

Commit

Permalink
Merge pull request #23 from kookmin-sw/BE_Feature/#18-post
Browse files Browse the repository at this point in the history
  • Loading branch information
wjdwlghks authored Apr 20, 2024
2 parents 657d44d + 20a103a commit 3896b63
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.project.capstone.club.controller.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.project.capstone.club.domain.Club;
import com.project.capstone.club.domain.PublicStatus;
import com.project.capstone.post.domain.Post;

import java.time.LocalDateTime;
import java.util.List;

public record ClubResponse (
Long id,
Expand All @@ -12,9 +15,11 @@ public record ClubResponse (
String name,
LocalDateTime createdAt,
int maximum,
PublicStatus publicstatus
PublicStatus publicstatus,
List<Post> posts

) {
public ClubResponse(Club club) {
this(club.getId(), club.getBook() == null ? null : club.getBook().getId(), club.getTopic(), club.getName(), club.getCreatedAt(), club.getMaximum(), club.getPublicStatus());
this(club.getId(), club.getBook() == null ? null : club.getBook().getId(), club.getTopic(), club.getName(), club.getCreatedAt(), club.getMaximum(), club.getPublicStatus(), club.getPosts());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.project.capstone.club.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.project.capstone.book.domain.Book;
import com.project.capstone.memberclub.domain.MemberClub;
import com.project.capstone.content.domain.Content;
Expand Down Expand Up @@ -40,12 +42,15 @@ public class Club {
private PublicStatus publicStatus;
private Integer password;

@JsonManagedReference
@OneToMany(mappedBy = "club")
private List<Post> posts = new ArrayList<>();

@JsonManagedReference
@OneToMany(mappedBy = "club")
private List<MemberClub> members = new ArrayList<>();

@JsonManagedReference
@OneToMany(mappedBy = "club")
private List<Content> contents = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@AllArgsConstructor
public enum MemberClubExceptionType implements ExceptionType {
ALREADY_JOIN(BAD_REQUEST, 301, "이미 가입한 모임입니다."),
MEMBERCLUB_NOT_FOUND(NOT_FOUND, 302, "위임 또는 추방하려는 멤버가 모임 구성원이 아닙니다.");
MEMBERCLUB_NOT_FOUND(NOT_FOUND, 302, "모임에서 멤버를 찾을 수 없습니다.");
;

private final HttpStatus status;
Expand Down
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);
}


}
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
) {
}
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());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.project.capstone.post.domain;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.project.capstone.club.domain.Club;
import com.project.capstone.comment.domain.Comment;
import com.project.capstone.member.domain.Member;
Expand Down Expand Up @@ -33,12 +35,15 @@ public class Post {
@Column(name = "is_sticky")
private boolean isSticky;

@JsonManagedReference
@OneToMany(mappedBy = "post")
private List<Comment> comments = new ArrayList<>();

@JsonBackReference
@ManyToOne
private Member member;

@JsonBackReference
@ManyToOne
private Club club;
}
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);
}
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);
}
}
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;
}
}
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);
}
}
5 changes: 5 additions & 0 deletions backend/src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ insert into club (id, manager_id, topic, name, created_at, maximum, public_statu
insert into club (id, manager_id, topic, name, created_at, maximum, public_status, password, book_id) values
(8, UNHEX(REPLACE("8fbcec63-e527-10ee-bd3d-0242ac120002", "-","")), '사회', '길동4의 모임', '2023-05-26 15:48:57.450179', 10, 'PUBLIC', null, 3);

insert into member_club (id, member_id, club_id) values
(1, UNHEX(REPLACE("3f06af63-a93c-11e4-9797-00505690773f", "-","")), 1);

insert into post (id, club_id, member_id, title, body, created_at, is_sticky) values
(1, 1, UNHEX(REPLACE("3f06af63-a93c-11e4-9797-00505690773f", "-","")), '게시글 제목 1', '게시글 내용 1', '2023-05-26 15:48:57.450179', true);

0 comments on commit 3896b63

Please sign in to comment.