Skip to content

Commit

Permalink
Merge pull request #163 from Media-XI/ART-184-be-내가-속한-팀-정보-api-응답-데이…
Browse files Browse the repository at this point in the history
…터-수정

Art 184 be 내가 속한 팀 정보 api 응답 데이터 수정
  • Loading branch information
haroya01 authored Apr 22, 2024
2 parents a7f5cee + 16bdb2a commit fd48f9c
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,18 @@ public ResponseEntity updateEmailReceive(@PathVariable String username,
return new ResponseEntity(message, HttpStatus.OK);
}

// @Operation(summary = "회원 아이디 전체 조회", description = "검색엔진 노출 용 회원 아이디 리스트를
// 조회합니다.")
// @GetMapping("/username")
// public ResponseEntity getAllUsername() {
// List<String> usernameList = memberService.getAllUsername();
// return new ResponseEntity(usernameList, HttpStatus.OK);
// }

@Operation(summary = "회원 아이디 전체 조회", description = "검색엔진 노출 용 회원 아이디 리스트를 조회합니다.")
@GetMapping("/username")
public ResponseEntity getAllUsername() {
List<String> usernameList = memberService.getAllUsername();
return new ResponseEntity(usernameList, HttpStatus.OK);
}

@GetMapping("/{username}/teams")
@Operation(summary = "회원이 속한 팀 목록 조회", description = "회원이 속한 모든 팀들을 조회합니다.")
public ResponseEntity getTeamsByUsername(@PathVariable String username) {
MemberResponseDTO.TeamProfiles response = memberService.getTeamProfiles(username);
return new ResponseEntity(response, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "팀 API", description = "팀과 관련된 API")
@RequestMapping("/api/teams")
Expand Down Expand Up @@ -88,12 +90,4 @@ public ResponseEntity getTeamUsers(@PathVariable Long teamId) {
TeamUserResponse.GetAll response = teamUserService.getTeamUsers(teamId);
return new ResponseEntity(response, HttpStatus.OK);
}

@GetMapping("/members/{username}")
@Operation(summary = "유저가 속한 팀 목록 조회", description = "유저가 속한 모든 팀들을 조회합니다.")
public ResponseEntity getTeamsByUsername(@PathVariable String username) {
Member member = memberService.getEntity(username);
TeamUserResponse.GetAll response = teamUserService.getTeamsByUsername(member);
return new ResponseEntity(response, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public ResponseEntity transferToOwner(@PathVariable String username, @RequestPar
return new ResponseEntity(response, HttpStatus.OK);
}

@PatchMapping("{username}")
@PatchMapping("/{username}")
@LoginOnly
@Operation(summary = "팀 유저 직책 변경", description = "팀 유저의 직책을 변경합니다.")
public ResponseEntity updateTeamUser(@PathVariable String username, @RequestParam Long teamId, @RequestBody @Valid TeamUserRequest.Update request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.example.codebase.domain.member.dto;

import com.example.codebase.domain.magazine.dto.MagazineCategoryResponse;
import com.example.codebase.domain.magazine.entity.MagazineCategory;
import com.example.codebase.domain.member.entity.Member;
import com.example.codebase.domain.team.dto.TeamResponse;
import com.example.codebase.domain.team.entity.TeamUser;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
Expand Down Expand Up @@ -52,7 +55,7 @@ public class MemberResponseDTO {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime updatedTime;

private List<TeamResponse.ProfileGet> teams = new ArrayList<>();
private List<TeamProfile> teams = new ArrayList<>();

public static MemberResponseDTO from(Member member) {
MemberResponseDTO dto = new MemberResponseDTO();
Expand Down Expand Up @@ -80,19 +83,70 @@ public static MemberResponseDTO from(Member member) {

// TODO : 기획자가 아니면 NPE 발생 가능성 있음 해결하기
dto.setCompanyName(
member.getCompanyName() != null ? member.getCompanyName() : null);
member.getCompanyName() != null ? member.getCompanyName() : null);
dto.setCompanyRole(member.getCompanyRole() != null ? member.getCompanyRole() : null);

dto.setAllowEmailReceive(member.isAllowEmailReceive());
dto.setAllowEmailReceiveDateTime(member.getAllowEmailReceiveDatetime());

if (!member.getTeamUser().isEmpty()) {
for (TeamUser teamUser : member.getTeamUser()) {
dto.teams.add(TeamResponse.ProfileGet.from(teamUser.getTeam()));
dto.getTeams().add(TeamProfile.from(teamUser));
}
}

return dto;
}

@Getter
@Setter
@Schema(name = "MemberResponseDTO.TeamProfile", description = "팀의 기본 정보")
public static class TeamProfile {

private Long id;

private String profileImage;

private String name;

public static TeamProfile from(TeamUser teamUser) {
TeamProfile teamProfile = new TeamProfile();
teamProfile.setId(teamUser.getTeam().getId());
teamProfile.setName(teamUser.getTeam().getName());
teamProfile.setProfileImage(teamUser.getTeam().getProfileImage());
return teamProfile;
}
}

@Getter
@Setter
@Schema(name = "MemberResponseDTO.TeamProfileWithRole", description = "팀의 기본 정보와 속한 사용자의 역할")
public static class TeamProfileWithRole extends TeamProfile {
private String role;

public static TeamProfileWithRole from(TeamUser teamUser) {
TeamProfileWithRole profileWithRole = new TeamProfileWithRole();
profileWithRole.setId(teamUser.getTeam().getId());
profileWithRole.setName(teamUser.getTeam().getName());
profileWithRole.setProfileImage(teamUser.getTeam().getProfileImage());
profileWithRole.setRole(teamUser.getRole().toString());
return profileWithRole;
}
}

@Getter
@Setter
@Schema(name = "MemberResponseDTO.TeamProfiles", description = "해당 사용자가 소속된 모든 팀과 권한 조회 DTO")
public static class TeamProfiles {
private List<TeamProfileWithRole> profiles;

public static TeamProfiles from(List<TeamUser> teamUsers) {
TeamProfiles teamProfiles = new TeamProfiles();
teamProfiles.setProfiles(teamUsers.stream()
.map(TeamProfileWithRole::from)
.toList());
return teamProfiles;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,12 @@ public Member getEntity(String username) {
return memberRepository.findByUsername(username)
.orElseThrow(NotFoundMemberException::new);
}

@Transactional(readOnly = true)
public MemberResponseDTO.TeamProfiles getTeamProfiles(String username) {
Member member = memberRepository.findByUsername(username)
.orElseThrow(NotFoundMemberException::new);

return MemberResponseDTO.TeamProfiles.from(member.getTeamUser());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class TeamResponse {
@Getter
@Setter
@Schema(name = "TeamResponse.Get", description = "팀 조회 DTO")
public static class Get{
public static class Get {

private Long id;

Expand Down Expand Up @@ -46,23 +46,4 @@ public static TeamResponse.Get from(Team team) {
return get;
}
}

@Getter
@Setter
@Schema(name = "TeamResponse.ProfileGet", description = "팀 프로파일 조회 DTO")
public static class ProfileGet{
private Long id;

private String profileImage;

private String name;

public static TeamResponse.ProfileGet from(Team team){
ProfileGet profileGet = new ProfileGet();
profileGet.setId(team.getId());
profileGet.setProfileImage(team.getProfileImage());
profileGet.setName(team.getName());
return profileGet;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ public interface TeamUserRepository extends JpaRepository<TeamUser, Long> {

boolean existsByTeamAndMember(Team team, Member member);

List<TeamUser> findByMember(Member member);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.codebase.domain.team.service;

import com.example.codebase.domain.member.entity.Member;
import com.example.codebase.domain.team.dto.TeamResponse;
import com.example.codebase.domain.team.dto.TeamUserRequest;
import com.example.codebase.domain.team.dto.TeamUserResponse;
import com.example.codebase.domain.team.entity.Team;
Expand All @@ -13,6 +14,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -58,7 +60,7 @@ public void addTeamUser(TeamUser member, Member inviteUser, TeamUserRequest.Crea
}

public void deleteTeamUser(TeamUser loginUser, TeamUser deleteUser) {
if(loginUser.isOwner() && deleteUser.isOwner()) {
if (loginUser.isOwner() && deleteUser.isOwner()) {
throw new RuntimeException("팀장은 자신을 추방할 수 없습니다.");
}
teamUserRepository.delete(deleteUser);
Expand All @@ -72,17 +74,11 @@ public void transferToOwner(TeamUser loginUser, TeamUser transferUser) {
}

public void updateTeamUser(TeamUser member, TeamUser changeMember, TeamUserRequest.Update request) {
if(!(Objects.equals(member.getId(), changeMember.getId())) && !member.isOwner()) {
if (!(Objects.equals(member.getId(), changeMember.getId())) && !member.isOwner()) {
throw new RuntimeException("본인 또는 팀장만 정보를 수정할 수 있습니다.");
}

member.update(request);
teamUserRepository.save(member);
}

@Transactional(readOnly = true)
public TeamUserResponse.GetAll getTeamsByUsername(Member member) {
List<TeamUser> teams = teamUserRepository.findByMember(member);
return TeamUserResponse.GetAll.from(teams);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
import com.example.codebase.domain.member.repository.MemberRepository;
import com.example.codebase.domain.team.dto.TeamRequest;
import com.example.codebase.domain.team.dto.TeamResponse;

import com.example.codebase.domain.team.dto.TeamUserRequest;
import com.example.codebase.domain.team.dto.TeamUserResponse;
import com.example.codebase.domain.team.entity.TeamUser;
import com.example.codebase.domain.team.repository.TeamUserRepository;
import com.example.codebase.domain.team.service.TeamService;
import com.example.codebase.domain.team.service.TeamUserService;
import com.fasterxml.jackson.core.type.TypeReference;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.findify.s3mock.S3Mock;
Expand All @@ -39,6 +47,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -78,6 +87,9 @@ class MemberControllerTest {
@Autowired
private TeamService teamService;

@Autowired
private TeamUserService teamUserService;

@BeforeAll
static void setUp(@Autowired S3Mock s3Mock,
@Autowired AmazonS3 amazonS3,
Expand Down Expand Up @@ -157,6 +169,13 @@ public TeamRequest.Create createTeamRequest(String name) {
"자신의 포지션, 직급");
}

public void createAndInviteMember(TeamUser loginUser, Member inviteMember) {
TeamUserRequest.Create request = new TeamUserRequest.Create(
"팀원"
);
teamUserService.addTeamUser(loginUser, inviteMember, request);
}

@DisplayName("회원가입 API가 작동한다")
@Test
void test1() throws Exception {
Expand Down Expand Up @@ -605,8 +624,8 @@ void create_curator() throws Exception {
// then
MemberResponseDTO memberResponse = objectMapper.readValue(response, MemberResponseDTO.class);
assertEquals(2, memberResponse.getTeams().size());
assertEquals(team1.getName(), memberResponse.getTeams().get(0).getName());
assertEquals(team2.getName(), memberResponse.getTeams().get(1).getName());
assertEquals("팀이름1", memberResponse.getTeams().get(0).getName());
assertEquals("팀이름2", memberResponse.getTeams().get(1).getName());
}

@DisplayName("사용자 프로필 조회 시 팀 반환 성공")
Expand All @@ -628,7 +647,37 @@ void create_curator() throws Exception {
// then
MemberResponseDTO memberResponse = objectMapper.readValue(response, MemberResponseDTO.class);
assertEquals(2, memberResponse.getTeams().size());
assertEquals(team1.getName(), memberResponse.getTeams().get(0).getName());
assertEquals(team2.getName(), memberResponse.getTeams().get(1).getName());
assertEquals("팀이름1", memberResponse.getTeams().get(0).getName());
assertEquals("팀이름2", memberResponse.getTeams().get(1).getName());
}

@DisplayName("해당 유저의 팀 목록 조회 ")
@Test
void 유저가_속한__조회() throws Exception {
//given
Member member = createOrLoadMember();
Member member2 = createOrLoadMember(2, RoleStatus.ARTIST);
TeamResponse.Get createTeam1 = createTeam(member, "ownerTeam1");
TeamResponse.Get createTeam2 = createTeam(member, "ownerTeam2");
TeamResponse.Get inviteTeam = createTeam(member2, "memberTeam1");
TeamUser teamOwner = teamUserService.findByTeamIdAndUsername(inviteTeam.getId(),member2.getUsername());
createAndInviteMember(teamOwner, member);

//when
String response = mockMvc.perform(get("/api/members/" + member.getUsername() + "/teams")
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
//then
MemberResponseDTO.TeamProfiles teamProfiles= objectMapper.readValue(response, MemberResponseDTO.TeamProfiles.class);
List<MemberResponseDTO.TeamProfileWithRole> profiles = teamProfiles.getProfiles();
assertEquals(3, profiles.size());
assertEquals("OWNER", profiles.get(0).getRole());
assertEquals(createTeam1.getId(), profiles.get(0).getId());
assertEquals("OWNER", profiles.get(1).getRole());
assertEquals(createTeam2.getId(), profiles.get(1).getId());
assertEquals("MEMBER", profiles.get(2).getRole());
assertEquals(inviteTeam.getId(), profiles.get(2).getId());
}
}
Loading

0 comments on commit fd48f9c

Please sign in to comment.