Skip to content

Commit

Permalink
Fix: 일기 두번 작성 에러 수정 (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyukjinKimm authored Jul 19, 2024
1 parent f280620 commit 9493112
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.donkeys_today.server.application.diary;

import com.donkeys_today.server.application.auth.JwtUtil;
import com.donkeys_today.server.application.user.UserService;
import com.donkeys_today.server.domain.diary.Diary;
import com.donkeys_today.server.domain.user.User;
import com.donkeys_today.server.infrastructure.diary.DiaryRepository;
Expand All @@ -19,6 +21,7 @@ public class DiaryPolicy {
private final ProfanityFilter profanityFilter;
private final DiaryRepository diaryRepository;
private final DiaryRetriever diaryRetriever;
private final UserService userService;

public boolean checkUserInitialDiary(User user) {
return diaryRepository.existsByUser(user);
Expand Down Expand Up @@ -52,4 +55,9 @@ public void updateDeletedDiary(User user, DiaryRequest request) {
.collect(Collectors.toUnmodifiableList());
diaryRepository.saveAll(newDiaries);
}

public boolean hasDiary() {
return !diaryRetriever.getTodayDiariesByUser(userService.getUserById(JwtUtil.getLoginMemberId()),
LocalDateTime.now()).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.donkeys_today.server.presentation.diary.dto.response.DiaryResponse;
import com.donkeys_today.server.presentation.diary.dto.response.DiarySimpleInfo;
import com.donkeys_today.server.presentation.user.dto.response.DiaryCreatedTimeGetResponse;
import com.donkeys_today.server.support.dto.type.ErrorType;
import com.donkeys_today.server.support.exception.DiaryExistException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
Expand Down Expand Up @@ -122,6 +124,11 @@ public DiaryCreatedResponse createDiary(DiaryRequest request) {
return DiaryCreatedResponse.createDiaryWithStaticReply(reply.getCreatedAt());
}

if (diaryPolicy.hasDiary()) {
throw new DiaryExistException(ErrorType.DIARY_ALREADY_EXIST);
}


log.info("diary ; {}", request.content());
List<Diary> diaryList = diaryCreator.saveAllDiary(user, request.content(), createdAt);
DiaryMessage message = diaryPublisher.convertDiariesToMessage(diaryList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public enum ErrorType {
* 일기 관련 오류,
*/
DIARY_MESSAGE_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "일기 데이터가 존재하지 않습니다."),
DIARY_ALREADY_EXIST(HttpStatus.BAD_REQUEST.value(), "일기는 하루에 한번 쓸 수 있습니다.."),

/**
* 답장 관련 오류,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.donkeys_today.server.support.exception;

import com.donkeys_today.server.support.dto.type.ErrorType;

public class DiaryExistException extends BusinessException{

public DiaryExistException(ErrorType errorType) {
super(errorType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ protected ResponseEntity<ApiResponse<?>> BusinessException(ReplyException e){
protected ResponseEntity<ApiResponse<?>> BusinessException(InvalidDateFormatException e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.error(ErrorType.INVALID_DATE_FORMAT,e.getErrorType()));
}

@ExceptionHandler(DiaryExistException.class)
protected ResponseEntity<ApiResponse<?>> DiaryExistException(DiaryExistException e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.error(ErrorType.DIARY_ALREADY_EXIST,e.getErrorType()));
}

}

0 comments on commit 9493112

Please sign in to comment.