Skip to content

Commit

Permalink
Merge pull request #138 from sharemindteam/feature/136-post-update
Browse files Browse the repository at this point in the history
feat: 일대다 상담 질문 저장 구현
  • Loading branch information
letskuku authored Mar 11, 2024
2 parents ae2fac5 + 893b672 commit e5a3f07
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.sharemind.post.domain.Post;
import com.example.sharemind.post.dto.request.PostCreateRequest;
import com.example.sharemind.post.dto.request.PostUpdateRequest;
import java.util.List;

public interface PostService {
Expand All @@ -11,4 +12,6 @@ public interface PostService {
List<Post> getUnpaidPrivatePosts();

Post getPostByPostId(Long postId);

void updatePost(PostUpdateRequest postUpdateRequest, Long customerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.example.sharemind.customer.application.CustomerService;
import com.example.sharemind.customer.domain.Customer;
import com.example.sharemind.global.content.ConsultCategory;
import com.example.sharemind.post.domain.Post;
import com.example.sharemind.post.dto.request.PostCreateRequest;
import com.example.sharemind.post.dto.request.PostUpdateRequest;
import com.example.sharemind.post.exception.PostErrorCode;
import com.example.sharemind.post.exception.PostException;
import com.example.sharemind.post.repository.PostRepository;
Expand Down Expand Up @@ -38,4 +40,16 @@ public Post getPostByPostId(Long postId) {
return postRepository.findByPostIdAndIsActivatedIsTrue(postId).orElseThrow(
() -> new PostException(PostErrorCode.POST_NOT_FOUND, postId.toString()));
}

@Transactional
@Override
public void updatePost(PostUpdateRequest postUpdateRequest, Long customerId) {
Customer customer = customerService.getCustomerByCustomerId(customerId);
Post post = getPostByPostId(postUpdateRequest.getPostId());
ConsultCategory consultCategory = ConsultCategory.getConsultCategoryByName(
postUpdateRequest.getConsultCategory());

post.updatePost(consultCategory, postUpdateRequest.getTitle(),
postUpdateRequest.getContent(), postUpdateRequest.getIsCompleted(), customer);
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/example/sharemind/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.example.sharemind.global.common.BaseEntity;
import com.example.sharemind.global.content.ConsultCategory;
import com.example.sharemind.post.content.PostStatus;
import com.example.sharemind.post.exception.PostErrorCode;
import com.example.sharemind.post.exception.PostException;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
Expand Down Expand Up @@ -62,6 +64,9 @@ public class Post extends BaseEntity {
@Column(name = "is_paid", nullable = false)
private Boolean isPaid;

@Column(name = "is_completed")
private Boolean isCompleted;

@Builder
public Post(Customer customer, Long cost, Boolean isPublic) {
this.customer = customer;
Expand All @@ -77,11 +82,38 @@ public void updateIsPaid() {
this.isPaid = true;
}

public void updatePost(ConsultCategory consultCategory, String title, String content,
Boolean isCompleted, Customer customer) {
checkUpdatability();
checkWriteAuthority(customer);

this.consultCategory = consultCategory;
this.title = title;
this.content = content;
this.isCompleted = isCompleted;

if (isCompleted) {
this.postStatus = PostStatus.PROCEEDING;
}
}

private void setIsPaid(Boolean isPublic) {
if (isPublic) {
this.isPaid = true;
} else {
this.isPaid = false;
}
}

private void checkWriteAuthority(Customer customer) {
if (!customer.getCustomerId().equals(this.customer.getCustomerId())) {
throw new PostException(PostErrorCode.POST_MODIFY_DENIED);
}
}

private void checkUpdatability() {
if (this.isCompleted.equals(true)) {
throw new PostException(PostErrorCode.POST_ALREADY_COMPLETED);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.sharemind.post.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;

@Getter
public class PostUpdateRequest {

@Schema(description = "일대다 상담 아이디")
@NotNull(message = "일대다 상담 아이디는 공백일 수 없습니다.")
private Long postId;

@Schema(description = "선택한 상담 카테고리", example = "BOREDOM")
@NotBlank(message = "상담 카테고리는 공백일 수 없습니다.")
private String consultCategory;

@Schema(description = "상담 제목", example = "남자친구의 심리가 궁금해요")
@NotBlank(message = "상담 제목은 공백일 수 없습니다.")
private String title;

@Schema(description = "상담 내용", example = "안녕하세요 어쩌구저쩌구~")
@NotBlank(message = "상담 내용은 공백일 수 없습니다.")
@Size(max = 1000, message = "상담 내용은 최대 1000자입니다.")
private String content;

@Schema(description = "최종 저장 여부", example = "최종 저장이면 true, 임시 저장이면 false")
@NotNull(message = "최종 저장 여부는 공백일 수 없습니다.")
private Boolean isCompleted;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
public enum PostErrorCode {

POST_NOT_FOUND(HttpStatus.NOT_FOUND, "일대다 상담이 존재하지 않습니다."),
POST_ALREADY_PAID(HttpStatus.BAD_REQUEST, "이미 결제 완료된 일대다 상담입니다.");
POST_ALREADY_PAID(HttpStatus.BAD_REQUEST, "이미 결제 완료된 일대다 상담입니다."),
POST_MODIFY_DENIED(HttpStatus.FORBIDDEN, "일대다 상담 질문 작성 권한이 없습니다."),
POST_ALREADY_COMPLETED(HttpStatus.BAD_REQUEST, "이미 일대다 상담 질문이 최종 제출되었습니다.");

private final HttpStatus httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public class PostException extends RuntimeException {

private final PostErrorCode errorCode;

public PostException(PostErrorCode errorCode) {
super(errorCode.getMessage());
this.errorCode = errorCode;
}

public PostException(PostErrorCode errorCode, String message) {
super(errorCode.getMessage() + " : " + message);
this.errorCode = errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.sharemind.global.jwt.CustomUserDetails;
import com.example.sharemind.post.application.PostService;
import com.example.sharemind.post.dto.request.PostCreateRequest;
import com.example.sharemind.post.dto.request.PostUpdateRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -15,6 +16,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -47,4 +49,28 @@ public ResponseEntity<Void> createPost(@Valid @RequestBody PostCreateRequest pos
postService.createPost(postCreateRequest, customUserDetails.getCustomer().getCustomerId());
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@Operation(summary = "일대다 상담 질문 내용 작성", description = "일대다 상담 질문 내용 작성")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "작성 성공"),
@ApiResponse(responseCode = "400",
description = "1. 요청 값 중 공백이 존재\n 2. 이미 최종 제출된 상담",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = CustomExceptionResponse.class))
),
@ApiResponse(responseCode = "403", description = "작성 권한이 없는 상담",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = CustomExceptionResponse.class))
),
@ApiResponse(responseCode = "404", description = "1. 존재하지 않는 일대다 상담\n 2. 존재하지 않는 상담 카테고리",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = CustomExceptionResponse.class))
)
})
@PatchMapping
public ResponseEntity<Void> updatePost(@Valid @RequestBody PostUpdateRequest postUpdateRequest,
@AuthenticationPrincipal CustomUserDetails customUserDetails) {
postService.updatePost(postUpdateRequest, customUserDetails.getCustomer().getCustomerId());
return ResponseEntity.ok().build();
}
}

0 comments on commit e5a3f07

Please sign in to comment.