Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from Sookmyung-Software-Hackathon/hwi_feat/post/
Browse files Browse the repository at this point in the history
…#4

[feat] - post delete
  • Loading branch information
Mingadinga authored Aug 27, 2022
2 parents b247b4d + f7678b4 commit b36cc15
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/team20/t4/post/PostController.java
Original file line number Diff line number Diff line change
@@ -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.*;

Expand All @@ -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 삭제에 성공했습니다.");
}

}
23 changes: 19 additions & 4 deletions src/main/java/com/team20/t4/post/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}



// 삭제

// 리스트 조회
// 검색 기준 : 위치
// 정렬 기준 : 최근 수정순
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/team20/t4/post/PostUpdateRequestDto.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit b36cc15

Please sign in to comment.