Skip to content

Commit

Permalink
Merge pull request #72 from DEPthes/feat/#67-post-edit
Browse files Browse the repository at this point in the history
[FEAT]: 게시글 수정 API 구현
  • Loading branch information
HuniHuns authored Aug 14, 2024
2 parents 7339e37 + 61d2c8a commit ac7c347
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findByEmail(String email);

boolean existsByEmail(String email);

Optional<Member> findById(Long id);
}
67 changes: 67 additions & 0 deletions src/main/java/mvp/deplog/domain/post/application/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,71 @@ public SuccessResponse<List<TempListRes>> getAllTempPosts(Long memberId) {

return SuccessResponse.of(TempListRes);
}

@Transactional
public SuccessResponse<CreatePostRes> modifyPost(Long memberId, Long postId, CreatePostReq createPostReq) {
validateTagName(createPostReq.getTagNameList());

Post post = postRepository.findById(postId)
.orElseThrow(() -> new ResourceNotFoundException("해당 id의 게시글을 찾을 수 없습니다: " + postId));
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException("해당 id의 멤버를 찾을 수 없습니다: " + memberId));

if (!post.getMember().equals(member)) {
throw new UnauthorizedException("본인이 작성한 게시글이 아니므로 수정할 수 없습니다.");
}

String title = post.getTitle();
String content = post.getContent();
String previewContent = post.getPreviewContent();
String previewImage = post.getPreviewImage();

// 수정 전 게시글 내 이미지 url 추출
List<String> oldImageUrls = MarkdownUtil.extractImageLinks(content);

if(createPostReq.getTitle() != null) {
title = createPostReq.getTitle();
}
if(createPostReq.getContent() != null) {
content = createPostReq.getContent();
previewContent = MarkdownUtil.extractPreviewContent(content);
previewImage = MarkdownUtil.extractPreviewImage(content);
}

post.updatePost(title, content, previewContent, previewImage, post.getStage());

// 수정 후 게시글 내 이미지 url 추출
List<String> newImageUrls = MarkdownUtil.extractImageLinks(post.getContent());

// 기존 이미지 url이 수정된 게시글에 있는지 확인
for(String oldImageUrl : oldImageUrls) {
if(!newImageUrls.contains(oldImageUrl)) {
fileService.deleteFile(oldImageUrl, DIRNAME);
}
}

if(createPostReq.getTagNameList() != null && !createPostReq.getTagNameList().isEmpty()) {
taggingRepository.deleteByPost(post);

for (String tagName : createPostReq.getTagNameList()) {
if(tagName != null) {
Tag tag = tagRepository.findByName(tagName)
.orElseGet(() -> tagRepository.save(Tag.builder().name(tagName).build()));

Tagging tagging = Tagging.builder()
.post(post)
.tag(tag)
.build();

taggingRepository.save(tagging);
}
}
}

CreatePostRes createPostRes = CreatePostRes.builder()
.postId(postId)
.build();

return SuccessResponse.of(createPostRes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface PostRepository extends JpaRepository<Post, Long> {

Optional<Post> findById(Long id);

@Query("SELECT p FROM Post p WHERE p.member.part IN :parts")
Page<Post> findByMemberPart(@Param("parts") List<Part> partGroup, Pageable pageable);

Expand Down
24 changes: 21 additions & 3 deletions src/main/java/mvp/deplog/domain/post/presentation/PostApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,18 @@ ResponseEntity<SuccessResponse<CreatePostRes>> createTempPost(
content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))}
)
})
@PutMapping("/publishing")
@PutMapping("/publishing/{postId}")
ResponseEntity<SuccessResponse<CreatePostRes>> publishTempPost(
@Parameter(description = "Access Token을 입력하세요.", required = true) @AuthenticationPrincipal UserDetailsImpl userDetails,
@Parameter(description = "발행할 임시 저장 게시글의 아이디를 입력하세요.", required = true) @RequestParam(value = "postId") Long postId,
@Parameter(description = "발행할 임시 저장 게시글의 아이디를 입력하세요.", required = true) @PathVariable(value = "postId") Long postId,
@Parameter(description = "Schemas의 CreatePostReq를 참고해주세요.", required = true) @RequestBody CreatePostReq createPostReq
);

@Operation(summary = "임시 저장 게시글 목록 조회 API", description = "해당 아이디의 임시 저장 게시글 목록을 조회합니다.")
@ApiResponses(value = {
@ApiResponse(
responseCode = "200", description = "임시 저장 게시글 목록 조회 성공",
content = {@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = TempListRes.class)))}
content = {@Content(mediaType = "application/json",array = @ArraySchema(schema = @Schema(implementation = TempListRes.class)))}
),
@ApiResponse(
responseCode = "400", description = "임시 저장 게시글 목록 조회 실패",
Expand All @@ -239,4 +239,22 @@ ResponseEntity<SuccessResponse<CreatePostRes>> publishTempPost(
ResponseEntity<SuccessResponse<List<TempListRes>>> getAllTempPosts(
@Parameter(description = "Access Token을 입력하세요.", required = true) @AuthenticationPrincipal UserDetailsImpl userDetails
);

@Operation(summary = "게시글 수정 API", description = "해당 아이디의 게시글을 수정합니다.")
@ApiResponses(value = {
@ApiResponse(
responseCode = "200", description = "게시글 수정 성공",
content = {@Content(mediaType = "application/json", schema = @Schema(implementation = CreatePostRes.class))}
),
@ApiResponse(
responseCode = "400", description = "게시글 수정 실패",
content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))}
)
})
@PutMapping("/edits/{postId}")
ResponseEntity<SuccessResponse<CreatePostRes>> modifyPosts(
@Parameter(description = "Access Token을 입력하세요.", required = true) @AuthenticationPrincipal UserDetailsImpl userDetails,
@Parameter(description = "수정할 게시글의 아이디를 입력하세요.", required = true) @PathVariable(value = "postId") Long postId,
@Parameter(description = "Schemas의 CreatePostReq를 참고해주세요.", required = true) @RequestBody CreatePostReq createPostReq
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public ResponseEntity<SuccessResponse<CreatePostRes>> createTempPost(@Authentica
}

@Override
@PutMapping("/publishing")
@PutMapping("/publishing/{postId}")
public ResponseEntity<SuccessResponse<CreatePostRes>> publishTempPost(@AuthenticationPrincipal UserDetailsImpl userDetails,
@RequestParam(value = "postId") Long postId,
@PathVariable(value = "postId") Long postId,
@RequestBody CreatePostReq createPostReq) {
return ResponseEntity.ok(postService.publishTempPost(userDetails.getMember().getId(), postId, createPostReq));
}
Expand All @@ -111,4 +111,12 @@ public ResponseEntity<SuccessResponse<CreatePostRes>> publishTempPost(@Authentic
public ResponseEntity<SuccessResponse<List<TempListRes>>> getAllTempPosts(@AuthenticationPrincipal UserDetailsImpl userDetails){
return ResponseEntity.ok(postService.getAllTempPosts(userDetails.getMember().getId()));
}

@Override
@PutMapping("/edits/{postId}")
public ResponseEntity<SuccessResponse<CreatePostRes>> modifyPosts(@AuthenticationPrincipal UserDetailsImpl userDetails,
@PathVariable(value = "postId") Long postId,
@RequestBody CreatePostReq createPostReq) {
return ResponseEntity.ok(postService.modifyPost(userDetails.getMember().getId(), postId, createPostReq));
}
}

0 comments on commit ac7c347

Please sign in to comment.