Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor#99 멤버 조회 방식 변경 및 추가 #99

Merged
merged 11 commits into from
Jan 5, 2025
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package leets.weeth.domain.user.application.exception;

import leets.weeth.global.common.exception.BusinessLogicException;

public class InvalidUserOrderException extends BusinessLogicException {
public InvalidUserOrderException() {super(400, "올바른 유저 조회 순서가 아닙니다.");}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package leets.weeth.domain.user.application.exception;

import leets.weeth.global.common.exception.BusinessLogicException;

public class StatusNotFoundException extends BusinessLogicException {
public StatusNotFoundException() {
super(400, "존재하지 않는 status 입니다.");
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package leets.weeth.domain.user.application.usecase;

import leets.weeth.domain.user.application.dto.response.UserResponseDto;
import leets.weeth.domain.user.domain.entity.enums.UsersOrderBy;

import java.util.List;

public interface UserManageUseCase {


List<UserResponseDto.AdminResponse> findAllByAdmin();
List<UserResponseDto.AdminResponse> findAllByAdmin(UsersOrderBy orderBy);

void accept(Long userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import leets.weeth.domain.attendance.domain.service.AttendanceSaveService;
import leets.weeth.domain.schedule.domain.entity.Meeting;
import leets.weeth.domain.schedule.domain.service.MeetingGetService;
import leets.weeth.domain.user.application.exception.InvalidUserOrderException;
import leets.weeth.domain.user.application.mapper.UserMapper;
import leets.weeth.domain.user.domain.entity.User;
import leets.weeth.domain.user.domain.entity.enums.StatusPriority;
import leets.weeth.domain.user.domain.entity.enums.UsersOrderBy;
import leets.weeth.domain.user.domain.service.UserDeleteService;
import leets.weeth.domain.user.domain.service.UserGetService;
import leets.weeth.domain.user.domain.service.UserUpdateService;
Expand All @@ -14,9 +17,13 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;
import java.util.stream.Collectors;

import static leets.weeth.domain.user.application.dto.response.UserResponseDto.AdminResponse;
import static leets.weeth.domain.user.domain.entity.enums.UsersOrderBy.*;

@Service
@RequiredArgsConstructor
Expand All @@ -34,10 +41,20 @@ public class UserManageUseCaseImpl implements UserManageUseCase {
private final PasswordEncoder passwordEncoder;

@Override
public List<AdminResponse> findAllByAdmin() {
return userGetService.findAll().stream()
public List<AdminResponse> findAllByAdmin(UsersOrderBy orderBy) {
if(orderBy == null || !EnumSet.allOf(UsersOrderBy.class).contains(orderBy)){
throw new InvalidUserOrderException();
}

if(orderBy.equals(NAME_ASCENDING)){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

개행 한 줄 부탁드릴게요!

return userGetService.findAll().stream()
.sorted(Comparator.comparingInt((user->(StatusPriority.fromStatus(user.getStatus())).getPriority())))
.map(mapper::toAdminResponse)
.toList();
}
// To do : 추후 기수 분리 후 작업 예정

return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package leets.weeth.domain.user.domain.entity.enums;

import leets.weeth.domain.user.application.exception.StatusNotFoundException;
import lombok.Getter;

@Getter
public enum StatusPriority {
ACTIVE(1),
WAITING(2),
LEFT(3),
BANNED(4);

private final int priority;

StatusPriority(int priority) {
this.priority = priority;
}

public static StatusPriority fromStatus(Status status) {
if (status == null) {
throw new StatusNotFoundException();
}
return StatusPriority.valueOf(status.name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package leets.weeth.domain.user.domain.entity.enums;

public enum UsersOrderBy {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

쿼리 파라미터의 경우 대문자로 입력 받기 보다는 소문자로 받아주는게 좋을 것 같아요!
URL에 노출되는 부분이라서

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음.. enum을 소문자로 관리하기도 좀 그렇긴 한데.. 그냥 대문자로 두는게 나을 수도 잇겟네요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

orderBy로 받고있습니다!

NAME_ASCENDING, // 이름순 정렬
CARDINAL_DESCENDING; // 기수 기준으로 내림차순 정렬
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
boolean existsByTelAndIdIsNot(String tel, Long id);

List<User> findAllByStatusOrderByName(Status status);
List<User> findAllByOrderByNameAsc();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 부분이 현재는 단순히 이름으로 정렬된 모든 유저를 반환해주는 로직으로 이해해서 문제는 없을거같은데,
추후에 페이징(Pageable)을 적용해주는 방식도 고민해주셨으면 좋겠습니다


@Query(value = "SELECT * FROM users u WHERE JSON_CONTAINS(u.cardinals, CAST(:cardinal AS JSON), '$')", nativeQuery = true)
List<User> findByCardinal(@Param("cardinal") int cardinal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public List<User> findAllByCardinal(int cardinal) {
}

public List<User> findAll() {
return userRepository.findAll();
return userRepository.findAllByOrderByNameAsc();
}

public boolean validateStudentId(String studentId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import leets.weeth.domain.user.application.usecase.UserManageUseCase;
import leets.weeth.domain.user.domain.entity.enums.UsersOrderBy;
import leets.weeth.global.common.response.CommonResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
Expand All @@ -22,8 +23,8 @@ public class UserAdminController {

@GetMapping("/all")
@Operation(summary = "어드민용 회원 조회")
public CommonResponse<List<AdminResponse>> findAll() {
return CommonResponse.createSuccess(USER_FIND_ALL_SUCCESS.getMessage(), userManageUseCase.findAllByAdmin());
public CommonResponse<List<AdminResponse>> findAll(@RequestParam UsersOrderBy orderBy) {
return CommonResponse.createSuccess(USER_FIND_ALL_SUCCESS.getMessage(), userManageUseCase.findAllByAdmin(orderBy));
}

@PatchMapping
Expand Down
Loading