Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] 게시글 수정하기 #135

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class CommentController {
private final CommentService commentService;

@Operation(summary = "댓글 작성")
@Operation(summary = "댓글 작성, 이미지가 없으면 null로 주시면 됩니다. ")
@PostMapping("/{postId}")
public ResponseEntity<ResponseData> createComment(@RequestBody CommentCreateRequest commentCreateRequest, @CurrentUser Account account, @PathVariable Long postId) {
try {
Expand All @@ -30,7 +30,7 @@ public ResponseEntity<ResponseData> createComment(@RequestBody CommentCreateRequ
}
}

@Operation(summary = "대댓글 작성")
@Operation(summary = "대댓글 작성, 이미지가 없으면 null로 주시면 됩니다.")
@PostMapping("/child/{commentId}")
public ResponseEntity<ResponseData> createChildComment(@RequestBody CommentCreateRequest commentCreateRequest, @CurrentUser Account account,@PathVariable Long commentId) {
try {
Expand All @@ -40,7 +40,7 @@ public ResponseEntity<ResponseData> createChildComment(@RequestBody CommentCreat
}
}

@Operation(summary = "댓글 수정")
@Operation(summary = "댓글 수정, 이미지가 없으면 null로 주시면 됩니다.")
@PutMapping("/{commentId}")
public ResponseEntity<ResponseData> updateComment(@RequestBody CommentCreateRequest commentCreateRequest, @CurrentUser Account account, @PathVariable Long commentId){
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class PostController {
private final PostService postService;

@Operation(summary = "게시물 작성")
@Operation(summary = "게시물 작성, 이미지가 없을 때는 빈 값으로 주시면 됩니다. ")
@PostMapping("")
public ResponseEntity<ResponseData> createPost( @CurrentUser Account account,@RequestBody PostCreateRequest createPostDTO) {
try{
Expand All @@ -34,6 +34,17 @@ public ResponseEntity<ResponseData> createPost( @CurrentUser Account account,@Re
}
}

@Operation(summary = "게시물 수정, 이미지가 없을 때는 빈 값으로 주시면 됩니다. ")
@PutMapping("/{postId}")
public ResponseEntity<ResponseData> updatePost(@CurrentUser Account account,@PathVariable Long postId, @RequestBody PostCreateRequest updateRequest) {
try {
return ResponseData.toResponseEntity(postService.updatePost(updateRequest,postId, account));
} catch (NoSuchElementException e) {
return ResponseData.toResponseEntity(ResponseCode.POST_UPDATE_FAIL);
}
}


@GetMapping("/type/{postType}")
@Operation(summary = "커뮤니티 글 목록 조회", description = "커뮤니티 글 목록을 조회합니다. type을 path variable로 받아 구분합니다.")
@ApiResponses(value = {
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/meltingpot/server/post/dto/PostDetailResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,35 @@ public class PostDetailResponse {
private String name;
private String title;
private String content;
List<String> imgUrls;
private List<ImageData> imgData; // Image data including ID and URL
private Integer commentCount;
private CommentsListResponse commentsList;
private LocalDateTime updatedAt;

public static PostDetailResponse of (Post post, CommentsListResponse commentsList) {
List<String> imgUrls = post.getPostImages().stream()
.map(PostImage::getImageUrl)
// Static nested class to hold image data
@Getter
@AllArgsConstructor
@NoArgsConstructor
public static class ImageData {
private Long imageId;
private String imageUrl;
}

public static PostDetailResponse of(Post post, CommentsListResponse commentsList) {
List<ImageData> imgData = post.getPostImages().stream()
.map(postImage -> new ImageData(postImage.getId(), postImage.getImageUrl()))
.collect(Collectors.toList());

int commentCount = post.getComments().stream()
.mapToInt(parentComment -> 1 + parentComment.getChildren().size())
.sum();

return PostDetailResponse.builder()
.postId(post.getId())
.name(post.getAccount().getName())
.title(post.getTitle())
.content(post.getContent())
.imgUrls(imgUrls)
.imgData(imgData)
.commentCount(commentCount)
.commentsList(commentsList)
.updatedAt(post.getUpdatedAt())
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/meltingpot/server/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import meltingpot.server.domain.entity.post.PostImage;
import meltingpot.server.domain.repository.AccountRepository;
import meltingpot.server.domain.repository.CommentRepository;
import meltingpot.server.domain.repository.PostImageRepository;
import meltingpot.server.domain.repository.PostRepository;
import meltingpot.server.post.dto.PostCreateRequest;
import meltingpot.server.post.dto.PostDetailResponse;
Expand All @@ -37,6 +38,7 @@ public class PostService {
private final AccountRepository accountRepository;
private final PostRepository postRepository;
private final CommentRepository commentRepository;
private final PostImageRepository postImageRepository;
@Autowired
private FileService fileService;

Expand All @@ -48,9 +50,29 @@ public ResponseCode createPost(PostCreateRequest createPostDTO,Account account){
List<PostImage> postImages = createPostImages(createPostDTO.getImageKeys(), post, account);
post.setPostImages(postImages);
postRepository.save(post);
return ResponseCode.POST_CREATE_SUCCESS;
return ResponseCode.CREATE_POST_SUCCESS;
}

/*post 수정하기*/
public ResponseCode updatePost(PostCreateRequest updateRequest,Long postId, Account account){
Post post = findPostById(postId);

post.setTitle(updateRequest.getTitle());
post.setContent(updateRequest.getContent());

// 기존의 모든 PostImage 삭제
if (post.getPostImages() != null) {
postImageRepository.deleteAll(post.getPostImages());

}
// 새로운 PostImage 설정
List<PostImage> postImages = createPostImages(updateRequest.getImageKeys(), post, account);
post.setPostImages(postImages);

postRepository.save(post);

return ResponseCode.UPDATE_POST_SUCCESS;
}

/*post 내용 불러오기*/
@Transactional(readOnly = true)
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/meltingpot/server/util/ResponseCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@ public enum ResponseCode {
READ_USERS_COMMENTS_SUCCESS(OK, "사용자가 댓글을 작성한 게시글 불러오기 성공"),
READ_USERS_PARTIES_SUCCESS(OK, "사용자가 참여한 파티 불러오기 성공"),

UPDATE_POST_SUCCESS(OK,"게시물 수정 성공"),
UPDATE_COMMENT_SUCCESS(OK, "댓글 수정 성공"),
READ_COMMENTS_LIST_SUCCESS(OK,"댓글 목록 불러오기 성공"),

POST_CREATE_SUCCESS(OK,"게시글 생성 성공"),



/* 201 CREATED : 요청 성공, 자원 생성 */
SIGNUP_SUCCESS(CREATED, "회원가입 성공"),
CREATE_CHAT_ROOM_SUCCESS(CREATED, "채팅방 생성 성공"),
CREATE_POST_SUCCESS(CREATED, "게시물 작성 성공"),
CREATE_COMMENT_SUCCESS(CREATED, "댓글 작성 성공"),
UPDATE_COMMENT_SUCCESS(CREATED, "댓글 수정 성공"),
CREATE_CHILD_COMMENT_SUCCESS(CREATED, "대댓글 작성 성공"),
REPORT_CREATE_SUCCESS(CREATED, "신고 작성 성공"),
PARTY_REPORT_SUCCESS(CREATED, "파티 신고 성공"),
Expand Down Expand Up @@ -91,6 +92,7 @@ public enum ResponseCode {
COMMENT_UPDATE_FAIL(BAD_REQUEST, "댓글 수정 실패 "),
READ_COMMENT_FAIL(BAD_REQUEST, "댓글 불러오기 실패 "),
POST_CREATE_FAIL(BAD_REQUEST, "게시글 작성 실패"),
POST_UPDATE_FAIL(BAD_REQUEST,"게시글 수정 실패"),
REPORT_CREATE_FAIL(BAD_REQUEST, "신고 작성 실패"),
AREA_FETCH_FAILED(BAD_REQUEST, "지역 조회 실패"),
AREA_FETCH_FAILED_NOT_SERVICE_AREA(BAD_REQUEST, "현재 좌표 조회는 국내에서만 사용 가능합니다"),
Expand Down