Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,13 @@ public static CommunityResponseDTO.DeletedPostDTO toDeletedPostDTO(Community com
.post_id(community.getId())
.build();
}

public static CommunityResponseDTO.ModifyPostDTO toModifyPostDTO(Community community, Boolean isOwner){
return CommunityResponseDTO.ModifyPostDTO.builder()
.title(community.getTitle())
.content(community.getContent())
.communityWriterEmail(community.getUser().getEmail())
.created_at(community.getCreatedAt())
.isOwner(isOwner)
.build();
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/example/helloworldmvc/domain/Community.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,11 @@ public void setUser(User user) {
user.getCommunityList().add(this);
}

public void setTitle(String title) {
this.title = title;
}
public void setContent(String content) {
this.content = content;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public interface CommunityService {
CommunityResponseDTO.PostListDTO getCommunityList(String userId, Long categoryId, Integer page, Integer size);
CommunityResponseDTO.PostDetailDTO getCommunityDetail(String userId, Long communityId, Integer page, Integer size);
CommunityResponseDTO.DeletedPostDTO deleteCommunityPost(String userId, Long categoryId, Long communityId);
CommunityResponseDTO.ModifyPostDTO modifyCommunityPost(String userId, Long communityId, CommunityRequestDTO.ModifyPostDTO modifyPostDTO);

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,25 @@ public CommunityResponseDTO.DeletedPostDTO deleteCommunityPost(String userId, Lo
communityRepository.deleteById(community.getId());
return CommunityConverter.toDeletedPostDTO(community);
}

@Override
public CommunityResponseDTO.ModifyPostDTO modifyCommunityPost(String userId, Long communityId, CommunityRequestDTO.ModifyPostDTO modifyPostDTO) {
User user = userRepository.findByEmail(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
Community community = communityRepository.findById(communityId).orElseThrow(() -> new GeneralException(ErrorStatus.COMMUNITY_POST_NOT_FOUND));
if(!community.getUser().getId().equals(user.getId())){
throw new GeneralException(ErrorStatus.COMMUNITY_NOT_OWNER);
}
if(!modifyPostDTO.getTitle().isEmpty()){
community.setTitle(modifyPostDTO.getTitle());
}
if(!modifyPostDTO.getContent().isEmpty()){
community.setContent(modifyPostDTO.getContent());
}
boolean isOwner = false;
if(community.getUser().getEmail().equals(user.getEmail())) {
isOwner = true;
}
communityRepository.save(community);
return CommunityConverter.toModifyPostDTO(community, isOwner);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,22 @@ public ApiResponse<CommunityResponseDTO.DeletedPostDTO> deleteCommunity(@Request
String gmail = jwtTokenProvider.getGoogleEmail(accessToken);
return ApiResponse.onSuccess(communityService.deleteCommunityPost(gmail, categoryId, communityId));
}

@PatchMapping(value = "/{category_id}/{community_id}/modify")
@Operation(summary = "커뮤니티 글 수정 API", description = "커뮤니티 게시판에 글을 수정하는 API입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER4001", description = "사용자를 찾을수 없습니다.")
})
@Parameters({
@Parameter(name = "Authorization", description = "RequestHeader - 로그인한 사용자 토큰"),
@Parameter(name = "community_id", description = "PathVariable - 게시글 아이디"),
})
public ApiResponse<CommunityResponseDTO.ModifyPostDTO> modifyCommunity(@RequestHeader(name = "Authorization") String accessToken,
@PathVariable(name = "community_id") Long communityId,
@RequestBody @Valid CommunityRequestDTO.ModifyPostDTO modifyPostDTO
) {
String gmail = jwtTokenProvider.getGoogleEmail(accessToken);
return ApiResponse.onSuccess(communityService.modifyCommunityPost(gmail, communityId, modifyPostDTO));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ public static class CreatePostDTO {

List<MultipartFile> images;
}

@Getter
@Setter
public static class ModifyPostDTO {
@NotBlank
String title;

@NotBlank
String content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,16 @@ public static class DeletedPostDTO{
String categoryName;
Long post_id;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class ModifyPostDTO{
String title;
String content;
String communityWriterEmail;
LocalDateTime created_at;
Boolean isOwner;
}
}
Loading