Skip to content

Commit

Permalink
feat: 회원 클래스 수정 api
Browse files Browse the repository at this point in the history
  • Loading branch information
birdieHyun committed Oct 26, 2023
1 parent 65bb891 commit e6c7ac4
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/docs/asciidoc/api/users/users.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ include::{snippets}/find-all-users-doc/http-request.adoc[]
==== HTTP Response
include::{snippets}/find-all-users-doc/http-response.adoc[]
include::{snippets}/find-all-users-doc/response-fields-data.adoc[]

=== 회원 클래스 업데이트
==== HTTP Request
include::{snippets}/patch-userClass-doc/http-request.adoc[]
include::{snippets}/patch-userClass-doc/request-fields.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.*;
import yonseigolf.server.user.dto.request.KakaoCode;
import yonseigolf.server.user.dto.request.SignUpUserRequest;
import yonseigolf.server.user.dto.request.UserClassRequest;
import yonseigolf.server.user.dto.response.AdminResponse;
import yonseigolf.server.user.dto.response.KakaoLoginResponse;
import yonseigolf.server.user.dto.response.SessionUser;
import yonseigolf.server.user.dto.response.SingleUserResponse;
import yonseigolf.server.user.dto.token.KakaoOauthInfo;
import yonseigolf.server.user.dto.token.OauthToken;
import yonseigolf.server.user.entity.UserClass;
import yonseigolf.server.user.service.OauthLoginService;
import yonseigolf.server.user.service.UserService;
import yonseigolf.server.util.CustomResponse;
Expand Down Expand Up @@ -101,15 +101,29 @@ public ResponseEntity<CustomResponse> signUp(@RequestBody @Validated SignUpUserR
}

@GetMapping("/admin/users")
public ResponseEntity<CustomResponse<Page<SingleUserResponse>>> findAllUsers(Pageable pageable) {
public ResponseEntity<CustomResponse<Page<SingleUserResponse>>> findAllUsers(Pageable pageable, UserClass userClass) {

return ResponseEntity
.ok()
.body(new CustomResponse<>(
"success",
200,
"유저 정보 조회 성공",
userService.findAllUsers(pageable)
userService.findAllUsers(pageable, userClass)
));
}

@PatchMapping("/admin/users/{userId}")
public ResponseEntity<CustomResponse> updateUserClass(@PathVariable Long userId, @RequestBody UserClassRequest userClass) {

userService.updateUserClass(userId, userClass.getUserClass());

return ResponseEntity
.ok()
.body(new CustomResponse(
"success",
200,
"유저 정보 수정 성공"
));
}

Expand All @@ -127,7 +141,7 @@ public ResponseEntity<CustomResponse<AdminResponse>> getLeaders() {
}

@GetMapping("/healthcheck")
public ResponseEntity<CustomResponse> healthCheck() {
public ResponseEntity<CustomResponse> healthCheck() {

return ResponseEntity
.ok()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package yonseigolf.server.user.dto.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import yonseigolf.server.user.entity.UserClass;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class UserClassRequest {

private UserClass userClass;
}
5 changes: 5 additions & 0 deletions src/main/java/yonseigolf/server/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public static User of(SignUpUserRequest request, Long kakaoId) {
.userClass(UserClass.NONE)
.build();
}

public void updateUserClass(UserClass userClass) {

this.userClass = userClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import yonseigolf.server.user.dto.response.SingleUserResponse;
import yonseigolf.server.user.entity.UserClass;


public interface UserRepositoryCustom {

Page<SingleUserResponse> findAllUsers(Pageable pageable);
Page<SingleUserResponse> findAllUsers(Pageable pageable, UserClass userClass);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.data.domain.Pageable;
import yonseigolf.server.user.dto.response.SingleUserResponse;
import yonseigolf.server.user.entity.User;
import yonseigolf.server.user.entity.UserClass;

import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -20,9 +21,10 @@ public class UserRepositoryImpl implements UserRepositoryCustom {
private final JPAQueryFactory queryFactory;

@Override
public Page<SingleUserResponse> findAllUsers(Pageable pageable) {
public Page<SingleUserResponse> findAllUsers(Pageable pageable, UserClass userClass) {

QueryResults<User> results = queryFactory.selectFrom(user)
.where(user.userClass.eq(userClass))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetchResults();
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/yonseigolf/server/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yonseigolf.server.user.dto.request.SignUpUserRequest;
import yonseigolf.server.user.dto.response.AdminResponse;
import yonseigolf.server.user.dto.response.SessionUser;
import yonseigolf.server.user.dto.response.SingleUserResponse;
import yonseigolf.server.user.dto.response.UserResponse;
import yonseigolf.server.user.entity.User;
import yonseigolf.server.user.entity.UserClass;
import yonseigolf.server.user.entity.UserRole;
import yonseigolf.server.user.repository.UserRepository;

Expand Down Expand Up @@ -51,9 +53,16 @@ public AdminResponse getLeaders() {
return AdminResponse.of(UserResponse.fromUser(leader), assistantLeaders);
}

public Page<SingleUserResponse> findAllUsers(Pageable pageable) {
public Page<SingleUserResponse> findAllUsers(Pageable pageable, UserClass userClass) {

return repository.findAllUsers(pageable);
return repository.findAllUsers(pageable, userClass);
}

@Transactional
public void updateUserClass(Long userId, UserClass userClass) {

User user = findById(userId);
user.updateUserClass(userClass);
}

private User findById(Long id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package yonseigolf.server.user.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -13,6 +14,7 @@
import yonseigolf.server.docs.utils.RestDocsSupport;
import yonseigolf.server.user.dto.request.KakaoCode;
import yonseigolf.server.user.dto.request.SignUpUserRequest;
import yonseigolf.server.user.dto.request.UserClassRequest;
import yonseigolf.server.user.dto.response.*;
import yonseigolf.server.user.dto.token.KakaoOauthInfo;
import yonseigolf.server.user.dto.token.OauthToken;
Expand All @@ -24,10 +26,11 @@
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -222,11 +225,11 @@ void findAllUserTest() throws Exception {
);

Page<SingleUserResponse> mockPage = new PageImpl<>(users);
given(userService.findAllUsers(any())).willReturn(mockPage);
given(userService.findAllUsers(any())).willReturn(mockPage);
given(userService.findAllUsers(any(), any())).willReturn(mockPage);
given(userService.findAllUsers(any(), any())).willReturn(mockPage);

// when
userService.findAllUsers(null);
userService.findAllUsers(any(), any());

// then
mockMvc.perform(get("/admin/users")
Expand Down Expand Up @@ -282,6 +285,29 @@ void findAllUserTest() throws Exception {
.description("페이지가 비어있는지 여부"))));
}

@Test
@DisplayName("사용자의 class를 업데이트 할 수 있다.")
void updateUserClassTest() throws Exception {
// given
Long userId = 1L;
UserClassRequest request = new UserClassRequest(UserClass.YB);
doNothing().when(userService).updateUserClass(any(), any());

// when

// then
mockMvc.perform(patch("/admin/users/" + userId)
.contentType(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(request)))
.andDo(print())
.andExpect(status().isOk())
.andDo(document("patch-userClass-doc",
requestFields(
fieldWithPath("userClass").type(JsonFieldType.STRING)
.description("유저 구분 (YB, OB, NONE 중 하나)")
)
));
}

@Override
protected Object initController() {
Expand Down

0 comments on commit e6c7ac4

Please sign in to comment.