Skip to content

Commit

Permalink
Merge pull request #97 from gutanbug/dev
Browse files Browse the repository at this point in the history
fix : 사용자들의 정보를 간단하게 볼 수 있는 API 추가
  • Loading branch information
gutanbug authored Apr 11, 2024
2 parents fbea03e + a641ee1 commit 29db5f5
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.renew.sw.mentoring.domain.user.controller;

import com.renew.sw.mentoring.domain.user.model.UserInfo;
import com.renew.sw.mentoring.domain.user.model.dto.request.*;
import com.renew.sw.mentoring.domain.user.model.dto.response.ResponseLoginDto;
import com.renew.sw.mentoring.domain.user.model.dto.response.ResponseReissueDto;
import com.renew.sw.mentoring.domain.user.model.dto.response.ResponseSimpleUserInfoDto;
import com.renew.sw.mentoring.domain.user.model.dto.response.ResponseUserInfoDto;
import com.renew.sw.mentoring.domain.user.service.UserInfoService;
import com.renew.sw.mentoring.domain.user.service.UserService;
Expand Down Expand Up @@ -36,6 +38,17 @@ public ResponseUserInfoDto getMyInfo(AppAuthentication auth) {
return userInfoService.getFullUserInfo(auth.getUserId());
}

/**
* 유저 정보 조회
*
* @param dto 요청 body
*/
@PostMapping("/info")
public ResponseSimpleUserInfoDto getUserInfo(@RequestBody RequestSimpleUserInfoDto dto) {
UserInfo result = userInfoService.getUserInfo(dto);
return new ResponseSimpleUserInfoDto(result);
}

/**
* 로그인
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.List;

@Getter
@RequiredArgsConstructor
@EqualsAndHashCode
Expand All @@ -13,11 +15,15 @@ public class UserInfo {
private final String nickname;
private final String studentId;
private final String teamName;
private final String mentorName;
private final List<String> teamMembers;

public UserInfo(User user) {
public UserInfo(User user, String mentorName, List<String> teamMembers) {
this.name = user.getName();
this.nickname = user.getNickname();
this.studentId = user.getStudentId();
this.teamName = user.getTeam().getTeamName();
this.mentorName = mentorName;
this.teamMembers = teamMembers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.renew.sw.mentoring.domain.user.model.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class RequestSimpleUserInfoDto {

@Schema(description = "닉네임", example = "nickname")
private String nickname;

public RequestSimpleUserInfoDto(String nickname) {
this.nickname = nickname;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.renew.sw.mentoring.domain.user.model.dto.response;

import com.renew.sw.mentoring.domain.user.model.UserInfo;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.List;

@Getter
@RequiredArgsConstructor
public class ResponseSimpleUserInfoDto {

private final String name;
private final String studentId;
private final String nickname;
private final String teamName;
private final String mentorName;
private final List<String> teamMembers;

public ResponseSimpleUserInfoDto(UserInfo userInfo) {
this.name = userInfo.getName();
this.studentId = userInfo.getStudentId();
this.nickname = userInfo.getNickname();
this.teamName = userInfo.getTeamName();
this.mentorName = userInfo.getMentorName();
this.teamMembers = userInfo.getTeamMembers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ public interface UserInfoMemoryRepository {

Optional<UserInfo> getUserInfo(Long userId, Instant now);

Optional<UserInfo> getUserInfoWithNickname(String nickname, Instant now);

void setUserInfo(Long userId, UserInfo userInfo, Instant now);

void setUserInfoWithNickname(String nickname, UserInfo userInfo, Instant now);

void removeUserInfo(Long userId);

void removeUserInfoWithNickname(String nickname);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,28 @@ public Optional<UserInfo> getUserInfo(Long userId, Instant now) {
return get(userId.toString(), UserInfo.class, now);
}

@Override
public Optional<UserInfo> getUserInfoWithNickname(String nickname, Instant now) {
return get(nickname, UserInfo.class, now);
}

@Override
public void setUserInfo(Long userId, UserInfo userInfo, Instant now) {
set(userId.toString(), userInfo, now, cacheDuration);
}

@Override
public void setUserInfoWithNickname(String nickname, UserInfo userInfo, Instant now) {
set(nickname, userInfo, now, cacheDuration);
}

@Override
public void removeUserInfo(Long userId) {
remove(userId.toString());
}

@Override
public void removeUserInfoWithNickname(String nickname) {
remove(nickname);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.renew.sw.mentoring.domain.user.exception.UserNotFoundException;
import com.renew.sw.mentoring.domain.user.model.UserInfo;
import com.renew.sw.mentoring.domain.user.model.dto.request.RequestSimpleUserInfoDto;
import com.renew.sw.mentoring.domain.user.model.dto.response.ResponseUserInfoDto;
import com.renew.sw.mentoring.domain.user.model.entity.User;
import com.renew.sw.mentoring.domain.user.repository.UserInfoMemoryRepository;
Expand All @@ -13,6 +14,7 @@

import java.time.Clock;
import java.time.Instant;
import java.util.List;

@Service
@RequiredArgsConstructor
Expand All @@ -32,14 +34,18 @@ public ResponseUserInfoDto getFullUserInfo(Long userId) {
}

@Transactional(readOnly = true)
public UserInfo getUserInfo(Long userId) {
public UserInfo getUserInfo(RequestSimpleUserInfoDto dto) {
Instant now = Instant.now(clock);
return memoryRepository.getUserInfo(userId, now)
return memoryRepository.getUserInfoWithNickname(dto.getNickname(), now)
.orElseGet(() -> {
User user = persistenceRepository.findById(userId)
User user = persistenceRepository.findByNickname(dto.getNickname())
.orElseThrow(UserNotFoundException::new);
UserInfo userInfo = new UserInfo(user);
memoryRepository.setUserInfo(userId, userInfo, now);
List<String> teamMembers = persistenceRepository.findTeamMenteeByTeamId(user.getTeam().getId())
.stream().map(User::getName).toList();
String mentorName = persistenceRepository.findMentorByTeamId(user.getTeam().getId())
.orElseThrow(UserNotFoundException::new).getName();
UserInfo userInfo = new UserInfo(user, mentorName, teamMembers);
memoryRepository.setUserInfoWithNickname(dto.getNickname(), userInfo, now);
return userInfo;
});
}
Expand All @@ -48,8 +54,16 @@ public void invalidateUserInfo(Long userId) {
memoryRepository.removeUserInfo(userId);
}

public void cacheUserInfo(Long userId, User user) {
UserInfo userInfo = new UserInfo(user);
memoryRepository.setUserInfo(userId, userInfo, Instant.now(clock));
public void invalidateUserInfoWithNickname(String nickname) {
memoryRepository.removeUserInfoWithNickname(nickname);
}

public void cacheUserInfo(String nickname, User user) {
List<String> teamMembers = persistenceRepository.findTeamMenteeByTeamId(user.getTeam().getId())
.stream().map(User::getName).toList();
String mentorName = persistenceRepository.findMentorByTeamId(user.getTeam().getId())
.orElseThrow(UserNotFoundException::new).getName();
UserInfo userInfo = new UserInfo(user, mentorName, teamMembers);
memoryRepository.setUserInfoWithNickname(nickname, userInfo, Instant.now(clock));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.renew.sw.mentoring.domain.user.service;


import com.renew.sw.mentoring.domain.team.exception.TeamNotFoundException;
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.exception.AlreadyNicknameException;
import com.renew.sw.mentoring.domain.user.exception.UserNotFoundException;
import com.renew.sw.mentoring.domain.user.exception.WrongPasswordException;
import com.renew.sw.mentoring.domain.user.model.UserRole;
import com.renew.sw.mentoring.domain.user.model.dto.request.*;
import com.renew.sw.mentoring.domain.user.model.dto.response.ResponseLoginDto;
import com.renew.sw.mentoring.domain.user.model.dto.response.ResponseReissueDto;
Expand Down Expand Up @@ -50,7 +47,7 @@ public ResponseLoginDto login(RequestLoginDto dto) {

if (passwordEncoder.matches(dto.getPassword(), user.getPassword())) {
AuthenticationToken token = jwtProvider.issue(user);
userInfoService.cacheUserInfo(user.getId(), user);
userInfoService.cacheUserInfo(user.getNickname(), user);
return new ResponseLoginDto(token);
} else {
throw new WrongPasswordException();
Expand Down Expand Up @@ -78,7 +75,7 @@ public void changeNickname(Long userId, RequestNickNameChangeDto dto) {
User user = findUser(userId);
checkAlreadyNickname(dto.getNickname());
user.changeNickName(dto.getNickname());
userInfoService.invalidateUserInfo(userId);
userInfoService.invalidateUserInfoWithNickname(user.getNickname());
}

@Transactional
Expand All @@ -87,7 +84,7 @@ public void changePassword(Long userId, RequestExistPasswordChangeDto dto) {
if (passwordEncoder.matches(dto.getPassword(), user.getPassword())) {
String encodedPassword = passwordEncoder.encode(dto.getNewPassword());
user.changePassword(encodedPassword);
userInfoService.invalidateUserInfo(userId);
userInfoService.invalidateUserInfoWithNickname(user.getNickname());
} else {
throw new WrongPasswordException();
}
Expand Down

0 comments on commit 29db5f5

Please sign in to comment.