Skip to content

Commit

Permalink
Merge pull request #63 from everymeals/feature/user-info-get
Browse files Browse the repository at this point in the history
[Feature/user info get] 마이페이지 - 사용자 프로필 조회 API
  • Loading branch information
dldmsql authored Nov 25, 2023
2 parents 3f5698a + cbd7fff commit 010728e
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 5 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/ci_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ on:
pull_request:
branches:
- develop

permissions: write-all

jobs:
sonarcloud:
name: SonarCloud
Expand Down Expand Up @@ -46,12 +49,13 @@ jobs:
touch ./application-secret.yml
echo "${{ secrets.PROPERTIES_SECRET }}" > ./application-secret.yml
shell: bash

- name: Make test application.yml
run: |
cd ./src/test/resources
touch ./application.yml
echo "${{ secrets.PROPERTIES_SECRET_TEST }}" > ./application.yml
shell: bash
run: |
mkdir -p src/test/resources
echo "${{ secrets.PROPERTIES_SECRET_TEST }}" | base64 --decode > src/test/resources/application.yml
# shell: bash

- name: Build and analyze
run: ./gradlew test sonar --info
env:
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/everymeal/server/user/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@


import everymeal.server.global.dto.response.ApplicationResponse;
import everymeal.server.global.util.authresolver.Auth;
import everymeal.server.global.util.authresolver.AuthUser;
import everymeal.server.global.util.authresolver.entity.AuthenticatedUser;
import everymeal.server.user.controller.dto.request.UserEmailAuthReq;
import everymeal.server.user.controller.dto.request.UserEmailLoginReq;
import everymeal.server.user.controller.dto.request.UserEmailSingReq;
import everymeal.server.user.controller.dto.response.UserEmailAuthRes;
import everymeal.server.user.controller.dto.response.UserLoginRes;
import everymeal.server.user.controller.dto.response.UserProfileRes;
import everymeal.server.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -130,6 +136,15 @@ public ApplicationResponse<Boolean> checkUser(
return ApplicationResponse.ok(userService.checkUser(email));
}

@Auth(require = true)
@GetMapping("/profile")
@SecurityRequirement(name = "jwt-user-auth")
@Operation(summary = "인증된 사용자의 프로필 정보 조회", description = "인증된 사용자의 프로필 정보를 조회합니다.")
public ApplicationResponse<UserProfileRes> getUserProfile(
@Parameter(hidden = true) @AuthUser AuthenticatedUser authenticatedUser) {
return ApplicationResponse.ok(userService.getUserProfile(authenticatedUser));
}

private ResponseEntity<ApplicationResponse<UserLoginRes>> setRefreshToken(
UserLoginRes response) {
ResponseCookie cookie =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package everymeal.server.user.controller.dto.response;


import java.util.Map;

public record UserProfileRes(
Long userId, String nickName, String profileImgUrl, String universityName) {
public static UserProfileRes of(Map<String, Object> user) {
return new UserProfileRes(
(Long) user.get("userId"),
(String) user.get("nickName"),
(String) user.get("profileImgUrl"),
(String) user.get("universityName"));
}
}
13 changes: 13 additions & 0 deletions src/main/java/everymeal/server/user/repository/UserMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package everymeal.server.user.repository;


import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface UserMapper {
Map<String, Object> getUserProfile(@Param("idx") Long idx);
}
4 changes: 4 additions & 0 deletions src/main/java/everymeal/server/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package everymeal.server.user.service;


import everymeal.server.global.util.authresolver.entity.AuthenticatedUser;
import everymeal.server.user.controller.dto.request.UserEmailAuthReq;
import everymeal.server.user.controller.dto.request.UserEmailLoginReq;
import everymeal.server.user.controller.dto.request.UserEmailSingReq;
import everymeal.server.user.controller.dto.response.UserEmailAuthRes;
import everymeal.server.user.controller.dto.response.UserLoginRes;
import everymeal.server.user.controller.dto.response.UserProfileRes;

public interface UserService {

Expand All @@ -18,4 +20,6 @@ public interface UserService {
Boolean verifyEmailAuth(String emailAuthToken, String emailAuthValue);

Boolean checkUser(String email);

UserProfileRes getUserProfile(AuthenticatedUser authenticatedUser);
}
11 changes: 11 additions & 0 deletions src/main/java/everymeal/server/user/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import everymeal.server.global.exception.ExceptionList;
import everymeal.server.global.util.JwtUtil;
import everymeal.server.global.util.MailUtil;
import everymeal.server.global.util.authresolver.entity.AuthenticatedUser;
import everymeal.server.global.util.aws.S3Util;
import everymeal.server.university.entity.University;
import everymeal.server.university.repository.UniversityRepository;
Expand All @@ -13,10 +14,13 @@
import everymeal.server.user.controller.dto.request.UserEmailSingReq;
import everymeal.server.user.controller.dto.response.UserEmailAuthRes;
import everymeal.server.user.controller.dto.response.UserLoginRes;
import everymeal.server.user.controller.dto.response.UserProfileRes;
import everymeal.server.user.entity.User;
import everymeal.server.user.repository.UserMapper;
import everymeal.server.user.repository.UserRepository;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Map;
import java.util.Random;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -32,6 +36,7 @@ public class UserServiceImpl implements UserService {
private final JwtUtil jwtUtil;
private final MailUtil mailUtil;
private final S3Util s3Util;
private final UserMapper userMapper;

@Override
@Transactional
Expand Down Expand Up @@ -144,4 +149,10 @@ public Boolean verifyEmailAuth(String emailAuthToken, String emailAuthValue) {
public Boolean checkUser(String email) {
return userRepository.findByEmail(email).isPresent();
}

@Override
public UserProfileRes getUserProfile(AuthenticatedUser authenticatedUser) {
Map<String, Object> result = userMapper.getUserProfile(authenticatedUser.getIdx());
return UserProfileRes.of(result);
}
}
16 changes: 16 additions & 0 deletions src/main/resources/mybatis/mappers/UserMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="everymeal.server.user.repository.UserMapper">

<select id="getUserProfile">
SELECT
u.idx as userId,
u.nickname as nickName,
u.profile_img_url as profileImgUrl,
u2.name as universityName
FROM users u
INNER JOIN university u2 ON u.university_idx = u2.idx AND u2.is_deleted = false
WHERE u.is_deleted = false AND u.idx = #{idx}
</select>

</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import everymeal.server.global.ControllerTestSupport;
import everymeal.server.global.util.authresolver.UserJwtResolver;
import everymeal.server.global.util.authresolver.entity.AuthenticatedUser;
import everymeal.server.user.controller.dto.request.UserEmailAuthReq;
import everymeal.server.user.controller.dto.request.UserEmailLoginReq;
import everymeal.server.user.controller.dto.request.UserEmailSingReq;
import everymeal.server.user.controller.dto.response.UserLoginRes;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;

class UserControllerTest extends ControllerTestSupport {
@Mock UserJwtResolver userJwtResolver;

@DisplayName("회원가입을 진행한다.")
@Test
Expand Down Expand Up @@ -117,4 +121,17 @@ void checkUser() throws Exception {
.andExpect(status().isOk())
.andExpect(jsonPath("$.message").value("OK"));
}

@DisplayName("인증된 유저의 프로필 정보 조회")
@Test
void getUserProfile() throws Exception {
// given
given(userJwtResolver.resolveArgument(any(), any(), any(), any()))
.willReturn(AuthenticatedUser.builder().idx(1L).build());
// when-then
mockMvc.perform(get("/api/v1/users/profile").contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.message").value("OK"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import everymeal.server.global.exception.ExceptionList;
import everymeal.server.global.util.JwtUtil;
import everymeal.server.global.util.MailUtil;
import everymeal.server.global.util.authresolver.entity.AuthenticatedUser;
import everymeal.server.global.util.aws.S3Util;
import everymeal.server.university.entity.University;
import everymeal.server.university.repository.UniversityRepository;
Expand Down Expand Up @@ -224,6 +225,30 @@ void checkUser() {
assertThat(response).isTrue();
}

@DisplayName("인증된 유저의 프로필 확인")
@Test
void getUserProfile() {
// given
String token = jwtUtil.generateEmailToken("[email protected]", "12345");

University university =
universityRepository.save(
University.builder().name("명지대학교").campusName("인문캠퍼스").build());
UserEmailSingReq request =
new UserEmailSingReq("nickname", token, "12345", university.getIdx(), "imageKey");

UserLoginRes userLoginRes = userService.signUp(request);

AuthenticatedUser user =
jwtUtil.getAuthenticateUserFromAccessToken(userLoginRes.accessToken());

// when
var response = userService.getUserProfile(user);

// then
assertEquals(response.nickName(), request.nickname());
}

private User createUser(String email, String nickname) {
return User.builder().email(email).nickname(nickname).build();
}
Expand Down

0 comments on commit 010728e

Please sign in to comment.