Skip to content

Commit

Permalink
Update: 확장성 고려하여 enum c->s, s->c, s->db 모두 값 구분, ErrorCode를 domain 종속…
Browse files Browse the repository at this point in the history
… 오류인지, 기본 오류인지 구분
  • Loading branch information
BYEONGRYEOL committed Jun 6, 2024
1 parent fb58d98 commit e8f32f9
Show file tree
Hide file tree
Showing 48 changed files with 416 additions and 335 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class UserPGREQController {
@ApiResponse(responseCode = "200", description = "성공",
content = {@Content(mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = PGREQDetailFindByUserResponseDto.class)))}),
@ApiResponse(responseCode = "404", description = "유저를 찾을 수 없음, ErrorCode.UserNotFound"),
@ApiResponse(responseCode = "404", description = "요청을 찾을 수 없음, ErrorCode.PictureGenerateRequestNotFound"),
@ApiResponse(responseCode = "404", description = "유저를 찾을 수 없음, DomainErrorCode.UserNotFound"),
@ApiResponse(responseCode = "404", description = "요청을 찾을 수 없음, DomainErrorCode.PictureGenerateRequestNotFound"),
})
public ResponseEntity<ApiResult<List<PGREQDetailFindByUserResponseDto>>> getAllUsersPictureGenerateRequest(
@AuthenticationPrincipal UserDetailsImpl userDetails
Expand All @@ -59,8 +59,8 @@ public ResponseEntity<ApiResult<List<PGREQDetailFindByUserResponseDto>>> getAllU
@ApiResponse(responseCode = "200", description = "성공",
content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = PGREQDetailFindByUserResponseDto.class))}),
@ApiResponse(responseCode = "404", description = "유저를 찾을 수 없음, ErrorCode.UserNotFound"),
@ApiResponse(responseCode = "404", description = "현재 진행중인 요청이 없습니다, ErrorCode.ActivePictureGenerateRequestNotExists"),
@ApiResponse(responseCode = "404", description = "유저를 찾을 수 없음, DomainErrorCode.UserNotFound"),
@ApiResponse(responseCode = "404", description = "현재 진행중인 요청이 없습니다, DomainErrorCode.ActivePictureGenerateRequestNotExists"),
})
@GetMapping("/active/deprecated")
public ResponseEntity<ApiResult<PGREQDetailFindByUserResponseDto>> getMyActivePictureGenerateRequest_Deprecated(
Expand All @@ -75,8 +75,8 @@ public ResponseEntity<ApiResult<PGREQDetailFindByUserResponseDto>> getMyActivePi
@ApiResponse(responseCode = "200", description = "성공",
content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = PGREQDetailFindByUserResponseDto.class))}),
@ApiResponse(responseCode = "404", description = "유저를 찾을 수 없음, ErrorCode.UserNotFound"),
@ApiResponse(responseCode = "404", description = "현재 진행중인 요청이 없습니다, ErrorCode.ActivePictureGenerateRequestNotExists"),
@ApiResponse(responseCode = "404", description = "유저를 찾을 수 없음, DomainErrorCode.UserNotFound"),
@ApiResponse(responseCode = "404", description = "현재 진행중인 요청이 없습니다, DomainErrorCode.ActivePictureGenerateRequestNotExists"),
})
@GetMapping("/active")
public ResponseEntity<ApiResult<Boolean>> getMyActivePictureGenerateRequest(
Expand All @@ -91,9 +91,9 @@ public ResponseEntity<ApiResult<Boolean>> getMyActivePictureGenerateRequest(
@ApiResponse(responseCode = "200", description = "성공",
content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = PGREQDetailFindByUserResponseDto.class))}),
@ApiResponse(responseCode = "404", description = "유저를 찾을 수 없음, ErrorCode.UserNotFound"),
@ApiResponse(responseCode = "404", description = "요청을 찾을 수 없음, ErrorCode.PictureGenerateRequestNotFound"),
@ApiResponse(responseCode = "403", description = "사진생성요청을 한 유저 본인만 조회할 수 있습니다., ErrorCode.OnlyRequesterCanViewRequest"),
@ApiResponse(responseCode = "404", description = "유저를 찾을 수 없음, DomainErrorCode.UserNotFound"),
@ApiResponse(responseCode = "404", description = "요청을 찾을 수 없음, DomainErrorCode.PictureGenerateRequestNotFound"),
@ApiResponse(responseCode = "403", description = "사진생성요청을 한 유저 본인만 조회할 수 있습니다., DomainErrorCode.OnlyRequesterCanViewRequest"),
})
@GetMapping("/{pictureGenerateRequestId}")
public ResponseEntity<ApiResult<PGREQDetailFindByUserResponseDto>> getPictureGenerateRequestDetail(
Expand All @@ -110,9 +110,9 @@ public ResponseEntity<ApiResult<PGREQDetailFindByUserResponseDto>> getPictureGen
@ApiResponse(responseCode = "200", description = "성공",
content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ApiResult.class))}),
@ApiResponse(responseCode = "404", description = "유저를 찾을 수 없음, ErrorCode.UserNotFound"),
@ApiResponse(responseCode = "404", description = "요청을 찾을 수 없음, ErrorCode.PictureGenerateRequestNotFound"),
@ApiResponse(responseCode = "403", description = "사진생성요청을 한 유저 본인만 조회할 수 있습니다., ErrorCode.OnlyRequesterCanViewRequest"),
@ApiResponse(responseCode = "404", description = "유저를 찾을 수 없음, DomainErrorCode.UserNotFound"),
@ApiResponse(responseCode = "404", description = "요청을 찾을 수 없음, DomainErrorCode.PictureGenerateRequestNotFound"),
@ApiResponse(responseCode = "403", description = "사진생성요청을 한 유저 본인만 조회할 수 있습니다., DomainErrorCode.OnlyRequesterCanViewRequest"),
})
@PostMapping("")
public ResponseEntity<ApiResult<Boolean>> createPictureGenerateRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import com.gt.genti.dto.PGREQDetailFindResponseDto;
import com.gt.genti.dto.PGREQSaveRequestDto;
import com.gt.genti.dto.PGREQUpdateRequestDto;
import com.gt.genti.error.ErrorCode;
import com.gt.genti.error.DefaultErrorCode;
import com.gt.genti.error.DomainErrorCode;
import com.gt.genti.error.ExpectedException;
import com.gt.genti.external.openai.dto.PromptAdvancementRequestCommand;
import com.gt.genti.external.openai.service.OpenAIService;
Expand Down Expand Up @@ -49,7 +50,7 @@ public Page<PGREQDetailFindResponseDto> getAllPictureGenerateRequest(
PGREQDetailFindResponseDto::new
);
if (result.isEmpty()) {
throw new ExpectedException(ErrorCode.Undefined);
throw new ExpectedException(DefaultErrorCode.UnHandledException, "사진생성요청이 1개도 없음");
}
return result;
}
Expand All @@ -63,21 +64,21 @@ public List<PGREQDetailFindByUserResponseDto> getAllPictureGenerateRequestForUse
PGREQDetailFindByUserResponseDto::new
).toList();
if (result.isEmpty()) {
throw new ExpectedException(ErrorCode.PictureGenerateRequestNotFound);
throw new ExpectedException(DomainErrorCode.PictureGenerateRequestNotFound);
}
return result;
}

private User findUser(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new ExpectedException(ErrorCode.UserNotFound));
.orElseThrow(() -> new ExpectedException(DomainErrorCode.UserNotFound));
}

@Override
public PGREQDetailFindByUserResponseDto findActivePGREQByUser(Long userId) {

PictureGenerateRequest foundPictureGenerateRequest = pictureGenerateRequestPort.findByUserIdOrderByCreatedByDesc(
userId).orElseThrow(() -> new ExpectedException(ErrorCode.PictureGenerateRequestNotFound));
userId).orElseThrow(() -> new ExpectedException(DomainErrorCode.PictureGenerateRequestNotFound));

return new PGREQDetailFindByUserResponseDto(
foundPictureGenerateRequest);
Expand All @@ -96,9 +97,9 @@ public PGREQDetailFindByUserResponseDto findPGREQByUserAndId(Long userId, Long p
PictureGenerateRequest foundPictureGenerateRequest = pictureGenerateRequestPort.findById(
pictureGenerateRequestId)
.orElseThrow(() ->
new ExpectedException(ErrorCode.PictureGenerateRequestNotFound));
new ExpectedException(DomainErrorCode.PictureGenerateRequestNotFound));
if (!Objects.equals(foundPictureGenerateRequest.getRequester().getId(), userId)) {
throw new ExpectedException(ErrorCode.OnlyRequesterCanViewRequest);
throw new ExpectedException(DomainErrorCode.OnlyRequesterCanViewRequest);
}
return new PGREQDetailFindByUserResponseDto(foundPictureGenerateRequest);
}
Expand Down Expand Up @@ -160,10 +161,10 @@ public void modifyPictureGenerateRequest(Long userId,

PictureGenerateRequest findPictureGenerateRequest = pictureGenerateRequestPort.findByIdAndRequesterId(
modifyDto.getPictureGenerateRequestId(), userId)
.orElseThrow(() -> new ExpectedException(ErrorCode.PictureGenerateRequestNotFound));
.orElseThrow(() -> new ExpectedException(DomainErrorCode.PictureGenerateRequestNotFound));

if (findPictureGenerateRequest.getCreator() != null) {
throw new ExpectedException(ErrorCode.RequestAlreadyInProgress);
throw new ExpectedException(DomainErrorCode.RequestAlreadyInProgress);
}

PicturePose picturePose = findPictureGenerateRequest.getPicturePose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.gt.genti.dto.PGREQDetailFindResponseDto;
import com.gt.genti.dto.PGRESUpdateByAdminResponseDto;
import com.gt.genti.dto.PGRESUpdateByCreatorResponseDto;
import com.gt.genti.error.ErrorCode;
import com.gt.genti.error.DomainErrorCode;
import com.gt.genti.error.ExpectedException;
import com.gt.genti.other.util.TimeUtils;
import com.gt.genti.repository.CreatorRepository;
Expand Down Expand Up @@ -63,11 +63,11 @@ public PGREQBriefFindByCreatorResponseDto getPictureGenerateRequestBrief(Long us
foundCreator);
case ASSIGNING ->
foundPGR = pictureGenerateRequestRepository.findByStatusOrderByCreatedAtDesc(foundCreator, status);
default -> throw new ExpectedException(ErrorCode.NotSupportedTemp);
default -> throw new ExpectedException(DomainErrorCode.NotSupportedTemp);
}

return foundPGR.map(PGREQBriefFindByCreatorResponseDto::new)
.orElseThrow(() -> new ExpectedException(ErrorCode.PictureGenerateRequestNotFound));
.orElseThrow(() -> new ExpectedException(DomainErrorCode.PictureGenerateRequestNotFound));

}

Expand All @@ -76,9 +76,9 @@ public PGREQDetailFindResponseDto getPictureGenerateRequestDetail(Long user,
Creator foundCreator = findCreatorByUserId(user);
PictureGenerateRequest foundPictureGenerateRequest = findPGREQ(pictureGenerateRequestId);
if (foundPictureGenerateRequest.getCreator() == null) {
throw new ExpectedException(ErrorCode.NotMatchedYet);
throw new ExpectedException(DomainErrorCode.NotMatchedYet);
} else if (!Objects.equals(foundPictureGenerateRequest.getCreator().getId(), foundCreator.getId())) {
throw new ExpectedException(ErrorCode.NotAssignedToMe);
throw new ExpectedException(DomainErrorCode.NotAssignedToMe);
}

return new PGREQDetailFindResponseDto(foundPictureGenerateRequest);
Expand Down Expand Up @@ -125,28 +125,28 @@ public Boolean updateMemo(Long pictureGenerateResponseId, MemoUpdateRequestDto m
public Boolean acceptPictureGenerateRequest(Long user, Long pictureGenerateRequestId) {
PictureGenerateRequest foundPictureGenerateRequest = findPGREQ(pictureGenerateRequestId);
if (!Objects.equals(foundPictureGenerateRequest.getCreator().getUser().getId(), user)) {
throw new ExpectedException(ErrorCode.NotAssignedToMe);
throw new ExpectedException(DomainErrorCode.NotAssignedToMe);
}
foundPictureGenerateRequest.accept();
return true;
}

private Creator findCreatorByUserId(Long user) {
return creatorRepository.findByUserId(user)
.orElseThrow(() -> new ExpectedException(ErrorCode.CreatorNotFound));
.orElseThrow(() -> new ExpectedException(DomainErrorCode.CreatorNotFound));
}

private PictureGenerateRequest findPGREQ(Long pictureGenerateRequestId) {
return pictureGenerateRequestRepository.findById(
pictureGenerateRequestId)
.orElseThrow(() -> new ExpectedException(ErrorCode.PictureGenerateRequestNotFound));
.orElseThrow(() -> new ExpectedException(DomainErrorCode.PictureGenerateRequestNotFound));
}

@Transactional
public Boolean rejectPictureGenerateRequest(Long user, Long pictureGenerateRequestId) {
PictureGenerateRequest foundPictureGenerateRequest = findPGREQ(pictureGenerateRequestId);
if (!Objects.equals(foundPictureGenerateRequest.getCreator().getUser().getId(), user)) {
throw new ExpectedException(ErrorCode.NotAssignedToMe);
throw new ExpectedException(DomainErrorCode.NotAssignedToMe);
}

foundPictureGenerateRequest.reject();
Expand All @@ -160,7 +160,7 @@ public PGRESUpdateByCreatorResponseDto submitToAdmin(Long user, Long pictureGene
Creator foundCreator = findCreatorByUserId(user);

if (!Objects.equals(foundPGRES.getCreator().getId(), foundCreator.getId())) {
throw new ExpectedException(ErrorCode.NotAssignedToMe);
throw new ExpectedException(DomainErrorCode.NotAssignedToMe);
}

pictureService.findPictureCreatedByCreatorByPictureGenerateResponse(foundPGRES);
Expand All @@ -170,7 +170,7 @@ public PGRESUpdateByCreatorResponseDto submitToAdmin(Long user, Long pictureGene
Duration elapsedDuration = foundPGRES.getElapsedTime();

if (elapsedDuration.compareTo(Duration.ofHours(TimeUtils.PGRES_LIMIT_HOUR)) > 0) {
throw new ExpectedException(ErrorCode.ExpiredPictureGenerateRequest);
throw new ExpectedException(DomainErrorCode.ExpiredPictureGenerateRequest);
}

Long reword = calculateReward(elapsedDuration.toMinutes());
Expand All @@ -182,7 +182,7 @@ public PGRESUpdateByCreatorResponseDto submitToAdmin(Long user, Long pictureGene

settlementRepository.save(settlement);
Deposit foundDeposit = depositRepository.findByUserId(user)
.orElseThrow(() -> new ExpectedException(ErrorCode.DepositNotFound));
.orElseThrow(() -> new ExpectedException(DomainErrorCode.DepositNotFound));

foundDeposit.add(reword);

Expand All @@ -197,7 +197,7 @@ public PGRESUpdateByAdminResponseDto submitFinal(Long pictureGenerateResponseId)
List<PictureCompleted> pictureCompletedList = pictureService.findAllPictureCompletedByPictureGenerateResponse(
foundPGRES);
if (pictureCompletedList.isEmpty()) {
throw new ExpectedException(ErrorCode.FinalPictureNotUploadedYet);
throw new ExpectedException(DomainErrorCode.FinalPictureNotUploadedYet);
}
foundPGRES.adminSubmit();
Duration elapsedDuration = foundPGRES.getElapsedTime();
Expand All @@ -222,7 +222,7 @@ public Boolean updatePictureListCreatedByAdmin(Long uploaderId,
PictureGenerateResponse foundPictureGenerateResponse = pictureGenerateResponseRepository.findById(
pictureGenerateResponseId)
.orElseThrow(() -> new ExpectedException(
ErrorCode.PictureGenerateResponseNotFound));
DomainErrorCode.PictureGenerateResponseNotFound));

List<CreatePictureCompletedCommand> commandList = requestDtoList.stream().map(
dto -> (CreatePictureCompletedCommand)CreatePictureCompletedCommand.builder()
Expand All @@ -249,18 +249,18 @@ public List<PGREQDetailFindResponseDto> getPictureGenerateRequestDetail3(Long us

private User findActiveUserByUserId(Long userId) {
User foundUser = userRepository.findById(userId)
.orElseThrow(() -> new ExpectedException(ErrorCode.UserNotFound));
.orElseThrow(() -> new ExpectedException(DomainErrorCode.UserNotFound));

if (!foundUser.isActivate()) {
throw new ExpectedException(ErrorCode.UserDeactivated);
throw new ExpectedException(DomainErrorCode.UserDeactivated);
}
return foundUser;
}

private PictureGenerateResponse findPGRES(Long pictureGenerateResponseId) {
return pictureGenerateResponseRepository.findById(
pictureGenerateResponseId)
.orElseThrow(() -> new ExpectedException(ErrorCode.PictureGenerateResponseNotFound));
.orElseThrow(() -> new ExpectedException(DomainErrorCode.PictureGenerateResponseNotFound));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.gt.genti.dto.ReportSaveRequestDto;
import com.gt.genti.dto.ReportFindResponseDto;
import com.gt.genti.dto.ReportUpdateRequestDto;
import com.gt.genti.error.ErrorCode;
import com.gt.genti.error.DomainErrorCode;
import com.gt.genti.error.ExpectedException;
import com.gt.genti.repository.PictureGenerateResponseRepository;
import com.gt.genti.repository.ReportRepository;
Expand All @@ -28,7 +28,7 @@ public Boolean createReport(ReportSaveRequestDto reportSaveRequestDto) {
Long pictureGenerateResponseId = reportSaveRequestDto.getPictureGenerateResponseId();
PictureGenerateResponse findPictureGenerateResponse = pictureGenerateResponseRepository.findById(
pictureGenerateResponseId).orElseThrow(() -> new ExpectedException(
ErrorCode.PictureGenerateResponseNotFound));
DomainErrorCode.PictureGenerateResponseNotFound));

Report createReport = new Report(findPictureGenerateResponse, reportSaveRequestDto.getContent());

Expand All @@ -45,7 +45,7 @@ public List<ReportFindResponseDto> getAllReports() {
public Boolean updateReport(ReportUpdateRequestDto reportUpdateRequestDto) {
Long id = reportUpdateRequestDto.getId();
ReportStatus status = reportUpdateRequestDto.getReportStatus();
Report findReport = reportRepository.findById(id).orElseThrow(() -> new ExpectedException(ErrorCode.ReportNotFound));
Report findReport = reportRepository.findById(id).orElseThrow(() -> new ExpectedException(DomainErrorCode.ReportNotFound));
findReport.updateStatus(status);
return true;
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/gt/genti/application/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
import com.gt.genti.dto.UserInfoUpdateRequestDto;
import com.gt.genti.dto.UserRoleUpdateRequestDto;
import com.gt.genti.dto.UserStatusUpdateRequestDto;
import com.gt.genti.error.ErrorCode;
import com.gt.genti.error.DomainErrorCode;
import com.gt.genti.error.ExpectedException;
import com.gt.genti.external.aws.AwsUtils;
import com.gt.genti.repository.CreatorRepository;
import com.gt.genti.repository.PictureCompletedRepository;
import com.gt.genti.repository.UserRepository;
Expand Down Expand Up @@ -122,20 +121,20 @@ public Page<UserFindByAdminResponseDto> getAllUserInfo(UserRole userRole, int pa

private User findDeactivatedUserByUserId(Long userId) {
User foundUser = userRepository.findById(userId)
.orElseThrow(() -> new ExpectedException(ErrorCode.UserNotFound));
.orElseThrow(() -> new ExpectedException(DomainErrorCode.UserNotFound));

if (foundUser.isActivate()) {
throw new ExpectedException(ErrorCode.AlreadyActivatedUser);
throw new ExpectedException(DomainErrorCode.AlreadyActivatedUser);
}
return foundUser;
}

private User findActivateUserByUserId(Long userId) {
User foundUser = userRepository.findById(userId)
.orElseThrow(() -> new ExpectedException(ErrorCode.UserNotFound));
.orElseThrow(() -> new ExpectedException(DomainErrorCode.UserNotFound));

if (!foundUser.isActivate()) {
throw new ExpectedException(ErrorCode.UserDeactivated);
throw new ExpectedException(DomainErrorCode.UserDeactivated);
}
return foundUser;
}
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/gt/genti/domain/Deposit.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.gt.genti.domain;

import com.gt.genti.domain.common.BaseTimeEntity;
import com.gt.genti.domain.enums.ReportStatus;
import com.gt.genti.domain.enums.converter.ReportStatusConverter;
import com.gt.genti.error.ErrorCode;
import com.gt.genti.error.DomainErrorCode;
import com.gt.genti.error.ExpectedException;

import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand Down Expand Up @@ -37,7 +34,7 @@ public class Deposit extends BaseTimeEntity {

public void add(Long amount){
if(amount < 0){
throw new ExpectedException(ErrorCode.AddPointAmountCannotBeMinus);
throw new ExpectedException(DomainErrorCode.AddPointAmountCannotBeMinus);
}
this.depositAmount += amount;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/gt/genti/domain/PictureGenerateRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.gt.genti.domain.enums.converter.ShotCoverageConverter;
import com.gt.genti.dto.PGREQUpdateRequestDto;
import com.gt.genti.dto.PGREQSaveRequestDto;
import com.gt.genti.error.ErrorCode;
import com.gt.genti.error.DomainErrorCode;
import com.gt.genti.error.ExpectedException;

import jakarta.persistence.Column;
Expand Down Expand Up @@ -118,7 +118,7 @@ public void assign(Creator creator) {

public void accept() {
if (LocalDateTime.now().isAfter(this.getModifiedAt().plusMinutes(ACCEPTABLE_TIME_MINUTE))) {
throw new ExpectedException(ErrorCode.ExpiredPictureGenerateRequest);
throw new ExpectedException(DomainErrorCode.ExpiredPictureGenerateRequest);
}
this.pictureGenerateRequestStatus = PictureGenerateRequestStatus.IN_PROGRESS;
}
Expand Down
Loading

0 comments on commit e8f32f9

Please sign in to comment.