Skip to content

Commit

Permalink
feat: 회원만 조회 가능ã�한 유ì저 API (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
tfer2442 committed Aug 10, 2024
1 parent bde9abb commit ac5e582
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
.requestMatchers(HttpMethod.GET, "/posts/generate-presigned-url").hasAnyRole("WEB_MASTER", "ADMIN", "TEAM_LEADER", "MEMBER")
.requestMatchers(HttpMethod.POST, "/notice/posts", "/event/posts").hasAnyRole("WEB_MASTER", "ADMIN")
.requestMatchers(HttpMethod.POST, "/activities/{activityId}/boards").hasAnyRole("WEB_MASTER", "ADMIN", "TEAM_LEADER")
.requestMatchers(HttpMethod.POST, "/boards/{boardId}/posts").hasAnyRole("WEB_MASTER", "ADMIN", "TEAM_LEADER", "MEMBER")
.requestMatchers(HttpMethod.POST, "/boards/{boardId}/posts").authenticated()
.requestMatchers(HttpMethod.DELETE, "/activities/{activityId}/boards/{boardId}").hasAnyRole("WEB_MASTER", "ADMIN", "TEAM_LEADER")
.requestMatchers(HttpMethod.PATCH, "/activities/{activityId}/boards/{boardId}/**").hasAnyRole("WEB_MASTER", "ADMIN", "TEAM_LEADER")
.requestMatchers("/private/users").authenticated()
.requestMatchers("/activities/{activityId}/boards","/activities/{activityId}/boards/{boardId}","/login", "/", "/join/**", "/reissue", "/swagger-ui/**", "/v3/api-docs/**", "/users/**","/semesters/**").permitAll()
.anyRequest().authenticated());

Expand Down
66 changes: 40 additions & 26 deletions src/main/java/com/haedal/haedalweb/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
//package com.haedal.haedalweb.controller;
package com.haedal.haedalweb.controller;

import com.haedal.haedalweb.dto.response.user.PrivateUserDTO;
import com.haedal.haedalweb.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "유저 API")
@RequiredArgsConstructor
@RestController
public class UserController {
private final UserService userService;
// @Operation(summary = "User 목록")
// @Parameters({
// @Parameter(name = "page", description = "현재 페이지"),
// @Parameter(name = "size", description = "한 페이지에 노출할 데이터 수")
// })
// @GetMapping
// public ResponseEntity<Page<AdminUserDTO>> getUser(@RequestParam(value = "page", defaultValue = "0") int page,
// @RequestParam(value = "size", defaultValue = "5") int size){
// Page<AdminUserDTO> activeUsers;
// activeUsers = userService.getUsers(PageRequest.of(page, size, Sort.by(Order.asc("role"), Order.asc("name"))));
//
//import io.swagger.v3.oas.annotations.tags.Tag;
//import lombok.RequiredArgsConstructor;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
////
////@Tag(name = "User API")
////@RequestMapping("/users")
////@RequiredArgsConstructor
////@RestController
////public class UserController {
////
////// @Operation(summary = "User 목록")
////// @Parameters({
////// @Parameter(name = "page", description = "현재 페이지"),
////// @Parameter(name = "size", description = "한 페이지에 노출할 데이터 수")
////// })
////// @GetMapping
////// public ResponseEntity<Page<AdminUserDTO>> getUser(@RequestParam(value = "page", defaultValue = "0") int page,
////// @RequestParam(value = "size", defaultValue = "5") int size){
////// Page<AdminUserDTO> activeUsers;
////// activeUsers = userService.getUsers(PageRequest.of(page, size, Sort.by(Order.asc("role"), Order.asc("name"))));
//////
////// return ResponseEntity.ok(activeUsers);
////// }
////}
// return ResponseEntity.ok(activeUsers);
// }

@Operation(summary = "User 목록 (학번 포함)")
@GetMapping("/private/users")
public ResponseEntity<List<PrivateUserDTO>> getUser(){
List<PrivateUserDTO> users = userService.getUsers();

return ResponseEntity.ok(users);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.haedal.haedalweb.constants.ErrorCode;
import com.haedal.haedalweb.constants.SuccessCode;
import com.haedal.haedalweb.domain.UserStatus;
import com.haedal.haedalweb.dto.response.AdminUserDTO;
import com.haedal.haedalweb.dto.response.user.AdminUserDTO;
import com.haedal.haedalweb.dto.request.UpdateRoleDTO;
import com.haedal.haedalweb.dto.response.common.SuccessResponse;
import com.haedal.haedalweb.service.admin.AdminUserService;
Expand Down Expand Up @@ -38,7 +38,7 @@ public class AdminUserController {
@Operation(summary = "User 목록")
@Parameter(name = "active", description = "활동 유저 true, 가입대기 유저 false")
@GetMapping
public ResponseEntity<List<AdminUserDTO>> getActiveUser(@RequestParam Boolean active){
public ResponseEntity<List<AdminUserDTO>> getUser(@RequestParam Boolean active){
List<AdminUserDTO> users = null;

if (active) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.haedal.haedalweb.dto.response;
package com.haedal.haedalweb.dto.response.user;

import java.time.LocalDateTime;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.haedal.haedalweb.dto.response.user;

import java.time.LocalDateTime;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PrivateUserDTO {
@Schema(description = "유저 아이디", example = "haedal12")
private String userId;

@Schema(description = "유저 학번", example = "2024111234")
private Integer studentNumber;

@Schema(description = "유저 이름", example = "조대성")
private String userName;
}
19 changes: 19 additions & 0 deletions src/main/java/com/haedal/haedalweb/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import com.haedal.haedalweb.constants.ErrorCode;
import com.haedal.haedalweb.domain.User;
import com.haedal.haedalweb.domain.UserStatus;
import com.haedal.haedalweb.dto.CustomUserDetails;
import com.haedal.haedalweb.dto.response.user.PrivateUserDTO;
import com.haedal.haedalweb.exception.BusinessException;
import com.haedal.haedalweb.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand All @@ -32,4 +35,20 @@ public User getLoggedInUser() {

return findUserById(userId);
}

public List<PrivateUserDTO> getUsers() {
List<User> users = userRepository.findByUserStatus(UserStatus.ACTIVE, null);

return users.stream()
.map(this::convertToPrivateUserDTO)
.collect(Collectors.toList());
}

private PrivateUserDTO convertToPrivateUserDTO(User user) {
return PrivateUserDTO.builder()
.userId(user.getId())
.studentNumber(user.getStudentNumber())
.userName(user.getName())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.haedal.haedalweb.domain.Role;
import com.haedal.haedalweb.domain.User;
import com.haedal.haedalweb.domain.UserStatus;
import com.haedal.haedalweb.dto.response.AdminUserDTO;
import com.haedal.haedalweb.dto.response.user.AdminUserDTO;
import com.haedal.haedalweb.exception.BusinessException;
import com.haedal.haedalweb.repository.UserRepository;
import lombok.RequiredArgsConstructor;
Expand Down

0 comments on commit ac5e582

Please sign in to comment.