Skip to content

Commit

Permalink
Merge pull request #88 from gutanbug/dev
Browse files Browse the repository at this point in the history
fix: 전체 팀 조회에서 팀원 조회도 가능하도록 수정
  • Loading branch information
kjungw1025 authored Mar 31, 2024
2 parents e2a1b6f + ee8988f commit d8650dd
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 16 deletions.
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,41 @@
# renew-sw-mentoring
<h2>2024 멘토멘티 관리 사이트를 위한 백엔드 레포지토리</h2>

2024년도 신입생 / 재학생 멘토멘티 프로그램을 진행하면서 미션 수행하는 걸 쉽게 확인하기 위한 토이 프로젝트입니다.

<사진예시>

<h3>기능</h3>
<p>
<ul>
< 미션 >
<li>미션 전체 조회</li>
<li>미션 상세 조회</li>
</ul>
</p>

<p>
<ul>
< 공지사항 / 미션 인증 게시판 >
<li>게시글 전체 조회</li>
<li>게시글 상세 조회</li>
<li>게시글 수정 , 삭제</li>
<li>댓글, 대댓글</li>
</ul>
</p>

<p>
<ul>
< 기타 >
<li>로그인, 회원가입</li>
<li>팀 상세 조회, 팀 전체 조회</li>
<li>닉네임 변경, 비밀번호 변경</li>
</ul>
</p>

<h3>아키텍처</h3>
<img width="609" alt="image" src="https://github.com/gutanbug/renew-sw-mentoring/assets/112674303/97a9db3c-1017-479c-8a6b-4f35e9a3ec86">


<h3>ERD</h3>
<img width="579" alt="image" src="https://github.com/gutanbug/renew-sw-mentoring/assets/112674303/be6a2062-e2e2-4f90-bf67-a3a774edc80a">

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import lombok.Builder;
import lombok.Getter;

import javax.validation.constraints.NotNull;
import java.util.List;

@Getter
public class SummarizedTeamDto {

Expand All @@ -17,9 +20,14 @@ public class SummarizedTeamDto {
@Schema(description = "점수", example = "500")
private final int score;

public SummarizedTeamDto(Team team) {
@Schema(description = "팀원", example = "[김멘토, 김멘티, 김멘티2]")
private final List<String> members;

public SummarizedTeamDto(Team team,
@NotNull List<String> members) {
this.id = team.getId();
this.teamName = team.getTeamName();
this.score = team.getScore();
this.members = members;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void checkAlreadyTeam(String teamName) {
*/
public Page<SummarizedTeamDto> getAllTeams(Pageable pageable) {
Page<Team> teams = teamRepository.findAllDesc(pageable);
return teams.map(SummarizedTeamDto::new);
return teams.map(team -> new SummarizedTeamDto(team, findTeamMembers(team)));
}

/**
Expand All @@ -64,4 +64,16 @@ public ResponseTeamInfoDto getMyTeamInfo(Long userId) {
}
return new ResponseTeamInfoDto(team, mentor, members);
}

private List<String> findTeamMembers(Team team) {
String mentor = userRepository.findMentorByTeamId(team.getId()).orElseThrow(TeamNotFoundException::new).getName();
List<User> userList = userRepository.findTeamMenteeByTeamId(team.getId());

List<String> members = new ArrayList<>();
members.add(mentor);
for (User user : userList) {
members.add(user.getName());
}
return members;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.renew.sw.mentoring.domain.team.controller;

import com.renew.sw.mentoring.domain.comment.repository.CommentRepository;
import com.renew.sw.mentoring.domain.completedmission.repository.CompletedMissionRepository;
import com.renew.sw.mentoring.domain.post.model.entity.type.MissionBoard;
import com.renew.sw.mentoring.domain.post.model.entity.type.Notice;
import com.renew.sw.mentoring.domain.post.repository.GenericPostRepository;
import com.renew.sw.mentoring.domain.team.model.entity.Team;
import com.renew.sw.mentoring.domain.team.repository.TeamRepository;
import com.renew.sw.mentoring.domain.user.model.UserRole;
import com.renew.sw.mentoring.domain.user.repository.UserRepository;
import com.renew.sw.mentoring.mock.TeamMock;
import com.renew.sw.mentoring.mock.UserMock;
import com.renew.sw.mentoring.util.AbstractContainerRedisTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -38,6 +44,15 @@ class TeamControllerTest extends AbstractContainerRedisTest {
@Autowired
private TeamRepository teamRepository;

@Autowired
private CompletedMissionRepository completedMissionRepository;

@Autowired
private CommentRepository commentRepository;

@Autowired
private GenericPostRepository<MissionBoard> missionBoardRepository;

@Autowired
private GenericPostRepository<Notice> noticeRepository;

Expand All @@ -46,24 +61,27 @@ class TeamControllerTest extends AbstractContainerRedisTest {

@BeforeEach
public void setUp() {
completedMissionRepository.deleteAll();
commentRepository.deleteAll();
missionBoardRepository.deleteAll();
noticeRepository.deleteAll();
userRepository.deleteAll();
teamRepository.deleteAll();

for (int i = 0; i < 4; i++) {
Team team = Team.builder()
.teamName(teamNameList.get(i))
.build();
for (int i = 0; i < teamNameList.size(); i++) {
Team team = TeamMock.create(teamNameList.get(i));
if (i == 4) team.setAdminTeam();
teamList.add(team);
teamRepository.save(team);
}

Team team = Team.builder()
.teamName(teamNameList.get(4))
.build();
team.setAdminTeam();
teamList.add(team);
teamRepository.save(team);
for (int i = 0; i < teamList.size(); i++) {
if (i == 4) {
userRepository.save(UserMock.create(teamList.get(i), UserRole.ADMIN));
} else {
userRepository.save(UserMock.create(teamList.get(i), UserRole.MENTOR));
}
}
}

@Test
Expand All @@ -77,6 +95,7 @@ void list() throws Exception {
.andExpect(jsonPath("content.size()", is(4)))
.andExpect(jsonPath("content[0].id", is(teamList.get(0).getId().intValue())))
.andExpect(jsonPath("content[0].teamName", is("팀1")))
.andExpect(jsonPath("content[0].score", is(0)));
.andExpect(jsonPath("content[0].score", is(0)))
.andExpect(jsonPath("content[0].members.size()", is(1)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.renew.sw.mentoring.domain.team.model.dto.list.SummarizedTeamDto;
import com.renew.sw.mentoring.domain.team.model.entity.Team;
import com.renew.sw.mentoring.domain.team.repository.TeamRepository;
import com.renew.sw.mentoring.domain.user.model.entity.User;
import com.renew.sw.mentoring.domain.user.repository.UserRepository;
import com.renew.sw.mentoring.mock.UserMock;
import com.renew.sw.mentoring.util.DummyPage;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -32,6 +35,9 @@ class TeamServiceTest {
@Mock
private TeamRepository teamRepository;

@Mock
private UserRepository userRepository;

private final List<Team> teamList = new ArrayList<>();

@Test
Expand All @@ -58,9 +64,13 @@ void getAllTeams() {
.build();
teamList.add(team);
}
List<User> userList = UserMock.createList(teamList.get(0), 3);

int TOTAL_ELEMENTS = 10;
Page<Team> teamPage = new DummyPage<>(teamList, TOTAL_ELEMENTS);
when(teamRepository.findAllDesc(any(Pageable.class))).thenReturn(teamPage);
when(userRepository.findMentorByTeamId(any())).thenReturn(Optional.of(userList.get(0)));
when(userRepository.findTeamMenteeByTeamId(any())).thenReturn(userList.subList(0, userList.size() - 1));

// when
Page<SummarizedTeamDto> list = teamService.getAllTeams(Pageable.unpaged());
Expand All @@ -70,6 +80,7 @@ void getAllTeams() {
for (int i = 0; i < list.getTotalElements(); i++) {
SummarizedTeamDto dto = list.getContent().get(i);
assertThat(dto.getTeamName()).isEqualTo(teamList.get(i).getTeamName());
assertThat(dto.getMembers()).isEqualTo(userList.stream().map(User::getName).toList());
}
}
}
32 changes: 30 additions & 2 deletions src/test/java/com/renew/sw/mentoring/mock/UserMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,56 @@
import com.renew.sw.mentoring.domain.team.model.entity.Team;
import com.renew.sw.mentoring.domain.user.model.UserRole;
import com.renew.sw.mentoring.domain.user.model.entity.User;
import com.renew.sw.mentoring.util.EntityUtil;
import com.renew.sw.mentoring.util.RandomGen;
import org.springframework.security.crypto.password.PasswordEncoder;

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

public class UserMock {
public static final String STUDENT_ID = "12345678";
public static final String PASSWORD = "abcdabab";

public static final String NAME = "username";
public static final String NICKNAME = "nickname";
public static User create(Team team, UserRole userRole, PasswordEncoder passwordEncoder) {
public static User create(Long userId, Team team, UserRole userRole, PasswordEncoder passwordEncoder) {
String password = PASSWORD;

if (passwordEncoder != null) {
password = passwordEncoder.encode(password);
}

return User.builder()
User user = User.builder()
.team(team)
.studentId(STUDENT_ID)
.password(password)
.name(NAME)
.nickname(NICKNAME)
.userRole(userRole)
.build();
EntityUtil.injectId(User.class, user, userId);
return user;
}

public static User create(Team team, UserRole userRole, PasswordEncoder passwordEncoder) {
return create(RandomGen.nextLong(), team, userRole, passwordEncoder);
}

public static User create(Team team, UserRole userRole) {
return create(RandomGen.nextLong(), team, userRole, null);
}

public static User create(Long userId, Team team, UserRole userRole) {
return create(userId, team, userRole, null);
}

public static List<User> createList(Team team, int size) {
List<User> users = new ArrayList<>();
users.add(create(team, UserRole.MENTOR));
for (int i = 0; i < size - 1; i++) {
users.add(create(team, UserRole.MENTEE));
}
return users;
}
}
8 changes: 8 additions & 0 deletions src/test/java/com/renew/sw/mentoring/util/EntityUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.renew.sw.mentoring.util;

public class EntityUtil {

public static <T> void injectId(Class<T> clazz, T obj, Long id) {
FieldReflector.inject(clazz, obj, "id", id);
}
}
25 changes: 25 additions & 0 deletions src/test/java/com/renew/sw/mentoring/util/FieldReflector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.renew.sw.mentoring.util;

import java.lang.reflect.Field;

public class FieldReflector {
public static <T> void inject(Class<T> clazz, T obj, String fieldName, Object value) {
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public static <T, R> R get(Class<T> clazz, T obj, String fieldName) {
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return (R) field.get(obj);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
20 changes: 20 additions & 0 deletions src/test/java/com/renew/sw/mentoring/util/RandomGen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.renew.sw.mentoring.util;

import java.util.Random;
import java.util.UUID;

public class RandomGen {
private static final Random RANDOM = new Random();

public static long nextLong() {
return RANDOM.nextLong();
}

public static int nextInt() {
return RANDOM.nextInt();
}

public static String nextUUID() {
return UUID.randomUUID().toString();
}
}

0 comments on commit d8650dd

Please sign in to comment.