Feat: 워크스페이스 멤버 추가 구현- S11P21S002-37

This commit is contained in:
kimtaesoo7 2024-08-30 10:57:42 +09:00
parent 617ad12dd2
commit f140ff11da
4 changed files with 34 additions and 4 deletions

View File

@ -8,6 +8,8 @@ import org.springframework.stereotype.Repository;
public interface WorkspaceParticipantRepository extends JpaRepository<WorkspaceParticipant, Integer> { public interface WorkspaceParticipantRepository extends JpaRepository<WorkspaceParticipant, Integer> {
boolean existsByMemberIdAndWorkspaceId(Integer memberId, Integer workspaceId); boolean existsByMemberIdAndWorkspaceId(Integer memberId, Integer workspaceId);
boolean existsByWorkspaceIdAndMemberId(Integer workspaceId, Integer memberId);
} }

View File

@ -77,4 +77,16 @@ public class WorkspaceController {
workspaceService.deleteWorkspace(memberId, workspaceId); workspaceService.deleteWorkspace(memberId, workspaceId);
return SuccessResponse.empty(); return SuccessResponse.empty();
} }
@Operation(summary = "워크스페이스 멤버 추가", description = "특정 워크스페이스에 멤버를 추가합니다.")
@SwaggerApiSuccess(description = "특정 워크스페이스에 멤버를 성공적으로 추가합니다.")
@SwaggerApiError({ErrorCode.BAD_REQUEST, ErrorCode.NOT_AUTHOR, ErrorCode.SERVER_ERROR})
@PostMapping("/{workspace_id}/member/{member_id}")
public BaseResponse<Void> addWorkspaceMember(
@CurrentUser final Integer memberId,
@PathVariable("workspace_id") final Integer workspaceId,
@PathVariable("member_id") final Integer newMemberId) {
workspaceService.addWorkspaceMember(memberId, workspaceId, newMemberId);
return SuccessResponse.empty();
}
} }

View File

@ -78,6 +78,14 @@ public class WorkspaceService {
workspaceRepository.delete(workspace); workspaceRepository.delete(workspace);
} }
public void addWorkspaceMember(final Integer memberId, final Integer workspaceId, final Integer newMemberId) {
Workspace workspace = getWorkspaceWithWriter(memberId, workspaceId);
checkWorkspaceMember(workspaceId, newMemberId);
Member member = getMember(newMemberId);
workspaceParticipantRepository.save(WorkspaceParticipant.of(workspace, member));
}
private Member getMember(final Integer memberId) { private Member getMember(final Integer memberId) {
return memberRepository.findById(memberId) return memberRepository.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
@ -97,4 +105,10 @@ public class WorkspaceService {
throw new CustomException(ErrorCode.NOT_AUTHOR); throw new CustomException(ErrorCode.NOT_AUTHOR);
} }
} }
private void checkWorkspaceMember(final Integer workspaceId, final Integer newMemberId) {
if (workspaceParticipantRepository.existsByWorkspaceIdAndMemberId(workspaceId, newMemberId)) {
throw new CustomException(ErrorCode.EARLY_ADD_MEMBER);
}
}
} }

View File

@ -30,13 +30,15 @@ public enum ErrorCode {
// Workspace - 3000 // Workspace - 3000
NOT_AUTHOR(HttpStatus.FORBIDDEN, 3001, "작성자가 아닙니다. 이 작업을 수행할 권한이 없습니다."), NOT_AUTHOR(HttpStatus.FORBIDDEN, 3001, "작성자가 아닙니다. 이 작업을 수행할 권한이 없습니다."),
WORKSPACE_NOT_FOUND(HttpStatus.BAD_REQUEST, 3002, "해당 워크스페이스는 존재하지 않습니다."), WORKSPACE_NOT_FOUND(HttpStatus.BAD_REQUEST, 3002, "해당 워크스페이스는 존재하지 않습니다."),
EARLY_ADD_MEMBER(HttpStatus.BAD_REQUEST, 3003, "이미 추가된 멤버입니다."),
// Project - 4000 // Project - 4000
PROJECT_NOT_FOUND(HttpStatus.NOT_FOUND, 4000, "프로젝트를 찾을 수 없습니다"), PROJECT_NOT_FOUND(HttpStatus.NOT_FOUND, 4000, "프로젝트를 찾을 수 없습니다"),
// Participant - 5000 // Participant - 5000,
PARTICIPANT_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, 5000, "해당 프로젝트에 접근 권한이 없습니다."), PARTICIPANT_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, 5000, "해당 프로젝트에 접근 권한이 없습니다."),
; ;
private final HttpStatus status; private final HttpStatus status;