Skip to content

Commit

Permalink
feat: 게시물 수정 및 삭제 기능 구현 (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
da-nyee committed Jul 30, 2021
1 parent 51a1a1d commit d9edfa6
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 10 deletions.
Expand Up @@ -4,8 +4,8 @@

public class PostFormatException extends PostException {

private static final String CODE = "F0001";
private static final String MESSAGE = "게시물 포맷 에러";
private static final String CODE = "F0004";
private static final String MESSAGE = "게시물 길이 에러";

public PostFormatException() {
super(CODE, HttpStatus.BAD_REQUEST, MESSAGE);
Expand Down
Expand Up @@ -7,11 +7,14 @@
import com.woowacourse.pickgit.exception.post.PostNotFoundException;
import com.woowacourse.pickgit.exception.user.UserNotFoundException;
import com.woowacourse.pickgit.post.application.dto.CommentResponse;
import com.woowacourse.pickgit.post.application.dto.request.PostDeleteRequestDto;
import com.woowacourse.pickgit.post.application.dto.request.PostRequestDto;
import com.woowacourse.pickgit.post.application.dto.request.PostUpdateRequestDto;
import com.woowacourse.pickgit.post.application.dto.request.RepositoryRequestDto;
import com.woowacourse.pickgit.post.application.dto.response.LikeResponseDto;
import com.woowacourse.pickgit.post.application.dto.response.PostImageUrlResponseDto;
import com.woowacourse.pickgit.post.application.dto.response.PostResponseDto;
import com.woowacourse.pickgit.post.application.dto.response.PostUpdateResponseDto;
import com.woowacourse.pickgit.post.application.dto.response.RepositoriesResponseDto;
import com.woowacourse.pickgit.post.domain.PickGitStorage;
import com.woowacourse.pickgit.post.domain.PlatformRepositoryExtractor;
Expand Down Expand Up @@ -210,4 +213,28 @@ private Post findPostById(Long id) {
"해당하는 게시물을 찾을 수 없습니다.")
);
}

public PostUpdateResponseDto update(PostUpdateRequestDto updateRequestDto) {
Post post = postRepository.findById(updateRequestDto.getPostId())
.orElseThrow(() -> new PostNotFoundException(
"P0002",
HttpStatus.INTERNAL_SERVER_ERROR,
"해당하는 게시물을 찾을 수 없습니다."
));

post.update(updateRequestDto.getContent(), updateRequestDto.getTags());

return PostUpdateResponseDto.toPostUpdateResponseDto(updateRequestDto);
}

public void delete(PostDeleteRequestDto deleteRequestDto) {
Post post = postRepository.findById(deleteRequestDto.getPostId())
.orElseThrow(() -> new PostNotFoundException(
"P0002",
HttpStatus.INTERNAL_SERVER_ERROR,
"해당하는 게시물을 찾을 수 없습니다."
));

postRepository.delete(post);
}
}
@@ -0,0 +1,26 @@
package com.woowacourse.pickgit.post.application.dto.request;

import lombok.Builder;

@Builder
public class PostDeleteRequestDto {

private Long postId;

private PostDeleteRequestDto() {
}

public static PostDeleteRequestDto toPostDeleteRequestDto(Long postId) {
return PostDeleteRequestDto.builder()
.postId(postId)
.build();
}

public PostDeleteRequestDto(Long postId) {
this.postId = postId;
}

public Long getPostId() {
return postId;
}
}
@@ -0,0 +1,45 @@
package com.woowacourse.pickgit.post.application.dto.request;

import com.woowacourse.pickgit.post.presentation.dto.request.PostUpdateRequest;
import java.util.List;
import lombok.Builder;

@Builder
public class PostUpdateRequestDto {

private Long postId;
private List<String> tags;
private String content;

private PostUpdateRequestDto() {
}

public PostUpdateRequestDto(Long postId, List<String> tags, String content) {
this.postId = postId;
this.tags = tags;
this.content = content;
}

public static PostUpdateRequestDto toUpdateRequestDto(
Long postId,
PostUpdateRequest updateRequest
) {
return PostUpdateRequestDto.builder()
.postId(postId)
.tags(updateRequest.getTags())
.content(updateRequest.getContent())
.build();
}

public Long getPostId() {
return postId;
}

public List<String> getTags() {
return tags;
}

public String getContent() {
return content;
}
}
@@ -0,0 +1,37 @@
package com.woowacourse.pickgit.post.application.dto.response;

import com.woowacourse.pickgit.post.application.dto.request.PostUpdateRequestDto;
import java.util.List;
import lombok.Builder;

@Builder
public class PostUpdateResponseDto {

private List<String> tags;
private String content;

private PostUpdateResponseDto() {
}

public PostUpdateResponseDto(List<String> tags, String content) {
this.tags = tags;
this.content = content;
}

public static PostUpdateResponseDto toPostUpdateResponseDto(
PostUpdateRequestDto updateRequestDto)
{
return PostUpdateResponseDto.builder()
.content(updateRequestDto.getContent())
.tags(updateRequestDto.getTags())
.build();
}

public List<String> getTags() {
return tags;
}

public String getContent() {
return content;
}
}
Expand Up @@ -111,10 +111,10 @@ public void addTags(List<Tag> tags) {
}

private void validateDuplicateTag(List<Tag> tags) {
Set<String> nonDuplicatetagNames = tags.stream()
Set<String> nonDuplicateTagNames = tags.stream()
.map(Tag::getName)
.collect(toSet());
if (nonDuplicatetagNames.size() != tags.size()) {
if (nonDuplicateTagNames.size() != tags.size()) {
throw new CannotAddTagException();
}
}
Expand Down Expand Up @@ -149,6 +149,19 @@ public Long getId() {
return id;
}

public void update(String content, List<String> tags) {
postTags.clear();

this.content = new PostContent(content);
addTags(convertToTag(tags));
}

private List<Tag> convertToTag(List<String> tags) {
return tags.stream()
.map(Tag::new)
.collect(toList());
}

public List<String> getImageUrls() {
return images.getUrls();
}
Expand Down
Expand Up @@ -8,6 +8,7 @@
public class PostContent {

public static final int MAXIMUM_CONTENT_LENGTH = 500;

@Lob
private String content;

Expand Down
Expand Up @@ -5,23 +5,26 @@
import com.woowacourse.pickgit.exception.authentication.UnauthorizedException;
import com.woowacourse.pickgit.post.application.PostService;
import com.woowacourse.pickgit.post.application.dto.CommentResponse;
import com.woowacourse.pickgit.post.application.dto.response.LikeResponseDto;
import com.woowacourse.pickgit.post.application.dto.response.PostResponseDto;
import com.woowacourse.pickgit.post.application.dto.request.PostDeleteRequestDto;
import com.woowacourse.pickgit.post.application.dto.request.PostRequestDto;
import com.woowacourse.pickgit.post.application.dto.request.PostUpdateRequestDto;
import com.woowacourse.pickgit.post.application.dto.request.RepositoryRequestDto;
import com.woowacourse.pickgit.post.application.dto.response.LikeResponseDto;
import com.woowacourse.pickgit.post.application.dto.response.PostImageUrlResponseDto;
import com.woowacourse.pickgit.post.application.dto.response.PostResponseDto;
import com.woowacourse.pickgit.post.application.dto.response.PostUpdateResponseDto;
import com.woowacourse.pickgit.post.application.dto.response.RepositoriesResponseDto;
import com.woowacourse.pickgit.post.domain.dto.RepositoryResponseDto;
import com.woowacourse.pickgit.post.presentation.dto.request.CommentRequest;
import com.woowacourse.pickgit.post.presentation.dto.request.ContentRequest;
import com.woowacourse.pickgit.post.presentation.dto.request.HomeFeedRequest;
import com.woowacourse.pickgit.post.presentation.dto.request.PostRequest;

import com.woowacourse.pickgit.post.presentation.dto.request.PostUpdateRequest;
import com.woowacourse.pickgit.post.presentation.dto.response.LikeResponse;
import com.woowacourse.pickgit.post.presentation.dto.response.PostUpdateResponse;
import java.net.URI;
import java.util.List;
import javax.validation.Valid;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand Down Expand Up @@ -93,7 +96,7 @@ public ResponseEntity<Void> write(
.write(createPostRequestDto(user, request));

return ResponseEntity
.created(redirectUrl(user, responseDto))
.created(redirectUrl(user.getUsername(), responseDto.getId()))
.build();
}

Expand Down Expand Up @@ -172,4 +175,34 @@ private void validateIsGuest(AppUser user) {
throw new UnauthorizedException();
}
}

@PutMapping("/posts/{postId}")
public ResponseEntity<PostUpdateResponse> update(
@Authenticated AppUser user,
@PathVariable Long postId,
@Valid @RequestBody PostUpdateRequest updateRequest
) {
PostUpdateResponseDto responseDto =
postService.update(PostUpdateRequestDto.toUpdateRequestDto(postId, updateRequest));

return ResponseEntity
.created(redirectUrl(user.getUsername(), postId))
.body(PostUpdateResponse.toPostUpdateResponse(responseDto));
}

private URI redirectUrl(String username, Long postId) {
return URI.create(String.format(REDIRECT_URL, username, postId));
}

@DeleteMapping("/posts/{postId}")
public ResponseEntity<Void> delete(
@Authenticated AppUser user,
@PathVariable Long postId
) {
postService.delete(PostDeleteRequestDto.toPostDeleteRequestDto(postId));

return ResponseEntity
.noContent()
.build();
}
}
@@ -0,0 +1,32 @@
package com.woowacourse.pickgit.post.presentation.dto.request;

import java.util.List;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import lombok.Builder;

@Builder
public class PostUpdateRequest {

private List<String> tags;

@NotNull(message = "F0001")
@Size(max = 500, message = "F0004")
private String content;

private PostUpdateRequest() {
}

public PostUpdateRequest(List<String> tags, String content) {
this.tags = tags;
this.content = content;
}

public List<String> getTags() {
return tags;
}

public String getContent() {
return content;
}
}
@@ -0,0 +1,35 @@
package com.woowacourse.pickgit.post.presentation.dto.response;

import com.woowacourse.pickgit.post.application.dto.response.PostUpdateResponseDto;
import java.util.List;
import lombok.Builder;

@Builder
public class PostUpdateResponse {

private List<String> tags;
private String content;

private PostUpdateResponse() {
}

public PostUpdateResponse(List<String> tags, String content) {
this.tags = tags;
this.content = content;
}

public static PostUpdateResponse toPostUpdateResponse(PostUpdateResponseDto updateResponseDto) {
return PostUpdateResponse.builder()
.tags(updateResponseDto.getTags())
.content(updateResponseDto.getContent())
.build();
}

public List<String> getTags() {
return tags;
}

public String getContent() {
return content;
}
}
Expand Up @@ -19,6 +19,6 @@ void validate_IsOver500_ThrowsException() {
assertThatThrownBy(() -> new PostContent(content))
.isInstanceOf(PostFormatException.class)
.extracting("errorCode")
.isEqualTo("F0001");
.isEqualTo("F0004");
}
}

0 comments on commit d9edfa6

Please sign in to comment.