Skip to content

Commit

Permalink
[REFACTOR] 게시글 업데이트 방식 변경(PUT->PATCH), 게시글 추가 필드 선언 (#176)
Browse files Browse the repository at this point in the history
* feat: 게시글 Entity 성별, 나이, 봉사 시간 필드 추가 / postReqDto 필드 추가 / Controller @Valid 추가

* refactor: 필요없는 memberId 필드 제거

* refactor: NotBlank 적용 안되는 필드 NotNull로 변경

* refactor: startTime,endTime -> startDate,endDate로 변경

* refactor: assistanceTime, headcount 필드 추가

* feat: assistanceTime, headcount 필드 추가

* refactor: 게시글 조회시 PostResDto 생성 로직 변경

* feat: 업데이트 방식 PUT -> PATCH 변경, @DynamicUpdate 적용

* style: 가독성을 위한 개행 추가

* feat: assistanceStartTime, assistanceEndTime ValueObject 적용

* refactor: AssistanceTime 적용

* refactor: if 에서 삼항연산자로 변경, assistanceTime 업데이트 로직 변경
  • Loading branch information
injae-348 authored Oct 3, 2024
1 parent 506e4a0 commit 88a9fd7
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import econo.buddybridge.post.dto.PostCustomPage;
import econo.buddybridge.post.dto.PostReqDto;
import econo.buddybridge.post.dto.PostResDto;
import econo.buddybridge.post.dto.PostUpdateReqDto;
import econo.buddybridge.post.entity.AssistanceType;
import econo.buddybridge.post.entity.PostStatus;
import econo.buddybridge.post.entity.PostType;
Expand All @@ -15,19 +16,21 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/posts")
Expand Down Expand Up @@ -73,7 +76,7 @@ public ApiResponse<ApiResponse.CustomBody<PostCustomPage>> getAllPosts(
@Operation(summary = "게시글 생성", description = "게시글을 생성합니다.")
@PostMapping
public ApiResponse<ApiResponse.CustomBody<Long>> createPost(
@RequestBody PostReqDto postReqDto,
@Valid @RequestBody PostReqDto postReqDto,
HttpServletRequest request
) {
Long memberId = SessionUtils.getMemberId(request);
Expand All @@ -96,14 +99,14 @@ public ApiResponse<ApiResponse.CustomBody<PostResDto>> getPost(

// 게시글 업데이트
@Operation(summary = "게시글 수정", description = "게시글을 수정합니다.")
@PutMapping("/{post-id}")
@PatchMapping("/{post-id}")
public ApiResponse<ApiResponse.CustomBody<Long>> updatePost(
@PathVariable("post-id") Long postId,
@RequestBody PostReqDto postReqDto,
@RequestBody PostUpdateReqDto postUpdateReqDto,
HttpServletRequest request
) {
Long memberId = SessionUtils.getMemberId(request);
Long updatedPostId = postService.updatePost(postId, postReqDto, memberId);
Long updatedPostId = postService.updatePost(postId, postUpdateReqDto, memberId);
return ApiResponseGenerator.success(updatedPostId, HttpStatus.OK);
}

Expand Down
58 changes: 51 additions & 7 deletions src/main/java/econo/buddybridge/post/dto/PostReqDto.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,67 @@
package econo.buddybridge.post.dto;

import econo.buddybridge.member.entity.DisabilityType;
import econo.buddybridge.member.entity.Gender;
import econo.buddybridge.member.entity.Member;
import econo.buddybridge.post.entity.AssistanceTime;
import econo.buddybridge.post.entity.AssistanceType;
import econo.buddybridge.post.entity.District;
import econo.buddybridge.post.entity.Post;
import econo.buddybridge.post.entity.PostStatus;
import econo.buddybridge.post.entity.PostType;
import econo.buddybridge.post.entity.Schedule;
import econo.buddybridge.post.entity.ScheduleType;
import java.time.LocalDateTime;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;

import java.time.LocalDateTime;

@Builder
public record PostReqDto(
Long memberId,
@NotBlank(message = "제목을 입력해주세요.")
String title,

@NotNull(message = "도움 종류를 선택해주세요. 교육 or 생활")
AssistanceType assistanceType,
LocalDateTime startTime,
LocalDateTime endTime,

@NotNull(message = "봉사 시작 날짜를 입력해주세요.")
LocalDateTime startDate,

@NotNull(message = "봉사 종료 날짜를 입력해주세요.")
LocalDateTime endDate,

@NotNull(message = "일정 종류를 선택해주세요. 정기 or 비정기")
ScheduleType scheduleType,

@NotBlank(message = "일정 상세를 입력해주세요. 예) 매주 월요일, 화목")
String scheduleDetails,

@NotNull(message = "지역을 선택해주세요.")
District district,

@NotBlank(message = "상세 내용을 입력해주세요.")
String content,

PostType postType,
DisabilityType disabilityType

@NotNull(message = "장애 종류를 선택해주세요.")
DisabilityType disabilityType,

@NotNull(message = "성별을 선택해주세요.")
Gender gender,

@NotNull(message = "나이를 입력해주세요.")
Integer age,

@NotNull(message = "봉사 시작 시간을 입력해주세요.")
LocalDateTime assistanceStartTime,

@NotNull(message = "봉사 종료 시간을 입력해주세요.")
LocalDateTime assistanceEndTime,

@NotNull(message = "모집 인원을 입력해주세요.")
Integer headcount
) {

public Post toEntity(Member author) {
Expand All @@ -33,8 +70,8 @@ public Post toEntity(Member author) {
.title(title)
.assistanceType(assistanceType)
.schedule(Schedule.builder()
.startTime(startTime)
.endTime(endTime)
.startDate(startDate)
.endDate(endDate)
.scheduleType(scheduleType)
.scheduleDetails(scheduleDetails)
.build())
Expand All @@ -43,6 +80,13 @@ public Post toEntity(Member author) {
.postType(postType)
.postStatus(PostStatus.RECRUITING)
.disabilityType(disabilityType)
.gender(gender)
.age(age)
.assistanceTime(AssistanceTime.builder()
.assistanceStartTime(assistanceStartTime)
.assistanceEndTime(assistanceEndTime)
.build())
.headcount(headcount)
.build();
}
}
43 changes: 17 additions & 26 deletions src/main/java/econo/buddybridge/post/dto/PostResDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@

import econo.buddybridge.member.dto.MemberResDto;
import econo.buddybridge.member.entity.DisabilityType;
import econo.buddybridge.member.entity.Gender;
import econo.buddybridge.post.entity.AssistanceType;
import econo.buddybridge.post.entity.District;
import econo.buddybridge.post.entity.Post;
import econo.buddybridge.post.entity.PostStatus;
import econo.buddybridge.post.entity.PostType;
import econo.buddybridge.post.entity.ScheduleType;
import java.time.LocalDateTime;
import lombok.Builder;

import java.time.LocalDateTime;

@Builder
public record PostResDto(
Long id,
MemberResDto author,
String title,
AssistanceType assistanceType,
LocalDateTime startTime,
LocalDateTime endTime,
LocalDateTime startDate,
LocalDateTime endDate,
ScheduleType scheduleType,
String scheduleDetails,
District district,
Expand All @@ -28,38 +30,22 @@ public record PostResDto(
LocalDateTime modifiedAt,
PostStatus postStatus,
DisabilityType disabilityType,
Gender gender,
Integer age,
LocalDateTime assistanceStartTime,
LocalDateTime assistanceEndTime,
Integer headcount,
Boolean isLiked
) {

public PostResDto(Post post) {
this(
post.getId(),
new MemberResDto(post.getAuthor()),
post.getTitle(),
post.getAssistanceType(),
post.getSchedule().getStartTime(),
post.getSchedule().getEndTime(),
post.getSchedule().getScheduleType(),
post.getSchedule().getScheduleDetails(),
post.getDistrict(),
post.getContent(),
post.getPostType(),
post.getCreatedAt(),
post.getModifiedAt(),
post.getPostStatus(),
post.getDisabilityType(),
false
);
}

public PostResDto(Post post, Boolean isLiked) {
this(
post.getId(),
new MemberResDto(post.getAuthor()),
post.getTitle(),
post.getAssistanceType(),
post.getSchedule().getStartTime(),
post.getSchedule().getEndTime(),
post.getSchedule().getStartDate(),
post.getSchedule().getEndDate(),
post.getSchedule().getScheduleType(),
post.getSchedule().getScheduleDetails(),
post.getDistrict(),
Expand All @@ -69,6 +55,11 @@ public PostResDto(Post post, Boolean isLiked) {
post.getModifiedAt(),
post.getPostStatus(),
post.getDisabilityType(),
post.getGender(),
post.getAge(),
post.getAssistanceTime().getAssistanceStartTime(),
post.getAssistanceTime().getAssistanceEndTime(),
post.getHeadcount(),
isLiked
);
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/econo/buddybridge/post/dto/PostUpdateReqDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package econo.buddybridge.post.dto;

import econo.buddybridge.member.entity.DisabilityType;
import econo.buddybridge.member.entity.Gender;
import econo.buddybridge.post.entity.AssistanceType;
import econo.buddybridge.post.entity.District;
import econo.buddybridge.post.entity.ScheduleType;
import lombok.Builder;

import java.time.LocalDateTime;

@Builder
public record PostUpdateReqDto(
String title,
AssistanceType assistanceType,
LocalDateTime startDate,
LocalDateTime endDate,
ScheduleType scheduleType,
String scheduleDetails,
District district,
String content,
DisabilityType disabilityType,
Gender gender,
Integer age,
LocalDateTime assistanceStartTime,
LocalDateTime assistanceEndTime,
Integer headcount
) {
}
23 changes: 23 additions & 0 deletions src/main/java/econo/buddybridge/post/entity/AssistanceTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package econo.buddybridge.post.entity;

import jakarta.persistence.Embeddable;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Embeddable
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@EqualsAndHashCode
public class AssistanceTime {

LocalDateTime assistanceStartTime;
LocalDateTime assistanceEndTime;
}
Loading

0 comments on commit 88a9fd7

Please sign in to comment.