Skip to content

Commit 275ac49

Browse files
authored
[feat] 게시글 수정 구현#45
[feat] 게시글 수정 구현#45
2 parents 3e33fb1 + f637f7b commit 275ac49

File tree

5 files changed

+126
-28
lines changed

5 files changed

+126
-28
lines changed

src/main/java/com/back/domain/post/post/controller/PostController.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.back.domain.post.post.controller;
22

3-
import com.back.domain.post.post.dto.request.PostRequestDto;
3+
import com.back.domain.post.post.dto.request.PostCreateRequestDto;
4+
import com.back.domain.post.post.dto.request.PostUpdateRequestDto;
45
import com.back.domain.post.post.dto.response.PostResponseDto;
5-
import com.back.domain.post.post.entity.Post;
66
import com.back.domain.post.post.service.PostService;
77
import com.back.global.rsData.RsData;
8+
import jakarta.validation.Valid;
89
import java.util.List;
910
import lombok.RequiredArgsConstructor;
1011
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.PatchMapping;
1113
import org.springframework.web.bind.annotation.PathVariable;
1214
import org.springframework.web.bind.annotation.PostMapping;
1315
import org.springframework.web.bind.annotation.RequestBody;
@@ -23,14 +25,14 @@ public class PostController {
2325

2426
/**
2527
* 게시글 작성 API
26-
* @param postRequestDto 게시글 작성 요청 DTO
28+
* @param reqBody 게시글 작성 요청 DTO
2729
* @return 작성된 게시글 정보
2830
*/
2931
@PostMapping
3032
public RsData<PostResponseDto> createPost(
31-
@RequestBody PostRequestDto postRequestDto
33+
@Valid @RequestBody PostCreateRequestDto reqBody
3234
) {
33-
return RsData.successOf(postService.createPost(postRequestDto)); // code=200, message="success"
35+
return RsData.successOf(postService.createPost(reqBody)); // code=200, message="success"
3436
}
3537

3638
/**
@@ -53,4 +55,18 @@ public RsData<PostResponseDto> getPost(
5355
) {
5456
return RsData.successOf(postService.getPost(postId)); // code=200, message="success"
5557
}
58+
59+
/**
60+
* 게시글 수정 API
61+
* @param postId 수정할 게시글 ID
62+
* @param reqBody 게시글 수정 요청 DTO
63+
* @return 수정된 게시글 정보
64+
*/
65+
@PatchMapping("/{postId}")
66+
public RsData<PostResponseDto> updatePost(
67+
@PathVariable Long postId,
68+
@Valid @RequestBody PostUpdateRequestDto reqBody
69+
) {
70+
return RsData.successOf(postService.updatePost(postId, reqBody)); // code=200, message="success"
71+
}
5672
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.back.domain.post.post.dto.request;
2+
3+
import com.back.domain.post.post.enums.PostStatus;
4+
import jakarta.validation.constraints.NotBlank;
5+
import java.util.List;
6+
7+
public record PostCreateRequestDto(
8+
Long categoryId,
9+
PostStatus status,
10+
@NotBlank (message = "제목은 필수입니다.")
11+
String title,
12+
@NotBlank (message = "내용은 필수입니다.")
13+
String content,
14+
String imageUrl,
15+
List<String> tags
16+
) {
17+
}

src/main/java/com/back/domain/post/post/dto/request/PostRequestDto.java renamed to src/main/java/com/back/domain/post/post/dto/request/PostUpdateRequestDto.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.back.domain.post.post.dto.request;
22

3+
import com.back.domain.post.post.enums.PostStatus;
34
import java.util.List;
45

5-
public record PostRequestDto(
6+
public record PostUpdateRequestDto(
67
Long categoryId,
8+
PostStatus status,
79
String title,
810
String content,
9-
String userNickName,
1011
String imageUrl,
1112
List<String> tags
1213
) {

src/main/java/com/back/domain/post/post/entity/Post.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
@Entity
3333
@Getter
34-
@Setter
3534
@Table(name = "post")
3635
@EntityListeners(AuditingEntityListener.class)
3736
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
@@ -97,8 +96,32 @@ public class Post {
9796
@Column(name = "view_count")
9897
private Integer viewCount;
9998

99+
public void updateCategory(Category category) {
100+
this.category = category;
101+
}
102+
103+
public void updateStatus(PostStatus status) {
104+
this.status = status;
105+
}
106+
107+
public void updateTitle(String title) {
108+
this.title = title;
109+
}
110+
111+
public void updateContent(String content) {
112+
this.content = content;
113+
}
114+
115+
public void updateImage(String imageUrl) {
116+
this.imageUrl = imageUrl;
117+
}
118+
100119
public void addTag(Tag tag) {
101120
PostTag postTag = PostTag.create(this, tag);
102121
this.postTags.add(postTag);
103122
}
123+
124+
public void clearTags() {
125+
this.postTags.clear();
126+
}
104127
}

src/main/java/com/back/domain/post/post/service/PostService.java

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import com.back.domain.post.category.entity.Category;
44
import com.back.domain.post.category.repository.CategoryRepository;
5-
import com.back.domain.post.post.dto.request.PostRequestDto;
5+
import com.back.domain.post.post.dto.request.PostCreateRequestDto;
6+
import com.back.domain.post.post.dto.request.PostUpdateRequestDto;
67
import com.back.domain.post.post.dto.response.PostResponseDto;
78
import com.back.domain.post.post.entity.Post;
89
import com.back.domain.post.post.entity.Tag;
@@ -28,34 +29,23 @@ public class PostService {
2829

2930
// 게시글 작성 로직
3031
@Transactional
31-
public PostResponseDto createPost(PostRequestDto postRequestDto) {
32+
public PostResponseDto createPost(PostCreateRequestDto reqBody) {
3233
User user = rq.getActor(); // 현재 로그인한 사용자의 정보 가져오기
3334

34-
Category category = categoryRepository.findById(postRequestDto.categoryId())
35-
.orElseThrow(() -> new IllegalArgumentException("해당 카테고리를 찾을 수 없습니다. ID: " + postRequestDto.categoryId()));
35+
Category category = categoryRepository.findById(reqBody.categoryId())
36+
.orElseThrow(() -> new IllegalArgumentException("해당 카테고리를 찾을 수 없습니다. ID: " + reqBody.categoryId()));
3637

3738
Post post = Post.builder()
3839
.category(category)
3940
.user(user)
40-
.title(postRequestDto.title())
41-
.content(postRequestDto.content())
42-
.imageUrl(postRequestDto.imageUrl())
41+
.title(reqBody.title())
42+
.content(reqBody.content())
43+
.imageUrl(reqBody.imageUrl())
4344
.build();
4445

45-
List<String> tagNames = postRequestDto.tags();
46+
List<String> tagNames = reqBody.tags();
4647
if (tagNames != null && !tagNames.isEmpty()) {
47-
for (String tagName : tagNames) {
48-
// 태그 이름으로 Tag 엔티티를 조회하거나, 없으면 새로 생성하여 저장
49-
Tag tag = tagRepository.findByName(tagName)
50-
.orElseGet(() -> tagRepository.save(
51-
Tag.builder()
52-
.name(tagName)
53-
.build()
54-
)
55-
);
56-
57-
post.addTag(tag);
58-
}
48+
addTag(tagNames, post);
5949
}
6050

6151
return new PostResponseDto(postRepository.save(post));
@@ -79,4 +69,55 @@ public PostResponseDto getPost(Long postId) {
7969
.orElseThrow(() -> new NoSuchElementException("해당 게시글을 찾을 수 없습니다. ID: " + postId))
8070
);
8171
}
72+
73+
// 게시글 수정 로직
74+
@Transactional
75+
public PostResponseDto updatePost(Long postId, PostUpdateRequestDto reqBody) {
76+
Post post = postRepository.findById(postId)
77+
.orElseThrow(() -> new NoSuchElementException("해당 게시글을 찾을 수 없습니다. ID: " + postId));
78+
79+
if (reqBody.categoryId() != null) {
80+
Category category = categoryRepository.findById(reqBody.categoryId())
81+
.orElseThrow(() -> new IllegalArgumentException(
82+
"해당 카테고리를 찾을 수 없습니다. ID: " + reqBody.categoryId()
83+
)
84+
);
85+
post.updateCategory(category);
86+
}
87+
if (reqBody.status() != null) {
88+
post.updateStatus(reqBody.status());
89+
}
90+
if (reqBody.title() != null && !reqBody.title().isBlank()){
91+
post.updateTitle(reqBody.title());
92+
}
93+
if (reqBody.content() != null && !reqBody.content().isBlank()) {
94+
post.updateContent(reqBody.content());
95+
}
96+
if (reqBody.imageUrl() != null && !reqBody.imageUrl().isBlank()) {
97+
post.updateImage(reqBody.imageUrl());
98+
}
99+
if (reqBody.tags() != null) {
100+
// 기존 태그들 삭제
101+
post.clearTags();
102+
103+
// 새로운 태그들 추가
104+
addTag(reqBody.tags(), post);
105+
}
106+
107+
return new PostResponseDto(post);
108+
}
109+
110+
// 태그 추가 메서드
111+
private void addTag(List<String> tagNames, Post post) {
112+
for (String tagName : tagNames) {
113+
Tag tag = tagRepository.findByName(tagName)
114+
.orElseGet(() -> tagRepository.save(
115+
Tag.builder()
116+
.name(tagName)
117+
.build()
118+
)
119+
);
120+
post.addTag(tag);
121+
}
122+
}
82123
}

0 commit comments

Comments
 (0)