Skip to content

Commit

Permalink
Merge pull request #84 from KNU-HAEDAL-Website/fix-create-post-api-is…
Browse files Browse the repository at this point in the history
…sue-83

Fix: 활동일 없으면 공지사항 게시글 생성 안되는 문제
  • Loading branch information
tfer2442 authored Aug 25, 2024
2 parents aeb7f84 + e8833c8 commit db94c63
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/haedal/haedalweb/constants/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public enum ErrorCode implements ResponseCode{
FORBIDDEN_UPDATE(HttpStatus.FORBIDDEN, "019", "수정, 삭제 권한이 없습니다."),
NOT_FOUND_POST_TYPE(HttpStatus.NOT_FOUND, "020", "해당 게시글 타입이 존재하지 않습니다."),
EXIST_POST(HttpStatus.CONFLICT, "021", "해당 게시판에 게시글이 존재하는 경우 삭제할 수 없습니다."),
NOT_FOUND_POST_ID(HttpStatus.NOT_FOUND, "022", "해당 게시글을 찾을 수 없습니다.");
NOT_FOUND_POST_ID(HttpStatus.NOT_FOUND, "022", "해당 게시글을 찾을 수 없습니다."),
INVALID_ARGUMENT(HttpStatus.BAD_REQUEST, "023", "인자 값이 누락되었거나 잘못된 형식입니다.");

private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ResponseEntity<PreSignedUrlDTO> generatePreSignedUrl() {

@Operation(summary = "활동 게시글 생성")
@ApiSuccessCodeExample(SuccessCode.ADD_POST_SUCCESS)
@ApiErrorCodeExamples({ErrorCode.NOT_FOUND_USER_ID, ErrorCode.NOT_FOUND_BOARD_ID, ErrorCode.NOT_FOUND_POST_TYPE})
@ApiErrorCodeExamples({ErrorCode.NOT_FOUND_USER_ID, ErrorCode.NOT_FOUND_BOARD_ID, ErrorCode.NOT_FOUND_POST_TYPE, ErrorCode.INVALID_ARGUMENT})
@Parameter(name = "boardId", description = "게시글 추가할 게시판 ID")
@PostMapping("/boards/{boardId}/posts")
public ResponseEntity<SuccessResponse> addPost(@PathVariable Long boardId, @RequestBody @Valid CreatePostDTO createPostDTO) {
Expand All @@ -60,7 +60,7 @@ public ResponseEntity<SuccessResponse> addPost(@PathVariable Long boardId, @Requ

@Operation(summary = "공지사항, 이벤트 게시글 생성")
@ApiSuccessCodeExample(SuccessCode.ADD_POST_SUCCESS)
@ApiErrorCodeExamples({ErrorCode.NOT_FOUND_USER_ID, ErrorCode.NOT_FOUND_BOARD_ID, ErrorCode.NOT_FOUND_POST_TYPE})
@ApiErrorCodeExamples({ErrorCode.NOT_FOUND_USER_ID, ErrorCode.NOT_FOUND_BOARD_ID, ErrorCode.NOT_FOUND_POST_TYPE, ErrorCode.INVALID_ARGUMENT})
@PostMapping("/posts")
public ResponseEntity<SuccessResponse> addNoticePost(@RequestBody @Valid CreatePostDTO createPostDTO) {
postService.createPost(createPostDTO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ public class CreatePostDTO {
@Schema(description = "게시글 대표 이미지 파일 Url", example = "posts/abc.jpg")
private String postImageUrl;

@Schema(description = "활동 시작일", example = "yyyy-MM-dd (2024-07-24)")
@Schema(description = "활동 시작일 (이벤트와 활동은 필수, 공지사항은 생략)", example = "yyyy-MM-dd (2024-07-24)")
private String postActivityStartDate;

@Schema(description = "활동 종료일", example = "yyyy-MM-dd (2024-07-24)")
@Schema(description = "활동 종료일 (생략 가능)", example = "yyyy-MM-dd (2024-07-24)")
private String postActivityEndDate;

@Schema(description = "게시글 타입", example = "(ACTIVITY, NOTICE, EVENT)")
private String postType;
//
// @Schema(description = "게시글 추가할 게시판 ID (공지사항, 이벤트는 생략 가능)")
// private Long boardId;
}
4 changes: 2 additions & 2 deletions src/main/java/com/haedal/haedalweb/dto/response/PostDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public class PostDTO {
@Schema(description = "게시글 조회수")
private Long postViews;

@Schema(description = "활동 시작일")
@Schema(description = "활동 시작일 (이벤트와 활동은 필수, 공지사항은 생략)", example = "yyyy-MM-dd (2024-07-24)")
private LocalDate postActivityStartDate;

@Schema(description = "활동 종료일")
@Schema(description = "활동 종료일 (생략 가능)", example = "yyyy-MM-dd (2024-07-24)")
private LocalDate postActivityEndDate;

@Schema(description = "게시글 생성일")
Expand Down
33 changes: 29 additions & 4 deletions src/main/java/com/haedal/haedalweb/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Expand All @@ -43,8 +45,19 @@ public void createPost(Long boardId, CreatePostDTO createPostDTO) { // createPos
throw new BusinessException(ErrorCode.NOT_FOUND_POST_TYPE);
}

LocalDate activityStartDate = LocalDate.parse(createPostDTO.getPostActivityStartDate(), DateTimeFormatter.ISO_DATE);
LocalDate activityEndDate = LocalDate.parse(createPostDTO.getPostActivityEndDate(), DateTimeFormatter.ISO_DATE);
LocalDate activityStartDate = null;
LocalDate activityEndDate = null;

try {
activityStartDate = LocalDate.parse(createPostDTO.getPostActivityStartDate(), DateTimeFormatter.ISO_DATE);
} catch (DateTimeException e) {
throw new BusinessException(ErrorCode.INVALID_ARGUMENT);
}

if (createPostDTO.getPostActivityEndDate() != null) {
activityEndDate = LocalDate.parse(createPostDTO.getPostActivityEndDate(), DateTimeFormatter.ISO_DATE);
}

LocalDateTime createDate = LocalDateTime.now();
User creator = userService.getLoggedInUser();

Expand Down Expand Up @@ -76,11 +89,23 @@ public void createPost(CreatePostDTO createPostDTO) {
throw new BusinessException(ErrorCode.NOT_FOUND_POST_TYPE);
}

LocalDate activityStartDate = LocalDate.parse(createPostDTO.getPostActivityStartDate(), DateTimeFormatter.ISO_DATE);
LocalDate activityEndDate = LocalDate.parse(createPostDTO.getPostActivityEndDate(), DateTimeFormatter.ISO_DATE);
LocalDate activityStartDate = null;
LocalDate activityEndDate = null;
LocalDateTime createDate = LocalDateTime.now();
User creator = userService.getLoggedInUser();

if (postType == PostType.EVENT) {
try {
activityStartDate = LocalDate.parse(createPostDTO.getPostActivityStartDate(), DateTimeFormatter.ISO_DATE);
} catch (DateTimeException e) {
throw new BusinessException(ErrorCode.INVALID_ARGUMENT);
}

if (createPostDTO.getPostActivityEndDate() != null) {
activityEndDate = LocalDate.parse(createPostDTO.getPostActivityEndDate(), DateTimeFormatter.ISO_DATE);
}
}

Post post = Post.builder()
.title(createPostDTO.getPostTitle())
.content(createPostDTO.getPostContent())
Expand Down

0 comments on commit db94c63

Please sign in to comment.