Skip to content

Commit

Permalink
Merge pull request #38 from Domitory-CheckMate/feature/37-post
Browse files Browse the repository at this point in the history
[feat] 게시글 수정 및 상태 변경
  • Loading branch information
ziiyouth authored Jan 12, 2024
2 parents 42be45f + a3f5dee commit 434c2aa
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,13 @@ public static PostCheckList createPostCheckList(CheckListRequestDto checkListReq
post.setPostCheckList(checkList);
return checkList;
}
}

public void updatePostCheckList(CheckListRequestDto checkListRequestDto) {
this.cleanType = checkListRequestDto.cleanType();
this.drinkType = checkListRequestDto.drinkType();
this.homeType = checkListRequestDto.homeType();
this.lifePatterType = checkListRequestDto.lifePatterType();
this.noiseType = checkListRequestDto.noiseType();
this.sleepType = checkListRequestDto.sleepType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.gachon.checkmate.domain.post.dto.request.PostCreateRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostUpdateRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostStateUpdateRequestDto;
import org.gachon.checkmate.domain.post.dto.response.PostDetailResponseDto;
import org.gachon.checkmate.domain.post.dto.response.PostSearchResponseDto;
import org.gachon.checkmate.domain.post.dto.response.PostStateUpdateResponseDto;
import org.gachon.checkmate.domain.post.dto.response.PostUpdateResponseDto;
import org.gachon.checkmate.domain.post.service.PostService;
import org.gachon.checkmate.global.common.SuccessResponse;
import org.gachon.checkmate.global.config.auth.UserId;
Expand Down Expand Up @@ -50,6 +54,22 @@ public ResponseEntity<SuccessResponse<?>> getMyPosts(@UserId final Long userId,
return SuccessResponse.ok(responseDto);
}

@PatchMapping("{id}")
public ResponseEntity<SuccessResponse<?>> updateMyPost(@UserId final Long userId,
@PathVariable("id") final Long postId,
@RequestBody @Valid final PostUpdateRequestDto requestDto) {
PostUpdateResponseDto responseDto = postService.updateMyPost(userId, postId, requestDto);
return SuccessResponse.ok(responseDto);
}

@PatchMapping("/state/{id}")
public ResponseEntity<SuccessResponse<?>> updatePostState(@UserId final Long userId,
@PathVariable("id") final Long postId,
@RequestBody @Valid final PostStateUpdateRequestDto requestDto) {
PostStateUpdateResponseDto responseDto = postService.updateMyPostState(userId, postId, requestDto);
return SuccessResponse.ok(responseDto);
}

@PostMapping
public ResponseEntity<SuccessResponse<?>> createPost(@UserId final Long userId,
@RequestBody @Valid final PostCreateRequestDto requestDto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.gachon.checkmate.domain.post.dto.request;

import jakarta.validation.constraints.NotNull;
import org.gachon.checkmate.domain.post.entity.PostState;

public record PostStateUpdateRequestDto(
@NotNull(message = "게시글 상태를 입력해주세요") PostState postState
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.gachon.checkmate.domain.post.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.gachon.checkmate.domain.checkList.dto.request.CheckListRequestDto;
import org.gachon.checkmate.domain.post.entity.ImportantKeyType;
import org.gachon.checkmate.domain.post.entity.RoomType;
import org.gachon.checkmate.domain.post.entity.SimilarityKeyType;

import java.time.LocalDate;

public record PostUpdateRequestDto(
@NotBlank(message = "제목을 입력해주세요") String title,
@NotBlank(message = "내용을 입력해주세요") String content,
@NotNull(message = "중요 키워드를 입력해주세요") ImportantKeyType importantKey,
@NotNull(message = "유사도를 입력해주세요") SimilarityKeyType similarityKey,
@NotNull(message = "기숙사 유형을 입력해주세요") RoomType roomType,
@NotNull(message = "모집 마감기간을 입력해주세요") LocalDate endDate,
@NotNull(message = "체크리스트를 입력해주세요") CheckListRequestDto checkList
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.gachon.checkmate.domain.post.dto.response;

import lombok.Builder;
import org.gachon.checkmate.domain.post.entity.Post;

@Builder
public record PostStateUpdateResponseDto (
Long postId,
String postState
) {
public static PostStateUpdateResponseDto of(Post post) {
return PostStateUpdateResponseDto.builder()
.postId(post.getId())
.postState(post.getPostState().getDesc())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.gachon.checkmate.domain.post.dto.response;

import lombok.Builder;
import org.gachon.checkmate.domain.checkList.dto.response.CheckListResponseDto;
import org.gachon.checkmate.domain.post.entity.Post;

import java.time.LocalDate;

@Builder
public record PostUpdateResponseDto(
Long postId,
String title,
String content,
String importantKey,
String similarityKey,
String roomType,
LocalDate endDate,
CheckListResponseDto checkList
) {
public static PostUpdateResponseDto of(Post post, CheckListResponseDto checkListResponseDto) {
return PostUpdateResponseDto.builder()
.postId(post.getId())
.title(post.getTitle())
.content(post.getContent())
.importantKey(post.getImportantKeyType().getDesc())
.similarityKey(post.getSimilarityKeyType().getDesc())
.roomType(post.getRoomType().getDesc())
.checkList(checkListResponseDto)
.build();
}
}

17 changes: 17 additions & 0 deletions src/main/java/org/gachon/checkmate/domain/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.gachon.checkmate.domain.post.converter.RoomTypeConverter;
import org.gachon.checkmate.domain.post.converter.SimilarityKeyTypeConverter;
import org.gachon.checkmate.domain.post.dto.request.PostCreateRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostStateUpdateRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostUpdateRequestDto;
import org.gachon.checkmate.domain.scrap.entity.Scrap;
import org.gachon.checkmate.global.common.BaseTimeEntity;

Expand Down Expand Up @@ -51,6 +53,7 @@ public static Post createPost(PostCreateRequestDto postCreateRequestDto, User us
.title(postCreateRequestDto.title())
.content(postCreateRequestDto.content())
.endDate(postCreateRequestDto.endDate())
.postState(PostState.RECRUITING)
.roomType(postCreateRequestDto.roomType())
.importantKeyType(postCreateRequestDto.importantKey())
.similarityKeyType(postCreateRequestDto.similarityKey())
Expand All @@ -67,4 +70,18 @@ public void setPostCheckList(PostCheckList postCheckList) {
public void addScrap(Scrap scrap) {
this.scrapList.add(scrap);
}

public void updatePost(PostUpdateRequestDto postUpdateRequestDto) {
this.title = postUpdateRequestDto.title();
this.content = postUpdateRequestDto.content();
this.importantKeyType = postUpdateRequestDto.importantKey();
this.similarityKeyType = postUpdateRequestDto.similarityKey();
this.roomType = postUpdateRequestDto.roomType();
this.endDate = postUpdateRequestDto.endDate();
this.postCheckList.updatePostCheckList(postUpdateRequestDto.checkList());
}

public void updatePostState(PostStateUpdateRequestDto postStateUpdateRequestDto) {
this.postState = postStateUpdateRequestDto.postState();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import org.gachon.checkmate.domain.member.entity.User;
import org.gachon.checkmate.domain.member.repository.UserRepository;
import org.gachon.checkmate.domain.post.dto.request.PostCreateRequestDto;
import org.gachon.checkmate.domain.post.dto.response.PostDetailResponseDto;
import org.gachon.checkmate.domain.post.dto.response.PostSearchElementResponseDto;
import org.gachon.checkmate.domain.post.dto.response.PostSearchResponseDto;
import org.gachon.checkmate.domain.post.dto.request.PostUpdateRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostStateUpdateRequestDto;
import org.gachon.checkmate.domain.post.dto.response.*;
import org.gachon.checkmate.domain.post.dto.support.PostDetailDto;
import org.gachon.checkmate.domain.post.dto.support.PostPagingSearchCondition;
import org.gachon.checkmate.domain.post.dto.support.PostSearchCondition;
Expand All @@ -22,6 +22,7 @@
import org.gachon.checkmate.domain.post.repository.PostRepository;
import org.gachon.checkmate.domain.scrap.repository.ScrapRepository;
import org.gachon.checkmate.global.error.exception.EntityNotFoundException;
import org.gachon.checkmate.global.error.exception.ForbiddenException;
import org.gachon.checkmate.global.error.exception.InvalidValueException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -90,6 +91,30 @@ public PostSearchResponseDto searchTextPost(Long userId, String text, Pageable p
return PostSearchResponseDto.of(searchResults, postSearchList.getTotalPages(), postSearchList.getTotalElements());
}

public PostUpdateResponseDto updateMyPost(Long userId, Long postId, PostUpdateRequestDto requestDto) {
User user = getUserOrThrow(userId);
Post post = getPostOrThrow(postId);
validatePostWriter(user, post);
validateAvailableEndDate(requestDto.endDate());
post.updatePost(requestDto);
CheckListResponseDto checkListResponseDto = createCheckListResponseDto(post.getPostCheckList());
return PostUpdateResponseDto.of(post, checkListResponseDto);
}

public PostStateUpdateResponseDto updateMyPostState(Long userId, Long postId, PostStateUpdateRequestDto requestDto) {
User user = getUserOrThrow(userId);
Post post = getPostOrThrow(postId);
validatePostWriter(user, post);
post.updatePostState(requestDto);
return PostStateUpdateResponseDto.of(post);
}

private void validatePostWriter(User user, Post post) {
if(!post.getUser().getId().equals(user.getId())) {
throw new ForbiddenException(NOT_POST_WRITER);
}
}

private List<PostSearchElementResponseDto> createPostSearchResponseDto(Page<PostSearchDto> postSearchDtoList, CheckList checkList) {
return postSearchDtoList.stream()
.map(postSearchDto ->
Expand Down Expand Up @@ -193,4 +218,9 @@ private User getUserOrThrow(Long userId) {
private boolean existPostInScrap(Long postId, Long userId) {
return scrapRepository.existsByPostIdAndUserId(postId, userId);
}

private Post getPostOrThrow(Long postId) {
return postRepository.findById(postId)
.orElseThrow(() -> new EntityNotFoundException(POST_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public enum ErrorCode {
* 403 Forbidden
*/
FORBIDDEN(HttpStatus.FORBIDDEN, "리소스 접근 권한이 없습니다."),
NOT_POST_WRITER(HttpStatus.FORBIDDEN, "게시물을 수정할 권한이 없습니다. 작성자만이 게시물을 수정할 수 있습니다."),

/**
* 404 Not Found
Expand Down

0 comments on commit 434c2aa

Please sign in to comment.