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

[Feat] 관리자 계정 조회 #80

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@
import org.haedal.zzansuni.auth.domain.AuthService;
import org.haedal.zzansuni.challengegroup.domain.application.ChallengeGroupService;
import org.haedal.zzansuni.core.api.ApiResponse;
import org.haedal.zzansuni.user.domain.UserModel;
import org.haedal.zzansuni.user.domain.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "admin", description = "관리자 API")
@RequiredArgsConstructor
@RestController
public class AdminController {

private final AuthService authService;
private final ChallengeGroupService challengeGroupService;
private final UserService userService;

@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "매니저 등록", description = "매니저를 등록한다.")
Expand All @@ -27,6 +32,14 @@ public ApiResponse<Void> createManager(@RequestBody @Valid AuthReq.EmailSignupRe
return ApiResponse.success(null, "매니저 등록 성공");
}

@ResponseStatus(HttpStatus.OK)
@Operation(summary = "매니저, 어드민 계정 조회", description = "매니저, 어드민 계정을 조회한다.")
@GetMapping("/api/admin/auth/manager")
public ApiResponse<List<AdminRes.ManagerAndAdmin>> getAdminAndManager() {
List<UserModel.Admin> managerAndAdmin = userService.getManagerAndAdmin();
return ApiResponse.success(AdminRes.ManagerAndAdmin.from(managerAndAdmin), "매니저, 어드민 계정 조회 성공");
}

@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "챌린지 그룹 생성", description = "챌린지 그룹과 해당하는 챌린지를 생성합니다")
@PostMapping("/api/admin/challengeGroups")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.haedal.zzansuni.admin.controller;

import lombok.Builder;
import org.haedal.zzansuni.user.domain.UserModel;

import java.time.LocalDateTime;
import java.util.List;

public class AdminRes {
@Builder
public record ManagerAndAdmin(
Long id,
String email,
String name,
String role,
LocalDateTime createdAt
) {
public static ManagerAndAdmin from(UserModel.Admin managerAndAdmin) {
return ManagerAndAdmin.builder()
.id(managerAndAdmin.id())
.email(managerAndAdmin.email())
.name(managerAndAdmin.nickname())
.role(managerAndAdmin.role().name())
.createdAt(managerAndAdmin.createdAt())
.build();
}
public static List<ManagerAndAdmin> from(List<UserModel.Admin> managerAndAdmins) {
return managerAndAdmins.stream()
.map(ManagerAndAdmin::from)
.toList();
}
Comment on lines +27 to +31
Copy link
Collaborator

Choose a reason for hiding this comment

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

List로 변환하는 함수라면 네이밍을 from이 아니라
fromModelAndConvertList와 같은 의도가 있는 네이밍이 좋지 않을까요?!

Copy link
Member Author

Choose a reason for hiding this comment

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

lgtm

}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.haedal.zzansuni.user.domain;

import lombok.Builder;
import org.haedal.zzansuni.global.security.Role;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -71,5 +73,23 @@ public String toString() {
}
}

@Builder
public record Admin(
Copy link
Collaborator

Choose a reason for hiding this comment

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

유저모델에 Role 추가하고, API response에서 바꾸는거는 어떻게생각하세요??
내정보조회에서만 role보이게하고, 다른 일반 api(랭킹조회 등등)에서는 role안보이게요!

백오피스에서 일반유저로 로그인시, 권한확인을 내정보확인에서 하면 깔끔할거같더라고요

Copy link
Member Author

@kwonssshyeon kwonssshyeon Sep 4, 2024

Choose a reason for hiding this comment

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

아 UserModel.Main -> response로 매핑할때 불필요한 정보를 빼거나 하자는 거죠 ?
좋습니다. 그러고보니까 지금 UserModel.Main이랑 UserModelAdmin이랑 필드가 완전히 겹치네요

이건 백오피스에서 관리자 계정 조회하기 위한 response인데 이때는 role이 필요하겠죠 ?

Long id,
String email,
String nickname,
LocalDateTime createdAt,
Role role
) {
public static Admin from(User user) {
return Admin.builder()
.id(user.getId())
.email(user.getEmail())
.nickname(user.getNickname())
.createdAt(user.getCreatedAt())
.role(user.getRole())
.build();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Optional;

public interface UserReader {
Expand All @@ -15,4 +16,5 @@ public interface UserReader {
Optional<User> findByAuthToken(String authToken);

Page<User> getUserPagingByRanking(Pageable pageable);
List<User> getManagerAndAdmin();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public class UserService {
private final UserReader userReader;
private final UserChallengeReader userChallengeReader;

@Transactional(readOnly = true)
public List<UserModel.Admin> getManagerAndAdmin() {
List<User> admins = userReader.getManagerAndAdmin();
return admins.stream()
.map(UserModel.Admin::from)
.toList();
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

일부로 getManagerAndAdmin으로 role로 찾지않고 특정해서 만든거 좋아보이네염! (유저의 경우 페이징이 필요하고 매니저와 admin은 필요없다고 판단하셔서 따로 만드신 의도 맞나염?)

Copy link
Member Author

Choose a reason for hiding this comment

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

넵 관리자 계정만 조회하는거라 레코드가 많지는 않을것 같아서 페이징 처리 안했어유


@Transactional(readOnly = true)
public UserModel.Main getUserModel(Long id) {
User user = userReader.getById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.global.security.Role;
import org.haedal.zzansuni.user.domain.QUser;
import org.haedal.zzansuni.user.domain.User;
import org.haedal.zzansuni.user.domain.UserReader;
Expand Down Expand Up @@ -54,4 +55,9 @@ public Page<User> getUserPagingByRanking(Pageable pageable) {

return new PageImpl<>(users, pageable, totalCount == null ? 0 : totalCount);
}

@Override
public List<User> getManagerAndAdmin() {
return userRepository.findByRoleIn(List.of(Role.ADMIN, Role.MANAGER));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.haedal.zzansuni.user.infrastructure;

import org.haedal.zzansuni.global.security.Role;
import org.haedal.zzansuni.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByAuthToken(String authToken);
boolean existsByEmail(String email);

Optional<User> findByEmail(String email);
List<User> findByRoleIn(List<Role> role);
}
Loading