Skip to content

Commit

Permalink
#15 Feat: 게시글 수정 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
diddnwjd committed Jan 11, 2024
1 parent 646175b commit 2ea7129
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/ixxp/culpop/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ public ResponseEntity<List<PostResponse>> getPost(@RequestParam(name = "category
public ResponseEntity<PostDetailResponse> getPostDetail(@PathVariable int postId) {
return ResponseEntity.ok(postService.getPostDetail(postId));
}

// 게시글 수정
@PatchMapping("/{postId}")
public ResponseEntity<StatusResponse> updatePost(@AuthenticationPrincipal UserDetailsImpl userDetails,
@PathVariable int postId,
@RequestBody PostRequest postRequest) {
postService.updatePost(userDetails.getUser(), postId, postRequest);
StatusResponse statusResponse = new StatusResponse(HttpStatus.OK.value(), "post 수정 완료");
return ResponseEntity.ok(statusResponse);
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/ixxp/culpop/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ public Post(User user, Category category, String title, String content) {
this.content = content;
this.viewCount = 0;
}

public void updatePost(User user, Category category, String title, String content) {
this.user = user;
this.category = category;
this.title = title;
this.content = content;
}
}
1 change: 1 addition & 0 deletions src/main/java/com/ixxp/culpop/mapper/PostMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public interface PostMapper {
void insertPost(Post post);
List<Post> selectPost(String category, int offset);
Post selectPostDetail(int postId);
void updatePost(Post post);
}
27 changes: 27 additions & 0 deletions src/main/java/com/ixxp/culpop/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -58,4 +59,30 @@ public PostDetailResponse getPostDetail(int postId) {
return new PostDetailResponse(postId, post.getUser().getUsername(), post.getTitle(), post.getContent()
, post.getCreatedAt(), post.getModifiedAt(), likeCount, post.getViewCount(), post.getCategory().getCateName());
}

// 게시글 수정
@Transactional
public void updatePost(User user, int postId, PostRequest postRequest) {
Post post = postMapper.selectPostDetail(postId);
if (post == null) {
throw new IllegalArgumentException("post가 존재하지 않습니다.");
}

if (post.getUser().getId() != user.getId()) {
throw new IllegalArgumentException("작성자만 수정 가능합니다.");
}

String cateName = postRequest.getCateName();
Category category = post.getCategory();
if (!Objects.equals(cateName, post.getCategory().getCateName())) {
category = categoryMapper.selectCategory(cateName);
if (category == null) {
category = new Category(cateName);
categoryMapper.insertCategory(category);
}
}

post.updatePost(user, category, postRequest.getTitle(), postRequest.getContent());
postMapper.updatePost(post);
}
}
4 changes: 4 additions & 0 deletions src/main/resources/mapper/post-mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@
LEFT JOIN Category c ON p.categoryId = c.id
WHERE p.id=#{id}
</select>
<update id="updatePost" parameterType="post">
UPDATE Post SET title=#{title}, content=#{content}, categoryId=#{category.id}
WHERE id=#{id}
</update>
</mapper>

0 comments on commit 2ea7129

Please sign in to comment.