Skip to content

Commit

Permalink
[FEAT] 프로젝트 매칭 요청 생성 메서드 구현, 프로젝트 매칭 요청 관련 유효성 검사 메서드 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
JIN-076 committed Feb 19, 2024
1 parent 6920c2a commit 65d3d3b
Showing 1 changed file with 80 additions and 0 deletions.
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);
}
}
}

0 comments on commit 65d3d3b

Please sign in to comment.