Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RequestMapping("/application")
@RequestMapping("/applications")
@RestController
public class ApplicationController {

private final ApplicationSubmissionService applicationSubmissionService;
private final ApplicationQueryService applicationQueryService;

// 지원서 제출하기 api
@PostMapping()
@PostMapping
public ResponseEntity<ApplicationSubmissionResponse> apply(
@AuthorizedUser SiteUser siteUser,
@Valid @RequestBody ApplyRequest applyRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
package com.example.solidconnection.community.board.controller;

import com.example.solidconnection.community.post.dto.PostListResponse;
import com.example.solidconnection.community.post.service.PostQueryService;
import com.example.solidconnection.type.BoardCode;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/communities")
@RequestMapping("/boards")
public class BoardController {

private final PostQueryService postQueryService;

// todo: 회원별로 접근 가능한 게시판 목록 조회 기능 개발
@GetMapping()
@GetMapping
public ResponseEntity<?> findAccessibleCodes() {
List<String> accessibleCodeList = new ArrayList<>();
for (BoardCode boardCode : BoardCode.values()) {
accessibleCodeList.add(String.valueOf(boardCode));
}
return ResponseEntity.ok().body(accessibleCodeList);
}

@GetMapping("/{code}")
public ResponseEntity<?> findPostsByCodeAndCategory(
@PathVariable(value = "code") String code,
@RequestParam(value = "category", defaultValue = "전체") String category) {
Comment on lines +34 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이건 게시글 리스트를 조회하는 것이길래 제가 지난번에 post쪽으로 옮겼던 것인데 boards가 더 적절한가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저렇게 바꾸게 된 맥락을 말씀드리자면!
원래 특정 게시판의 게시글 목록 조회가 /communities/{code}였었어요.
그런데 uri 회의에서 communities 였던걸 boards 로 바꾸기로 해서 /boards/{code} 로 바꾼거예요.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 기억났습니다! ㅎㅎ.. 깜빡했네요

List<PostListResponse> postsByCodeAndPostCategory = postQueryService
.findPostsByCodeAndPostCategory(code, category);
return ResponseEntity.ok().body(postsByCodeAndPostCategory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,36 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/posts")
@RequestMapping("/comments")
public class CommentController {

private final CommentService commentService;

@PostMapping("/{post_id}/comments")
@PostMapping
public ResponseEntity<?> createComment(
@AuthorizedUser SiteUser siteUser,
@PathVariable("post_id") Long postId,
@Valid @RequestBody CommentCreateRequest commentCreateRequest
) {
CommentCreateResponse response = commentService.createComment(siteUser, postId, commentCreateRequest);
CommentCreateResponse response = commentService.createComment(siteUser, commentCreateRequest);
return ResponseEntity.ok().body(response);
}

@PatchMapping("/{post_id}/comments/{comment_id}")
@PatchMapping("/{comment_id}")
public ResponseEntity<?> updateComment(
@AuthorizedUser SiteUser siteUser,
@PathVariable("post_id") Long postId,
@PathVariable("comment_id") Long commentId,
@Valid @RequestBody CommentUpdateRequest commentUpdateRequest
) {
CommentUpdateResponse response = commentService.updateComment(siteUser, postId, commentId, commentUpdateRequest);
CommentUpdateResponse response = commentService.updateComment(siteUser, commentId, commentUpdateRequest);
return ResponseEntity.ok().body(response);
}

@DeleteMapping("/{post_id}/comments/{comment_id}")
@DeleteMapping("/{comment_id}")
public ResponseEntity<?> deleteCommentById(
@AuthorizedUser SiteUser siteUser,
@PathVariable("post_id") Long postId,
@PathVariable("comment_id") Long commentId
) {
CommentDeleteResponse response = commentService.deleteCommentById(siteUser, postId, commentId);
CommentDeleteResponse response = commentService.deleteCommentById(siteUser, commentId);
return ResponseEntity.ok().body(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import com.example.solidconnection.community.post.domain.Post;
import com.example.solidconnection.siteuser.domain.SiteUser;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

public record CommentCreateRequest(
@NotNull(message = "게시글 ID를 설정해주세요.")
Long postId,

@NotBlank(message = "댓글 내용은 빈 값일 수 없습니다.")
@Size(min = 1, max = 255, message = "댓글 내용은 최소 1자 이상, 최대 255자 이하여야 합니다.")
String content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import com.example.solidconnection.community.comment.dto.CommentUpdateResponse;
import com.example.solidconnection.community.comment.dto.PostFindCommentResponse;
import com.example.solidconnection.community.comment.repository.CommentRepository;
import com.example.solidconnection.custom.exception.CustomException;
import com.example.solidconnection.community.post.domain.Post;
import com.example.solidconnection.community.post.repository.PostRepository;
import com.example.solidconnection.custom.exception.CustomException;
import com.example.solidconnection.siteuser.domain.SiteUser;
import com.example.solidconnection.siteuser.repository.SiteUserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -22,13 +23,15 @@
import static com.example.solidconnection.custom.exception.ErrorCode.CAN_NOT_UPDATE_DEPRECATED_COMMENT;
import static com.example.solidconnection.custom.exception.ErrorCode.INVALID_COMMENT_LEVEL;
import static com.example.solidconnection.custom.exception.ErrorCode.INVALID_POST_ACCESS;
import static com.example.solidconnection.custom.exception.ErrorCode.USER_NOT_FOUND;

@Service
@RequiredArgsConstructor
public class CommentService {

private final CommentRepository commentRepository;
private final PostRepository postRepository;
private final SiteUserRepository siteUserRepository;

@Transactional(readOnly = true)
public List<PostFindCommentResponse> findCommentsByPostId(SiteUser siteUser, Long postId) {
Expand All @@ -43,15 +46,23 @@ private Boolean isOwner(Comment comment, SiteUser siteUser) {
}

@Transactional
public CommentCreateResponse createComment(SiteUser siteUser, Long postId, CommentCreateRequest commentCreateRequest) {
Post post = postRepository.getById(postId);
public CommentCreateResponse createComment(SiteUser siteUser, CommentCreateRequest commentCreateRequest) {
Post post = postRepository.getById(commentCreateRequest.postId());

Comment parentComment = null;
if (commentCreateRequest.parentId() != null) {
parentComment = commentRepository.getById(commentCreateRequest.parentId());
validateCommentDepth(parentComment);
}
Comment createdComment = commentRepository.save(commentCreateRequest.toEntity(siteUser, post, parentComment));

/*
* todo: siteUser를 영속 상태로 만들 수 있도록 컨트롤러에서 siteUserId 를 넘겨줄 것인지,
* siteUser 에 postList 를 FetchType.EAGER 로 설정할 것인지,
* post 와 siteUser 사이의 양방향을 끊을 것인지 생각해봐야한다.
*/
SiteUser siteUser1 = siteUserRepository.findById(siteUser.getId()).orElseThrow(() -> new CustomException(USER_NOT_FOUND));
Comment comment = commentCreateRequest.toEntity(siteUser1, post, parentComment);
Comment createdComment = commentRepository.save(comment);

return CommentCreateResponse.from(createdComment);
}
Expand All @@ -64,8 +75,7 @@ private void validateCommentDepth(Comment parentComment) {
}

@Transactional
public CommentUpdateResponse updateComment(SiteUser siteUser, Long postId, Long commentId, CommentUpdateRequest commentUpdateRequest) {
Post post = postRepository.getById(postId);
public CommentUpdateResponse updateComment(SiteUser siteUser, Long commentId, CommentUpdateRequest commentUpdateRequest) {
Comment comment = commentRepository.getById(commentId);
validateDeprecated(comment);
validateOwnership(comment, siteUser);
Expand All @@ -82,8 +92,7 @@ private void validateDeprecated(Comment comment) {
}

@Transactional
public CommentDeleteResponse deleteCommentById(SiteUser siteUser, Long postId, Long commentId) {
Post post = postRepository.getById(postId);
public CommentDeleteResponse deleteCommentById(SiteUser siteUser, Long commentId) {
Comment comment = commentRepository.getById(commentId);
validateOwnership(comment, siteUser);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.example.solidconnection.community.post.controller;

import com.example.solidconnection.community.post.dto.PostListResponse;
import com.example.solidconnection.custom.resolver.AuthorizedUser;
import com.example.solidconnection.community.post.dto.PostCreateRequest;
import com.example.solidconnection.community.post.dto.PostCreateResponse;
import com.example.solidconnection.community.post.dto.PostDeleteResponse;
Expand All @@ -13,6 +11,7 @@
import com.example.solidconnection.community.post.service.PostCommandService;
import com.example.solidconnection.community.post.service.PostLikeService;
import com.example.solidconnection.community.post.service.PostQueryService;
import com.example.solidconnection.custom.resolver.AuthorizedUser;
import com.example.solidconnection.siteuser.domain.SiteUser;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -33,41 +32,29 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/communities")
@RequestMapping("/posts")
public class PostController {

private final PostQueryService postQueryService;
private final PostCommandService postCommandService;
private final PostLikeService postLikeService;

@GetMapping("/{code}")
public ResponseEntity<?> findPostsByCodeAndCategory(
@PathVariable(value = "code") String code,
@RequestParam(value = "category", defaultValue = "전체") String category) {

List<PostListResponse> postsByCodeAndPostCategory = postQueryService
.findPostsByCodeAndPostCategory(code, category);
return ResponseEntity.ok().body(postsByCodeAndPostCategory);
}

@PostMapping(value = "/{code}/posts")
@PostMapping
public ResponseEntity<?> createPost(
@AuthorizedUser SiteUser siteUser,
@PathVariable("code") String code,
@Valid @RequestPart("postCreateRequest") PostCreateRequest postCreateRequest,
@RequestParam(value = "file", required = false) List<MultipartFile> imageFile
) {
if (imageFile == null) {
imageFile = Collections.emptyList();
}
PostCreateResponse post = postCommandService.createPost(siteUser, code, postCreateRequest, imageFile);
PostCreateResponse post = postCommandService.createPost(siteUser, postCreateRequest, imageFile);
return ResponseEntity.ok().body(post);
}

@PatchMapping(value = "/{code}/posts/{post_id}")
@PatchMapping(value = "/{post_id}")
public ResponseEntity<?> updatePost(
@AuthorizedUser SiteUser siteUser,
@PathVariable("code") String code,
@PathVariable("post_id") Long postId,
@Valid @RequestPart("postUpdateRequest") PostUpdateRequest postUpdateRequest,
@RequestParam(value = "file", required = false) List<MultipartFile> imageFile
Expand All @@ -76,48 +63,44 @@ public ResponseEntity<?> updatePost(
imageFile = Collections.emptyList();
}
PostUpdateResponse postUpdateResponse = postCommandService.updatePost(
siteUser, code, postId, postUpdateRequest, imageFile
siteUser, postId, postUpdateRequest, imageFile
);
return ResponseEntity.ok().body(postUpdateResponse);
}

@GetMapping("/{code}/posts/{post_id}")
@GetMapping("/{post_id}")
public ResponseEntity<?> findPostById(
@AuthorizedUser SiteUser siteUser,
@PathVariable("code") String code,
@PathVariable("post_id") Long postId
) {
PostFindResponse postFindResponse = postQueryService.findPostById(siteUser, code, postId);
PostFindResponse postFindResponse = postQueryService.findPostById(siteUser, postId);
return ResponseEntity.ok().body(postFindResponse);
}

@DeleteMapping(value = "/{code}/posts/{post_id}")
@DeleteMapping(value = "/{post_id}")
public ResponseEntity<?> deletePostById(
@AuthorizedUser SiteUser siteUser,
@PathVariable("code") String code,
@PathVariable("post_id") Long postId
) {
PostDeleteResponse postDeleteResponse = postCommandService.deletePostById(siteUser, code, postId);
PostDeleteResponse postDeleteResponse = postCommandService.deletePostById(siteUser, postId);
return ResponseEntity.ok().body(postDeleteResponse);
}

@PostMapping(value = "/{code}/posts/{post_id}/like")
@PostMapping(value = "/{post_id}/like")
public ResponseEntity<?> likePost(
@AuthorizedUser SiteUser siteUser,
@PathVariable("code") String code,
@PathVariable("post_id") Long postId
) {
PostLikeResponse postLikeResponse = postLikeService.likePost(siteUser, code, postId);
PostLikeResponse postLikeResponse = postLikeService.likePost(siteUser, postId);
return ResponseEntity.ok().body(postLikeResponse);
}

@DeleteMapping(value = "/{code}/posts/{post_id}/like")
@DeleteMapping(value = "/{post_id}/like")
public ResponseEntity<?> dislikePost(
@AuthorizedUser SiteUser siteUser,
@PathVariable("code") String code,
@PathVariable("post_id") Long postId
) {
PostDislikeResponse postDislikeResponse = postLikeService.dislikePost(siteUser, code, postId);
PostDislikeResponse postDislikeResponse = postLikeService.dislikePost(siteUser, postId);
return ResponseEntity.ok().body(postDislikeResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import jakarta.validation.constraints.Size;

public record PostCreateRequest(
@NotNull(message = "게시글 카테고리를 설정해주세요.")
String boardCode,

@NotNull(message = "게시글 카테고리를 설정해주세요.")
String postCategory,

Expand Down
Loading