-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] 프로젝트 매칭 요청 생성 메서드 구현, 프로젝트 매칭 요청 관련 유효성 검사 메서드 구현
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...n/java/io/oeid/mogakgo/domain/project_join_req/application/ProjectJoinRequestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.oeid.mogakgo.domain.project_join_req.application; | ||
|
||
import static io.oeid.mogakgo.exception.code.ErrorCode400.INVALID_PROJECT_JOIN_REQUEST; | ||
import static io.oeid.mogakgo.exception.code.ErrorCode400.INVALID_PROJECT_JOIN_REQUEST_REGION; | ||
import static io.oeid.mogakgo.exception.code.ErrorCode400.PROJECT_JOIN_REQUEST_ALREADY_EXIST; | ||
import static io.oeid.mogakgo.exception.code.ErrorCode403.PROJECT_JOIN_FORBIDDEN_OPERATION; | ||
import static io.oeid.mogakgo.exception.code.ErrorCode404.PROJECT_NOT_FOUND; | ||
import static io.oeid.mogakgo.exception.code.ErrorCode404.USER_NOT_FOUND; | ||
|
||
import io.oeid.mogakgo.domain.project.domain.entity.Project; | ||
import io.oeid.mogakgo.domain.project.exception.ProjectException; | ||
import io.oeid.mogakgo.domain.project.infrastructure.ProjectJpaRepository; | ||
import io.oeid.mogakgo.domain.project_join_req.application.dto.req.ProjectJoinCreateReq; | ||
import io.oeid.mogakgo.domain.project_join_req.domain.entity.ProjectJoinRequest; | ||
import io.oeid.mogakgo.domain.project_join_req.exception.ProjectJoinRequestException; | ||
import io.oeid.mogakgo.domain.project_join_req.infrastruture.ProjectJoinRequestJpaRepository; | ||
import io.oeid.mogakgo.domain.user.domain.User; | ||
import io.oeid.mogakgo.domain.user.exception.UserException; | ||
import io.oeid.mogakgo.domain.user.infrastructure.UserJpaRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ProjectJoinRequestService { | ||
|
||
private final ProjectJoinRequestJpaRepository projectJoinRequestRepository; | ||
private final UserJpaRepository userRepository; | ||
private final ProjectJpaRepository projectRepository; | ||
|
||
public Long create(Long userId, ProjectJoinCreateReq request) { | ||
User tokenUser = validateToken(userId); | ||
validateSender(tokenUser, request.getSenderId()); | ||
Project project = validateProjectExist(request.getProjectId()); | ||
validateProjectCreator(project, userId); | ||
validateUserRegion(project, tokenUser); | ||
validateAlreadyExistRequest(userId, project.getId()); | ||
|
||
// 프로젝트 매칭 요청 생성 | ||
ProjectJoinRequest projectJoinRequest = ProjectJoinRequest.of(tokenUser, project); | ||
projectJoinRequestRepository.save(projectJoinRequest); | ||
|
||
return projectJoinRequest.getId(); | ||
} | ||
|
||
private User validateToken(Long userId) { | ||
return userRepository.findById(userId) | ||
.orElseThrow(() -> new UserException(USER_NOT_FOUND)); | ||
} | ||
|
||
private void validateSender(User tokenUser, Long userId) { | ||
if (!tokenUser.getId().equals(userId)) { | ||
throw new ProjectJoinRequestException(PROJECT_JOIN_FORBIDDEN_OPERATION); | ||
} | ||
} | ||
|
||
private Project validateProjectExist(Long projectId) { | ||
return projectRepository.findById(projectId) | ||
.orElseThrow(() -> new ProjectException(PROJECT_NOT_FOUND)); | ||
} | ||
|
||
private void validateProjectCreator(Project project, Long userId) { | ||
if (project.getCreator().getId().equals(userId)) { | ||
throw new ProjectJoinRequestException(INVALID_PROJECT_JOIN_REQUEST); | ||
} | ||
} | ||
|
||
private void validateAlreadyExistRequest(Long userId, Long projectId) { | ||
projectJoinRequestRepository.findAlreadyExists(userId, projectId) | ||
.orElseGet(() -> { | ||
throw new ProjectJoinRequestException(PROJECT_JOIN_REQUEST_ALREADY_EXIST); | ||
}); | ||
} | ||
|
||
private void validateUserRegion(Project project, User tokenUser) { | ||
if (!tokenUser.getRegion().equals(project.getCreatorInfo().getRegion())) { | ||
throw new ProjectJoinRequestException(INVALID_PROJECT_JOIN_REQUEST_REGION); | ||
} | ||
} | ||
} |