Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: 게시글 생성, 수정, 삭제 API 기능 구현 #260

Merged
merged 3 commits into from
Nov 10, 2024
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 @@ -49,10 +49,26 @@ public ApiResponse<PostResponseDTO.CreatePostResultDTO> createPost(@ModelAttribu
}

// 멤버의 게시글 수정하기
@PatchMapping(path = "/{postId}", consumes = "multipart/form-data")
@Operation(summary = "다이어리 수정 API", description = "다이어리를 수정합니다.")
public ApiResponse<PostResponseDTO.UpdatePostResultDTO> updatePost(@ModelAttribute PostRequestDTO.UpdatePostDTO request, @PathVariable Long postId){
Member member = memberCommandService.getRequester();
jwtTokenProvider.isValidToken(member.getMemberId());

return ApiResponse.onSuccess(SuccessStatus.POST_OK, PostConverter.toUpdatePostResultDTO(postCommandService.updatePost(postId, request)));
}


// 게시글 삭제하기
@DeleteMapping("/{postId}")
@Operation(summary = "다이어리 삭제 API", description = "다이어리를 삭제합니다.")
public ApiResponse<?> deletePost(@PathVariable Long postId){
Member member = memberCommandService.getRequester();
jwtTokenProvider.isValidToken(member.getMemberId());

postCommandService.deletePost(postId);
return ApiResponse.onSuccess(SuccessStatus.POST_OK, null);
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ private static PostResponseDTO.SimplePostResponseDTO toSimplePostResponseDto(Pos
public static Post toPost(PostRequestDTO.CreatePostRequestDTO request, TeamRepository teamRepository, ProjectRepository projectRepository) {
Team team = null;
Project project = null;
if (request.teamId() != null) {
team = teamRepository.findById(request.teamId()).orElseThrow(() -> new IllegalArgumentException("Team not found with id: " + request.teamId()));
if (request.getTeamId() != null) {
team = teamRepository.findById(request.getTeamId()).orElseThrow(() -> new IllegalArgumentException("Team not found with id: " + request.getTeamId()));
}
if (request.projectId() != null) {
project = projectRepository.findById(request.projectId()).orElseThrow(() -> new IllegalArgumentException("Project not found with id: " + request.projectId()));
if (request.getProjectId() != null) {
project = projectRepository.findById(request.getProjectId()).orElseThrow(() -> new IllegalArgumentException("Project not found with id: " + request.getProjectId()));
}
return Post.builder()
.postTitle(request.postTitle())
.postBody(request.postBody())
.postTitle(request.getPostTitle())
.postBody(request.getPostBody())
.team(team)
.project(project)
.postStatus(request.postStatus() != null ? request.postStatus() : true) // 기본값 설정
.postAccess(request.postAccess() != null ? request.postAccess() : PostAccess.MEMBER) // 기본값 설정
.postStatus(request.getPostStatus() != null ? request.getPostStatus() : true) // 기본값 설정
.postAccess(request.getPostAccess() != null ? request.getPostAccess() : PostAccess.MEMBER) // 기본값 설정
.build();
}

Expand All @@ -72,6 +72,27 @@ public static PostResponseDTO.CreatePostResultDTO toCreateResultDTO(Post post) {
}


public static PostResponseDTO.UpdatePostResultDTO toUpdatePostResultDTO(Post post) {
return PostResponseDTO.UpdatePostResultDTO.builder()
.postId(post.getPostId())
.memberId(post.getMember().getMemberId())
.teamId(post.getTeam() != null ? post.getTeam().getTeamId() : null)
.projectId(post.getProject() != null ? post.getProject().getProjectId() : null)
.postTitle(post.getPostTitle())
.postBody(post.getPostBody())
.postStatus(post.getPostStatus())
.coauthorIds(post.getAuthorsList().stream()
.map(author -> author.getMember().getMemberId())
.collect(Collectors.toSet()))
.postAccess(post.getPostAccess())
.thumbnailImageUrl((post.getThumbnailImage() != null)
? post.getThumbnailImage().getFileUrl()
: "")
.postFileList(PostFileConverter.toPostFileListDTO(post.getPostFileList()))
.build();
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,53 @@
public class PostRequestDTO {


// @Builder
// @JsonInclude(JsonInclude.Include.NON_NULL)
// @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
// public record CreatePostRequestDTO (
// Long teamId,
// Long projectId,
// String postTitle,
// String postBody,
// Boolean postStatus,
// PostAccess postAccess,
// String thumbnailImageName,
// List<MultipartFile> postFiles
// ) {
// }

@Getter
//@Setter
@Builder
@AllArgsConstructor
//@NoArgsConstructor
public static class CreatePostRequestDTO {
private Long teamId;
private Long projectId;
private String postTitle;
private String postBody;
private Boolean postStatus;
//private Set<String> postCategory;
private PostAccess postAccess;
private String thumbnailImageName;
private List<MultipartFile> postFiles;
}

@Getter
//@Setter
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public record CreatePostRequestDTO (
Long teamId,
Long projectId,
String postTitle,
String postBody,
Boolean postStatus,
PostAccess postAccess,
String thumbnailImageName,
List<MultipartFile> postFiles
) {
@AllArgsConstructor
//@NoArgsConstructor
public static class UpdatePostDTO {
private String postTitle;
private String postBody;
private Boolean postStatus;
//private Set<String> postCategory;
private PostAccess postAccess;
private String thumbnailImageName;
private List<MultipartFile> addedPostFiles;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,42 @@ public record SimplePostResponseDTO(
) {
}


@Getter
@Builder
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record CreatePostResultDTO(
Long postId,
Long memberId,
Long teamId,
Long projectId,
String postTitle,
String postBody,
String thumbnailImageUrl,
Boolean postStatus,
String postCategory,
Set<Long> coauthorIds,
PostAccess postAccess,
PostFileResponseDTO.PostFileListDTO postFileList
){
@AllArgsConstructor
//@NoArgsConstructor
public static class CreatePostResultDTO {
Long postId;
Long memberId;
Long teamId;
Long projectId;
String postTitle;
String postBody;
String thumbnailImageUrl;
Boolean postStatus;
String postCategory;
Set<Long> coauthorIds;
PostAccess postAccess;
PostFileResponseDTO.PostFileListDTO postFileList;
}

@Getter
@Builder
//@NoArgsConstructor
@AllArgsConstructor
public static class UpdatePostResultDTO {
Long postId;
Long memberId;
Long teamId;
Long projectId;
String postTitle;
String postBody;
String thumbnailImageUrl;
Boolean postStatus;
String postCategory;
Set<Long> coauthorIds;
PostAccess postAccess;
PostFileResponseDTO.PostFileListDTO postFileList;
}

}
14 changes: 12 additions & 2 deletions src/main/java/com/codiary/backend/domain/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.codiary.backend.domain.category.entity.Category;
import com.codiary.backend.domain.comment.entity.Comment;
import com.codiary.backend.domain.member.entity.Member;
import com.codiary.backend.domain.post.dto.request.PostRequestDTO;
import com.codiary.backend.global.common.BaseEntity;
import com.codiary.backend.domain.project.entity.Project;
import com.codiary.backend.domain.team.entity.Team;
Expand Down Expand Up @@ -38,10 +39,10 @@ public class Post extends BaseEntity {
@JoinColumn(name = "project_id")
private Project project;

@Column(name = "post_title", columnDefinition = "varchar(500)")
@Column(name = "post_title", nullable = false, columnDefinition = "varchar(500)")
private String postTitle;

@Column(name = "post_body", columnDefinition = "varchar(3000)")
@Column(name = "post_body", nullable = false, columnDefinition = "varchar(3000)")
private String postBody;

@OneToOne
Expand Down Expand Up @@ -86,8 +87,17 @@ public class Post extends BaseEntity {

public void setProject(Project project) { this.project = project;}


public void update(PostRequestDTO.UpdatePostDTO request) {
this.postTitle = request.getPostTitle();
this.postBody = request.getPostBody();
this.postAccess = request.getPostAccess();
this.postStatus = request.getPostStatus();
}

public void setCategories(List<Category> categories) {
this.categoriesList.clear();
this.categoriesList.addAll(categories);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public Post createPost(PostRequestDTO.CreatePostRequestDTO request) {
Post tempPost = postRepository.save(newPost);
tempPost.setPostFileList(new ArrayList<>());

if (request.postFiles() != null) {
for (MultipartFile file : request.postFiles()) {
if (request.getPostFiles() != null) {
for (MultipartFile file : request.getPostFiles()) {
if (file.isEmpty()) {
continue;
}
Expand All @@ -66,7 +66,7 @@ public Post createPost(PostRequestDTO.CreatePostRequestDTO request) {
}

// 대표 사진 설정
String thumbnailImageName = request.thumbnailImageName();
String thumbnailImageName = request.getThumbnailImageName();
for (PostFile postFile : tempPost.getPostFileList()) {
if (postFile.getFileName().equals(thumbnailImageName)) {
tempPost.setThumbnailImage(postFile);
Expand All @@ -79,4 +79,52 @@ public Post createPost(PostRequestDTO.CreatePostRequestDTO request) {
Post savedPost = postRepository.save(tempPost);
return savedPost;
}


public Post updatePost(Long postId, PostRequestDTO.UpdatePostDTO request) {
Member getMember = memberCommandService.getRequester();
Post updatePost = postRepository.findById(postId).get();
updatePost.update(request);

// 새로운 이미지 추가
if (request.getAddedPostFiles() != null) {
for (MultipartFile file : request.getAddedPostFiles()) {
if (file.isEmpty()) {
continue;
}
String uuid = UUID.randomUUID().toString();
Uuid savedUuid = uuidRepository.save(Uuid.builder().uuid(uuid).build());
String fileUrl = s3Manager.uploadFile(s3Manager.generatePostName(savedUuid), file);

PostFile newPostFile = PostFileConverter.toPostFile(fileUrl, updatePost, file.getOriginalFilename());
postFileRepository.save(newPostFile);

updatePost.getPostFileList().add(newPostFile);
}
}

// 대표 사진 설정
String thumbnailImageName = request.getThumbnailImageName();
for (PostFile postFile : updatePost.getPostFileList()) {
if (postFile.getFileName() == thumbnailImageName) {
updatePost.setThumbnailImage(postFile);
}
}
if (updatePost.getPostFileList().size() != 0 && updatePost.getThumbnailImage() == null) {
updatePost.setThumbnailImage(updatePost.getPostFileList().get(0));
}

return postRepository.save(updatePost);
}


public void deletePost(Long postId) {
Member getMember = memberCommandService.getRequester();

Post deletePost = postRepository.findById(postId).get();
postRepository.delete(deletePost);
}



}
Loading