Skip to content

Commit

Permalink
Merge pull request #160 from Media-XI/ART-181-be-팀-정보-반환-기능
Browse files Browse the repository at this point in the history
[PR] 팀 정보 반환 기능 추가
  • Loading branch information
haroya01 authored Apr 17, 2024
2 parents 52f9123 + af22f2c commit ec775cc
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.example.codebase.domain.member.dto;

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 lombok.Getter;
import lombok.Setter;

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


@Getter
@Setter
Expand Down Expand Up @@ -50,6 +55,8 @@ 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<>();

Check warning on line 58 in src/main/java/com/example/codebase/domain/member/dto/MemberResponseDTO.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Mismatched query and update of collection

Contents of collection `teams` are updated, but never queried

public static MemberResponseDTO from(Member member) {
MemberResponseDTO dto = new MemberResponseDTO();
dto.setUsername(member.getUsername());
Expand Down Expand Up @@ -83,6 +90,12 @@ public static MemberResponseDTO from(Member member) {
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()));
}
}

return dto;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public class Member {

@Builder.Default
@OneToMany(mappedBy = "member", cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<TeamUser> teamUserRoles = new ArrayList<>();
private List<TeamUser> teamUser = new ArrayList<>();

public static User toUser(Member member) {
return new User(member.getUsername(), member.getPassword(), member.getAuthorities().stream()
Expand Down Expand Up @@ -326,11 +326,14 @@ public void setNotificationSetting(NotificationSetting notificationSetting) {

@PreRemove
private void preRemove() {
for(TeamUser teamUser : teamUserRoles) {
for(TeamUser teamUser : teamUser) {
if(teamUser.isOwner()) {
throw new RuntimeException("팀장인 팀이 존재합니다. 팀장을 변경하거나 팀을 삭제한 후에 시도해주세요.");
}
}
}

public void addTeamUser(TeamUser teamUser) {
this.teamUser.add(teamUser);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.example.codebase.domain.notification.repository.NotificationSettingRepository;
import com.example.codebase.s3.S3Service;
import com.example.codebase.util.RedisUtil;
import jakarta.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -164,6 +164,7 @@ public MemberResponseDTO createCurator(CreateCuratorMemberDTO createCuratorMembe
return MemberResponseDTO.from(member);
}

@Transactional(readOnly = true)
public MemberResponseDTO getProfile(String username) {
Member member =
memberRepository.findByUsername(username).orElseThrow(NotFoundMemberException::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,23 @@ 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 @@ -30,6 +30,8 @@ public static class Get{

private TeamUserRole role;

private Long teamId;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdTime;

Expand All @@ -45,6 +47,7 @@ public static TeamUserResponse.Get from(TeamUser teamUser) {
get.setRole(teamUser.getRole());
get.setCreatedTime(teamUser.getCreatedTime());
get.setUpdatedTime(teamUser.getUpdatedTime());
get.setTeamId(teamUser.getTeam().getId());
return get;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ public class TeamUser {
private LocalDateTime updatedTime = LocalDateTime.now();

public static TeamUser toEntity(String position, Member member, Team team, TeamUserRole role) {
return TeamUser.builder()
TeamUser teamUser = TeamUser.builder()
.member(member)
.team(team)
.role(role)
.position(position)
.build();
member.addTeamUser(teamUser);
return teamUser;
}

public void validOwner() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
import com.amazonaws.services.s3.AmazonS3;
import com.example.codebase.config.S3MockConfig;
import com.example.codebase.domain.auth.WithMockCustomUser;
import com.example.codebase.domain.member.dto.CreateArtistMemberDTO;
import com.example.codebase.domain.member.dto.CreateCuratorMemberDTO;
import com.example.codebase.domain.member.dto.CreateMemberDTO;
import com.example.codebase.domain.member.dto.UpdateMemberDTO;
import com.example.codebase.domain.member.dto.*;
import com.example.codebase.domain.member.entity.Authority;
import com.example.codebase.domain.member.entity.Member;
import com.example.codebase.domain.member.entity.MemberAuthority;
import com.example.codebase.domain.member.entity.RoleStatus;
import com.example.codebase.domain.member.repository.MemberAuthorityRepository;
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.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.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.findify.s3mock.S3Mock;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.*;
Expand All @@ -36,10 +41,12 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
Expand Down Expand Up @@ -73,6 +80,9 @@ class MemberControllerTest {
@Autowired
private MemberRepository memberRepository;

@Autowired
private TeamService teamService;

@BeforeAll
static void setUp(@Autowired S3Mock s3Mock,
@Autowired AmazonS3 amazonS3,
Expand All @@ -91,7 +101,11 @@ static void tearDown(@Autowired S3Mock s3Mock, @Autowired AmazonS3 amazonS3) {

@BeforeEach
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
objectMapper.registerModule(new JavaTimeModule());
}

public Member createOrLoadMember() {
Expand Down Expand Up @@ -135,6 +149,20 @@ private byte[] createImageFile() throws IOException {
return Files.readAllBytes(file.toPath());
}

public TeamResponse.Get createTeam(Member member, String name) {
return teamService.createTeam(createTeamRequest(name), member);
}

public TeamRequest.Create createTeamRequest(String name) {
return new TeamRequest.Create(
name,
"팀 주소",
"http://test.com/profile.jpg",
"http://test.com/background.jpg",
"팀소개",
"자신의 포지션, 직급"
);
}

@DisplayName("회원가입 API가 작동한다")
@Test
Expand Down Expand Up @@ -572,7 +600,7 @@ void create_curator() throws Exception {

mockMvc
.perform(
put("/api/members/" + member.getUsername() +"/email-receive")
put("/api/members/" + member.getUsername() + "/email-receive")
.param("emailReceive", "true")
)
.andDo(print())
Expand Down Expand Up @@ -602,4 +630,53 @@ void create_curator() throws Exception {
.andExpect(status().isOk());
}

@DisplayName("회원 프로필 조회시 팀 목록 반환 성공")
@WithMockCustomUser(username = "testid1", role = "USER")
@Test
void 회원_프로필__조회시__반환_확인() throws Exception {
// given
Member member = createOrLoadMember();
TeamResponse.Get team1 = createTeam(member, "팀이름1");
TeamResponse.Get team2 = createTeam(member, "팀이름2");

// when
String response = mockMvc.perform(
get("/api/members/profile")
.contentType(MediaType.APPLICATION_JSON)
)
.andDo(print())
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString(StandardCharsets.UTF_8);

// 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());
}

@DisplayName("사용자 프로필 조회 시 팀 반환 성공")
@Test
void 사용자_프로필_조회시__반환_확인() throws Exception {
// given
Member member = createOrLoadMember();
TeamResponse.Get team1 = createTeam(member, "팀이름1");
TeamResponse.Get team2 = createTeam(member, "팀이름2");

// when
String response = mockMvc.perform(
get(String.format("/api/members/%s", member.getUsername()))
.contentType(MediaType.APPLICATION_JSON)
)
.andDo(print())
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString(StandardCharsets.UTF_8);


// 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ public void createAndInviteMember(TeamUser loginUser, Member inviteMember) {
TeamUserResponse.GetAll teamUserResponse = objectMapper.readValue(response, TeamUserResponse.GetAll.class);
assertEquals(3, teamUserResponse.getTeamUsers().size());
assertEquals("OWNER", teamUserResponse.getTeamUsers().get(0).getRole().name());
assertEquals(createTeam1.getId(),teamUserResponse.getTeamUsers().get(0).getTeamId());
assertEquals("OWNER", teamUserResponse.getTeamUsers().get(1).getRole().name());
assertEquals(createTeam2.getId(),teamUserResponse.getTeamUsers().get(1).getTeamId());
assertEquals("MEMBER", teamUserResponse.getTeamUsers().get(2).getRole().name());
assertEquals(inviteTeam.getId(),teamUserResponse.getTeamUsers().get(2).getTeamId());
}
}

0 comments on commit ec775cc

Please sign in to comment.