-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feat: νμ μΆκ°/μμ /μ‘°ν API κ°λ°
- Loading branch information
Showing
16 changed files
with
239 additions
and
30 deletions.
There are no files selected for viewing
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
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
65 changes: 51 additions & 14 deletions
65
src/main/java/com/codiary/backend/domain/team/controller/TeamController.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 |
---|---|---|
@@ -1,57 +1,94 @@ | ||
package com.codiary.backend.domain.team.controller; | ||
|
||
import com.codiary.backend.domain.member.entity.Member; | ||
import com.codiary.backend.domain.member.security.CustomMemberDetails; | ||
import com.codiary.backend.domain.member.service.MemberCommandService; | ||
import com.codiary.backend.domain.team.converter.TeamConverter; | ||
import com.codiary.backend.domain.team.dto.request.TeamRequestDTO; | ||
import com.codiary.backend.domain.team.dto.response.TeamResponseDTO; | ||
import com.codiary.backend.domain.team.entity.Team; | ||
import com.codiary.backend.domain.team.entity.TeamMember; | ||
import com.codiary.backend.domain.team.service.TeamMemberService; | ||
import com.codiary.backend.domain.team.service.TeamService; | ||
import com.codiary.backend.global.apiPayload.ApiResponse; | ||
import com.codiary.backend.global.apiPayload.code.status.SuccessStatus; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/teams") | ||
@RequestMapping("/api/v2/teams") | ||
@RequiredArgsConstructor | ||
@Tag(name = "ν API", description = "ν μμ±/μ‘°ν/μμ κ΄λ ¨ API μ λλ€.") | ||
public class TeamController { | ||
private final TeamService teamService; | ||
private final TeamMemberService teamMemberService; | ||
private final MemberCommandService memberCommandService; | ||
|
||
@PostMapping("") | ||
@Operation(summary = "ν μμ±") | ||
public ApiResponse<TeamResponseDTO.TeamDTO> createTeam(@RequestBody TeamRequestDTO.CreateTeamDTO request){ | ||
Member member = memberCommandService.getRequester(); | ||
Team newTeam = teamService.createTeam(request, member); | ||
public ApiResponse<TeamResponseDTO.TeamDTO> createTeam(@RequestBody TeamRequestDTO.CreateTeamDTO request, | ||
@AuthenticationPrincipal CustomMemberDetails memberDetails){ | ||
Team newTeam = teamService.createTeam(request, memberDetails.getId()); | ||
return ApiResponse.onSuccess(SuccessStatus.TEAM_OK, TeamConverter.toTeamResponseDto(newTeam)); | ||
} | ||
|
||
@GetMapping("/profile/{team_id}") | ||
@Operation(summary = "ν νλ‘ν μ‘°ν") | ||
public ApiResponse<TeamResponseDTO.TeamProfileDTO> getTeamProfile(@PathVariable("team_id") Long teamId){ | ||
Member member = memberCommandService.getRequester(); | ||
Team fetchedTeam = teamService.getTeamProfile(teamId, member); | ||
public ApiResponse<TeamResponseDTO.TeamProfileDTO> getTeamProfile(@PathVariable("team_id") Long teamId, | ||
@AuthenticationPrincipal CustomMemberDetails memberDetails){ | ||
Team fetchedTeam = teamService.getTeamProfile(teamId, memberDetails.getId()); | ||
return ApiResponse.onSuccess(SuccessStatus.TEAM_OK, TeamConverter.toTeamProfileResponseDto(fetchedTeam)); | ||
} | ||
|
||
@GetMapping("/{team_id}") | ||
@Operation(summary = "ν μ 보 μ‘°ν") | ||
public ApiResponse<TeamResponseDTO.TeamDTO> getTeam(@PathVariable("team_id") Long teamId){ | ||
Member member = memberCommandService.getRequester(); | ||
Team fetchedTeam = teamService.getTeam(teamId, member); | ||
public ApiResponse<TeamResponseDTO.TeamDTO> getTeam(@PathVariable("team_id") Long teamId, | ||
@AuthenticationPrincipal CustomMemberDetails memberDetails){ | ||
Team fetchedTeam = teamService.getTeam(teamId, memberDetails.getId()); | ||
return ApiResponse.onSuccess(SuccessStatus.TEAM_OK, TeamConverter.toTeamResponseDto(fetchedTeam)); | ||
} | ||
|
||
@PutMapping("/{team_id}") | ||
@Operation(summary = "ν μ 보 μμ ") | ||
public ApiResponse<TeamResponseDTO.TeamDTO> updateTeam(@PathVariable("team_id") Long teamId, @RequestBody TeamRequestDTO.UpdateTeamDTO request){ | ||
Member member = memberCommandService.getRequester(); | ||
Team updatedTeam = teamService.updateTeam(request, teamId, member); | ||
public ApiResponse<TeamResponseDTO.TeamDTO> updateTeam(@PathVariable("team_id") Long teamId, | ||
@RequestBody TeamRequestDTO.UpdateTeamDTO request, | ||
@AuthenticationPrincipal CustomMemberDetails memberDetails){ | ||
Team updatedTeam = teamService.updateTeam(request, teamId, memberDetails.getId()); | ||
return ApiResponse.onSuccess(SuccessStatus.TEAM_OK, TeamConverter.toTeamResponseDto(updatedTeam)); | ||
} | ||
|
||
@PostMapping("/team_member") | ||
@Operation(summary = "νμ μΆκ°") | ||
public ApiResponse<TeamResponseDTO.TeamMemberDTO> addTeamMember( | ||
@RequestParam("team_id") Long teamId, | ||
@RequestBody TeamRequestDTO.TeamMemberDTO request, | ||
@AuthenticationPrincipal CustomMemberDetails memberDetails | ||
){ | ||
TeamMember teamMember = teamMemberService.addTeamMember(memberDetails.getId(), teamId, request); | ||
return ApiResponse.onSuccess(SuccessStatus.TEAM_OK, TeamConverter.toTeamMemberResponseDTO(teamMember)); | ||
} | ||
|
||
@DeleteMapping("/team_member") | ||
@Operation(summary = "νμ μμ ") | ||
public ApiResponse<String> deleteTeamMember( | ||
@RequestParam("team_id") Long teamId, | ||
@RequestParam("member_id") Long memberId, | ||
@AuthenticationPrincipal CustomMemberDetails memberDetails | ||
){ | ||
teamMemberService.deleteTeamMember(memberDetails.getId(), teamId, memberId); | ||
return ApiResponse.onSuccess(SuccessStatus.TEAM_OK, "νμ μμ κ° μλ£λμμ΅λλ€."); | ||
} | ||
|
||
@GetMapping("/team_member") | ||
@Operation(summary = "νμ μ‘°ν") | ||
public ApiResponse<List<TeamResponseDTO.TeamMemberDTO>> getTeamMember( | ||
@RequestParam("team_id") Long teamId | ||
){ | ||
Team team = teamMemberService.getTeamMember(teamId); | ||
return ApiResponse.onSuccess(SuccessStatus.TEAM_OK, TeamConverter.toTeamMemberListResponseDto(team)); | ||
} | ||
} |
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
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
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
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
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
14 changes: 14 additions & 0 deletions
14
src/main/java/com/codiary/backend/domain/team/repository/TeamMemberRepository.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,14 @@ | ||
package com.codiary.backend.domain.team.repository; | ||
|
||
import com.codiary.backend.domain.member.entity.Member; | ||
import com.codiary.backend.domain.team.entity.Team; | ||
import com.codiary.backend.domain.team.entity.TeamMember; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface TeamMemberRepository extends JpaRepository<TeamMember, Long> { | ||
boolean existsByTeamAndMember(Team team, Member member); | ||
|
||
Optional<TeamMember> findByTeamAndMember(Team team, Member member); | ||
} |
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
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
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
83 changes: 83 additions & 0 deletions
83
src/main/java/com/codiary/backend/domain/team/service/TeamMemberService.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,83 @@ | ||
package com.codiary.backend.domain.team.service; | ||
|
||
import com.codiary.backend.domain.member.entity.Member; | ||
import com.codiary.backend.domain.member.repository.MemberRepository; | ||
import com.codiary.backend.domain.team.dto.request.TeamRequestDTO; | ||
import com.codiary.backend.domain.team.entity.Team; | ||
import com.codiary.backend.domain.team.entity.TeamMember; | ||
import com.codiary.backend.domain.team.enumerate.TeamMemberRole; | ||
import com.codiary.backend.domain.team.repository.TeamMemberRepository; | ||
import com.codiary.backend.domain.team.repository.TeamRepository; | ||
import com.codiary.backend.global.apiPayload.code.status.ErrorStatus; | ||
import com.codiary.backend.global.apiPayload.exception.GeneralException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class TeamMemberService { | ||
private final TeamMemberRepository teamMemberRepository; | ||
private final TeamRepository teamRepository; | ||
private final MemberRepository memberRepository; | ||
|
||
@Transactional | ||
public TeamMember addTeamMember(Long requestMemberId, Long teamId, TeamRequestDTO.TeamMemberDTO request) { | ||
//validation: ν/λ©€λ² μ ν¨μ± λ° νμ μ κ·ΌμΈμ§, μ‘΄μ¬νλ λλ€μμΈμ§, μ΄λ―Έ μΆκ°ν νμμΈμ§ μ ν¨μ± κ²μ¬ | ||
Team team = teamRepository.findById(teamId) | ||
.orElseThrow(() -> new GeneralException(ErrorStatus.TEAM_NOT_FOUND)); | ||
|
||
Member requestMember = memberRepository.findById(requestMemberId) | ||
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND)); | ||
|
||
if(!teamMemberRepository.existsByTeamAndMember(team, requestMember)){ | ||
throw new GeneralException(ErrorStatus.TEAM_MEMBER_ONLY_ACCESS); | ||
} | ||
|
||
Member newMember = memberRepository.findByNicknameIgnoreCase(request.memberNickName().toString()) | ||
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND)); | ||
|
||
if(teamMemberRepository.existsByTeamAndMember(team, newMember)){ | ||
throw new GeneralException(ErrorStatus.TEAM_MEMBER_ALREADY_EXISTS); | ||
} | ||
|
||
//business logic: νμ μΆκ° | ||
TeamMember teamMember = TeamMember.builder() | ||
.team(team) | ||
.member(newMember) | ||
.teamMemberRole(TeamMemberRole.valueOf(request.memberRole())) | ||
.build(); | ||
|
||
//return | ||
return teamMemberRepository.save(teamMember); | ||
} | ||
|
||
@Transactional | ||
public void deleteTeamMember(Long requestMemberId, Long teamId, Long memberId) { | ||
//validation: ν/μμ²μ/νμ μ ν¨μ± λ° μμ²μκ° νμμΈμ§, μ‘΄μ¬νλ νμμΈμ§ μ ν¨μ± κ²μ¬ | ||
Team team = teamRepository.findById(teamId) | ||
.orElseThrow(() -> new GeneralException(ErrorStatus.TEAM_NOT_FOUND)); | ||
|
||
Member requester = memberRepository.findById(requestMemberId) | ||
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND)); | ||
|
||
Member member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND)); | ||
|
||
if(!teamMemberRepository.existsByTeamAndMember(team, requester)){ | ||
throw new GeneralException(ErrorStatus.TEAM_MEMBER_NOT_FOUND); | ||
} | ||
|
||
TeamMember teamMember = teamMemberRepository.findByTeamAndMember(team, member) | ||
.orElseThrow(() -> new GeneralException(ErrorStatus.TEAM_MEMBER_NOT_FOUND)); | ||
|
||
//business logic: νμ μμ | ||
teamMemberRepository.delete(teamMember); | ||
} | ||
|
||
public Team getTeamMember(Long teamId){ | ||
return teamRepository.findByIdWithTeamMemberList(teamId) | ||
.orElseThrow(() -> new GeneralException(ErrorStatus.TEAM_NOT_FOUND)); | ||
} | ||
} |
Oops, something went wrong.