Skip to content

Commit

Permalink
Merge pull request #48 from gutanbug/dev
Browse files Browse the repository at this point in the history
feat #14 : 팀 상세 조회 로직 추가
  • Loading branch information
gutanbug authored Mar 11, 2024
2 parents 5cd4e39 + d461d87 commit e17c6bc
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.renew.sw.mentoring.domain.completedmission.repository;

import com.renew.sw.mentoring.domain.completedmission.model.entity.CompletedMission;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface CompletedMissionRepository extends JpaRepository<CompletedMission, Long> {

@Query("select c from CompletedMission c where c.team.id = :teamId")
Page<CompletedMission> findAllByTeamId(Pageable pageable, @Param("teamId") Long teamId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.renew.sw.mentoring.domain.team.model.dto.list.SummarizedTeamDto;
import com.renew.sw.mentoring.domain.team.model.dto.response.ResponsePage;
import com.renew.sw.mentoring.domain.team.model.dto.response.ResponseTeamInfoDto;
import com.renew.sw.mentoring.domain.team.service.TeamService;
import com.renew.sw.mentoring.global.auth.jwt.AppAuthentication;
import com.renew.sw.mentoring.global.auth.role.UserAuth;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
Expand All @@ -29,4 +32,16 @@ public ResponsePage<SummarizedTeamDto> getTeams(@RequestParam(defaultValue = "1"
Page<SummarizedTeamDto> teams = teamService.getAllTeams(pageable);
return new ResponsePage<>(teams);
}

/**
* 우리 팀 상세 조회
* <p>
* 팀원, 멘토, 총 점수가 조회됩니다.
* </p>
*/
@GetMapping("/my")
@UserAuth
public ResponseTeamInfoDto myTeam(AppAuthentication auth) {
return teamService.getMyTeamInfo(auth.getUserId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.renew.sw.mentoring.domain.team.model.dto.response;

import com.renew.sw.mentoring.domain.team.model.entity.Team;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;

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

@Getter
public class ResponseTeamInfoDto {

@Schema(description = "팀명", example = "소웨1조")
private final String teamName;

@Schema(description = "총 점수", example = "100")
private final int score;

@Schema(description = "멘토", example = "김멘토")
private final String mentor;

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

public ResponseTeamInfoDto(Team team,
@NotNull String mentor,
@NotNull List<String> members) {
this.teamName = team.getTeamName();
this.score = team.getScore();
this.mentor = mentor;
this.members = members;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ public interface TeamRepository extends JpaRepository<Team, Long> {

@Query("select t from Team t where t.isAdminTeam = false order by t.score desc")
Page<Team> findAllDesc(Pageable pageable);

@Query("select t from Team t " +
"inner join User u " +
"on t.id = u.team.id " +
"where u.id = :userId")
Optional<Team> findByUserId(@Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
package com.renew.sw.mentoring.domain.team.service;

import com.renew.sw.mentoring.domain.team.exception.AlreadyTeamException;
import com.renew.sw.mentoring.domain.team.exception.TeamNotFoundException;
import com.renew.sw.mentoring.domain.team.model.dto.list.SummarizedTeamDto;
import com.renew.sw.mentoring.domain.team.model.dto.response.ResponseTeamInfoDto;
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 lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Slf4j
public class TeamService {

private final TeamRepository teamRepository;
private final UserRepository userRepository;

/**
* 이미 존재하는 팀인지 확인
Expand All @@ -36,4 +44,24 @@ public Page<SummarizedTeamDto> getAllTeams(Pageable pageable) {
Page<Team> teams = teamRepository.findAllDesc(pageable);
return teams.map(SummarizedTeamDto::new);
}
}

/**
* 우리 팀 상세 조회
*
* @return 팀명, 팀 점수, 멘토, 멤버
*/
public ResponseTeamInfoDto getMyTeamInfo(Long userId) {
Team team = teamRepository.findByUserId(userId).orElseThrow(TeamNotFoundException::new);

String mentor = userRepository.findMentorByTeamId(team.getId()).orElseThrow(TeamNotFoundException::new).getName();

List<User> userList = userRepository.findTeamMemberByTeamId(team.getId());

List<String> members = new ArrayList<>();

for (User user : userList) {
members.add(user.getName());
}
return new ResponseTeamInfoDto(team, mentor, members);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
Expand All @@ -17,4 +18,10 @@ public interface UserRepository extends JpaRepository<User, Long> {

@Query("select u from User u where u.name = :name")
Optional<User> findByName(@Param("name") String name);

@Query("select u from User u where u.team.id = :teamId and u.userRole = 'MENTOR'")
Optional<User> findMentorByTeamId(@Param("teamId") Long teamId);

@Query("select u from User u where u.team.id = :teamId")
List<User> findTeamMemberByTeamId(@Param("teamId") Long teamId);
}
5 changes: 5 additions & 0 deletions src/test/java/com/renew/sw/mentoring/mock/UserAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public static void withMentor(Long userId) {
.setAuthentication(new JwtAuthentication(userId, UserRole.MENTOR));
}

public static void withMentee(Long userId) {
SecurityContextHolder.getContext()
.setAuthentication(new JwtAuthentication(userId, UserRole.MENTEE));
}

public static void withAdmin(Long userId) {
SecurityContextHolder.getContext()
.setAuthentication(new JwtAuthentication(userId, UserRole.ADMIN));
Expand Down

0 comments on commit e17c6bc

Please sign in to comment.