diff --git a/src/main/java/com/team20/t4/post/PostController.java b/src/main/java/com/team20/t4/post/PostController.java index 56f9475..060d0f6 100644 --- a/src/main/java/com/team20/t4/post/PostController.java +++ b/src/main/java/com/team20/t4/post/PostController.java @@ -1,5 +1,6 @@ package com.team20.t4.post; +import com.team20.t4.common.responseFormat.OnlyResponseString; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; @@ -20,4 +21,15 @@ public PostResponseDto getSinglePost(@PathVariable Long postId){ return postService.getSinglePost(postId); } + @PostMapping("/post/{postId}") + public Long updatePost(@PathVariable Long postId, @RequestBody PostUpdateRequestDto requestDto){ + return postService.updatePost(postId, requestDto); + } + + @DeleteMapping("/post/{postId}") + public OnlyResponseString deletePost(@PathVariable Long postId){ + postService.deletePost(postId); + return new OnlyResponseString("Post 삭제에 성공했습니다."); + } + } diff --git a/src/main/java/com/team20/t4/post/PostService.java b/src/main/java/com/team20/t4/post/PostService.java index 0fbb432..0b8e822 100644 --- a/src/main/java/com/team20/t4/post/PostService.java +++ b/src/main/java/com/team20/t4/post/PostService.java @@ -33,11 +33,26 @@ private Post getPostOrElseThrowRequestException(Long postId) { .orElseThrow(() -> new RequestException(RequestErrorCode.NOT_FOUND, "존재하지 않는 글입니다.")); } - // 수정 + @Transactional + public Long updatePost(Long postId, PostUpdateRequestDto requestDto) throws RequestException{ + checkPostExists(postId); + Post updatedPost = postRepository.save(requestDto.toEntity(postId)); + return updatedPost.getId(); + } + + private void checkPostExists(Long postId) { + if(!postRepository.existsById(postId)) + throw new RequestException(RequestErrorCode.NOT_FOUND, "요청한 Post가 존재하지 않습니다."); + } + + @Transactional + public void deletePost(Long postId){ + // plan이 진행중이면 plan도 삭제 + // plan이 완료이면 plan은 삭제하지 않음 + postRepository.deleteById(postId); + } + - - // 삭제 - // 리스트 조회 // 검색 기준 : 위치 // 정렬 기준 : 최근 수정순 diff --git a/src/main/java/com/team20/t4/post/PostUpdateRequestDto.java b/src/main/java/com/team20/t4/post/PostUpdateRequestDto.java new file mode 100644 index 0000000..3d02ea5 --- /dev/null +++ b/src/main/java/com/team20/t4/post/PostUpdateRequestDto.java @@ -0,0 +1,30 @@ +package com.team20.t4.post; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.Size; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class PostUpdateRequestDto { + private Long postId; + @Size(max = 255, message = "title은 255자 이하여야합니다.") + private String title; + @Size(max = 1000, message = "content는 1000자 이하여야합니다.") + private String content; + @Size(max = 255, message = "chatRoomLink는 255자 이하여야합니다.") + private String chatRoomLink; + // private PlanSaveRequestDto plan; + + public Post toEntity(Long postId) { + return Post.builder() + .id(postId) + .title(title) + .content(content) + .chatRoomLink(chatRoomLink) + .build(); + } +}