백엔드/Spring Boot
모여봄 #2 CRUD 구현
jhss9747
2025. 3. 2. 19:04
팀 CRUD 구현
내가 맡은 기능은 팀 기능으로써 팀원 추가, 관리 등의 기능을 개발하게 되었다.
CRUD 별로 작성한 코드들을 정리해서 기록해두었다.
Create
- Controller
/**
* 팀 생성 메소드
* @param userNo 유저번호
* @param teamName 팀 이름
* @param teamIntroduce 팀 설명
* @param projectStatus 프로젝트 상태
* @return teamResponseDto
*/
@PostMapping()
public ResponseEntity<TeamResponseDto> createTeam(
@RequestParam Long userNo,
@RequestParam String teamName,
@RequestParam(required = false) String teamIntroduce,
@RequestParam ProjectStatus projectStatus){
TeamDto teamDto = new TeamDto(userNo, teamName, teamIntroduce, projectStatus);
TeamResponseDto teamResponseDto = teamService.createTeam(teamDto);
return ResponseEntity.status(HttpStatus.OK).body(teamResponseDto);
}
- Service
TeamResponseDto createTeam(TeamDto teamDto);
- ServiceImpl
- userNo를 사용하여 해당 유저 정보를 가져옴
- 빌더패턴을 활용하여 팀 정보를 저장
- 유저 정보와 팀 정보를 합쳐서 TeamUser 테이블에 저장
- 해당 팀 정보를 TeamResponseDto에 넣어 반환
- 이 부분은 혹시 몰라서 넣어둠 (제거해도 됨)
/**
* 팀 생성
*
* @param teamDto 팀 생성 정보
* @return teamResponseDto
*/
@Override
public TeamResponseDto createTeam(TeamDto teamDto) {
User user = userRepository.findById(teamDto.getId())
.orElseThrow(() -> new IllegalArgumentException("유저가 존재하지 않습니다."));
// 팀 저장
Team team = Team.builder()
.teamName(teamDto.getTeamName())
.teamIntroduce(teamDto.getTeamIntroduce())
.projectStatus(teamDto.getProjectStatus())
.timePeriod(new TimePeriod())
.build();
team = teamRepository.save(team);
// TeamUser 저장
TeamUser teamUser = TeamUser.builder()
.user(user)
.team(team)
.status(true)
.build();
teamUserRepository.save(teamUser);
TeamResponseDto teamResponseDto = new TeamResponseDto(
team.getNo(),
team.getTeamName(),
team.getTeamIntroduce(),
team.getProjectStatus(),
team.getTimePeriod()
);
return teamResponseDto;
}
Read
생략
이후에 따로 작성할 예정
Update
해당하는 팀이 없을 수 있기 때문에 모든 코드에 예외 처리가 되어있음
- Controll
/**
* 팀 수정 메소드
* @param teamNo 팀 번호
* @param teamName 팀 이름
* @param teamIntroduce 팀 정보
* @param projectStatus 팀 상태
* @return teamDto
*/
@Operation(summary = "팀 수정 메서드", description = "팀 수정 메서드 입니다.")
@PutMapping()
public ResponseEntity<TeamDto> updateTeam(
@RequestParam Long teamNo,
@RequestParam String teamName,
@RequestParam(required = false) String teamIntroduce,
@RequestParam ProjectStatus projectStatus) throws Exception {
TeamDto teamDto = new TeamDto(teamNo, teamName, teamIntroduce, projectStatus);
teamService.updateTeam(teamDto);
return ResponseEntity.status(HttpStatus.OK).body(teamDto);
}
- Team 엔티티
- 엔티티에 직접 대입할 수 없기에 생성자 추가
public void updateTeamDetails(String teamName, String teamIntroduce, ProjectStatus projectStatus, TimePeriod timePeriod) {
this.teamName = teamName;
this.teamIntroduce = teamIntroduce;
this.projectStatus = projectStatus;
this.timePeriod = timePeriod;
}
- Service
TeamResponseDto updateTeam(TeamDto team) throws Exception;
- ServiceImpl
- 팀번호로 해당하는 팀의 정보를 먼저 찾아옴
- 없으면 예외 반환
- 해당 팀 정보에 변경된 정보 삽입
- 저장 후 Dto 반환 ← 반환 값 임의로 넣어둠 (제거해도 문제없음)
- 팀번호로 해당하는 팀의 정보를 먼저 찾아옴
/**
* 팀 정보 수정
*
* @param teamDto 팀 정보
* @return TeamResponseDto
*/
@Override
public TeamResponseDto updateTeam(TeamDto teamDto) throws Exception{
Team searchTeam = teamRepository.findById(teamDto.getId())
.orElseThrow(() -> new Exception("팀을 찾을 수 없습니다."));
searchTeam.updateTeamDetails(
teamDto.getTeamName(),
teamDto.getTeamIntroduce(),
teamDto.getProjectStatus(),
new TimePeriod()
);
Team updateTeam = teamRepository.save(searchTeam);
return new TeamResponseDto(
updateTeam.getNo(),
updateTeam.getTeamName(),
updateTeam.getTeamIntroduce(),
updateTeam.getProjectStatus(),
updateTeam.getTimePeriod()
);
}
Delete
- Controll
/**
* 팀 삭제 메소드
* @param teamId 팀id
* @return 없음
* @throws Exception 예외없음
*/
@Operation(summary = "팀 삭제 메서드", description = "팀 삭제 메서드입니다.")
@DeleteMapping()
public ResponseEntity<String> deleteTeam(Long teamId) throws Exception {
teamService.deleteTeam(teamId);
return ResponseEntity.status(HttpStatus.OK).body("정상적으로 삭제되었습니다.");
}
- Service
void deleteTeam(Long id) throws Exception;
- ServiceImpl
/**
* 팀 삭제
* @param id 팀 ID
* @throws Exception
*/
@Override
public void deleteTeam(Long id) throws Exception {
teamRepository.deleteById(id);
}
- Team 엔티티
- 팀이 삭제되면 팀-유저 테이블도 삭제되도록 Cascade 설정
@OneToMany(mappedBy = "team", cascade = CascadeType.ALL, orphanRemoval = true)
private List<TeamUser> teamUsers = new ArrayList<>();