Skip to content

Commit

Permalink
feat: 일반 관리자 기능 구현 (#14)
Browse files Browse the repository at this point in the history
* fix: 도메인 관련 문제 수정
- Group 엔티티 명을 Role 로 변경
- ManyToOne Fetch Type 을 Lazy로 설정
- Member 엔티티 오타 수정

* feat: 기본 Repository 정의

* feat: Controller 및 Service 기본 로직 구현

* feat: Response dto 정의

---------

Co-authored-by: Kyunghun Kim <[email protected]>
  • Loading branch information
kimday0326 and KarmaPol authored Dec 24, 2023
1 parent cc718db commit a702524
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.pgms.apimember.controller;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.pgms.apimember.dto.response.AdminGetResponse;
import com.pgms.apimember.service.AdminService;
import com.pgms.coredomain.response.ApiResponse;

import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
public class AdminController {

private static final Long TEMP_CURRENT_ID = 1L;

private final AdminService adminService;

@GetMapping("/members")
public ResponseEntity<ApiResponse<?>> getMembers(@RequestParam(required = false) List<Long> memberIds) {
return ResponseEntity.ok(ApiResponse.ok(adminService.getMembers(memberIds)));
}

@GetMapping("/me")
public ResponseEntity<ApiResponse<AdminGetResponse>> getMyInfo() {
return ResponseEntity.ok(ApiResponse.ok(adminService.getAdmin(TEMP_CURRENT_ID)));
}

@DeleteMapping("/me")
public ResponseEntity<ApiResponse<Void>> deleteMyAccount() {
adminService.deleteAdmin(TEMP_CURRENT_ID);
return ResponseEntity.ok(ApiResponse.ok(null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.pgms.apimember.dto.response;

import java.time.LocalDateTime;

import com.pgms.coredomain.domain.member.Admin;
import com.pgms.coredomain.domain.member.Role;
import com.pgms.coredomain.domain.member.enums.AccountStatus;

public record AdminGetResponse(
Long id,
String name,
String phoneNumber,
String email,
AccountStatus status,
LocalDateTime lastLoginAt,
LocalDateTime lastPasswordUpdatedAt,
LocalDateTime createdAt,
LocalDateTime updatedAt,
Role role
) {
public static AdminGetResponse toDto(Admin admin) {
return new AdminGetResponse(
admin.getId(),
admin.getName(),
admin.getPhoneNumber(),
admin.getEmail(),
admin.getStatus(),
admin.getLastLoginAt(),
admin.getLastPasswordUpdatedAt(),
admin.getCreatedAt(),
admin.getUpdatedAt(),
admin.getRole()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.pgms.apimember.dto.response;

import java.time.LocalDateTime;

import com.pgms.coredomain.domain.member.Member;
import com.pgms.coredomain.domain.member.Role;
import com.pgms.coredomain.domain.member.enums.AccountStatus;
import com.pgms.coredomain.domain.member.enums.Gender;
import com.pgms.coredomain.domain.member.enums.Provider;

public record MemberDetailGetResponse(
Long id,
String name,
String phoneNumber,
String email,
String birthDate,
Gender gender,
String streetAddress,
String detailAddress,
String zipCode,
AccountStatus status,
Provider provider,
LocalDateTime lastLoginAt,
LocalDateTime lastPasswordUpdatedAt,
LocalDateTime createdAt,
LocalDateTime updatedAt,
Role role
) {
public static MemberDetailGetResponse toDto(Member member) {
return new MemberDetailGetResponse(
member.getId(),
member.getName(),
member.getPhoneNumber(),
member.getEmail(),
member.getBirthDate(),
member.getGender(),
member.getStreetAddress(),
member.getDetailAddress(),
member.getZipCode(),
member.getStatus(),
member.getProvider(),
member.getLastLoginAt(),
member.getLastPasswordUpdatedAt(),
member.getCreatedAt(),
member.getUpdatedAt(),
member.getRole()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.pgms.apimember.dto.response;

import com.pgms.coredomain.domain.member.Member;

public record MemberSummaryGetResponse(
Long id,
String name,
String phoneNumber,
String email
) {
public static MemberSummaryGetResponse toDto(Member member) {
return new MemberSummaryGetResponse(
member.getId(),
member.getName(),
member.getPhoneNumber(),
member.getEmail()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.pgms.apimember.service;

import java.util.List;
import java.util.NoSuchElementException;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pgms.apimember.dto.response.AdminGetResponse;
import com.pgms.apimember.dto.response.MemberDetailGetResponse;
import com.pgms.apimember.dto.response.MemberSummaryGetResponse;
import com.pgms.coredomain.domain.member.Admin;
import com.pgms.coredomain.domain.member.repository.AdminRepository;
import com.pgms.coredomain.domain.member.repository.MemberRepository;

import lombok.RequiredArgsConstructor;

@Service
@Transactional
@RequiredArgsConstructor
public class AdminService {

private final AdminRepository adminRepository;
private final MemberRepository memberRepository;

@Transactional(readOnly = true)
public Object getMembers(List<Long> memberIds) {
if (memberIds == null || memberIds.isEmpty()) {
return memberRepository.findAll().stream().map(MemberSummaryGetResponse::toDto).toList();
} else {
return memberRepository.findAllById(memberIds).stream().map(MemberDetailGetResponse::toDto).toList();
}
}

@Transactional(readOnly = true)
public AdminGetResponse getAdmin(Long adminId) {
return AdminGetResponse.toDto(getAvailableAdmin(adminId));
}

public void deleteAdmin(Long adminId) {
final Admin admin = getAvailableAdmin(adminId);
admin.updateToDeleted();
}

private Admin getAvailableAdmin(Long adminId) {
final Admin admin = adminRepository.findById(adminId)
.orElseThrow(() -> new NoSuchElementException("Admin not found"));
if (admin.isDeleted()) {
throw new IllegalArgumentException("Admin is already deleted");
}
return admin;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.pgms.coredomain.domain.member;

import java.time.LocalDateTime;

import com.pgms.coredomain.domain.member.enums.AccountStatus;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
Expand Down Expand Up @@ -41,9 +40,14 @@ public class Admin extends AccountBaseEntity {
@Enumerated(EnumType.STRING)
private AccountStatus status;

@Column(name = "last_login_at", nullable = false)
private LocalDateTime lastLoginAt;
@ManyToOne(fetch = FetchType.LAZY)
private Role role;

public boolean isDeleted() {
return this.status == AccountStatus.DELETED;
}

@ManyToOne
private Group group;
public void updateToDeleted() {
this.status = AccountStatus.DELETED;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.pgms.coredomain.domain.member;

import java.time.LocalDateTime;

import com.pgms.coredomain.domain.member.enums.AccountStatus;
import com.pgms.coredomain.domain.member.enums.Gender;
import com.pgms.coredomain.domain.member.enums.Provider;
Expand All @@ -10,13 +8,16 @@
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Getter;

@Getter
@Entity
@Table(name = "member")
public class Member extends AccountBaseEntity {
Expand Down Expand Up @@ -51,8 +52,8 @@ public class Member extends AccountBaseEntity {
@Column(name = "detail_address", nullable = false)
private String detailAddress;

@Column(name = "zipcode", nullable = false)
private String zipcode;
@Column(name = "zip_code", nullable = false)
private String zipCode;

@Column(name = "status", nullable = false)
@Enumerated(EnumType.STRING)
Expand All @@ -62,10 +63,11 @@ public class Member extends AccountBaseEntity {
@Enumerated(EnumType.STRING)
private Provider provider;

@Column(name = "last_login_at", nullable = false)
private LocalDateTime lastLoginAt;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
private Role role;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private Group group;
public boolean isDeleted() {
return this.status == AccountStatus.DELETED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import jakarta.persistence.Table;

@Entity
@Table(name = "group")
public class Group extends BaseEntity {
@Table(name = "role")
public class Role extends BaseEntity {

@Id
@Column(name = "id", nullable = false)
Expand All @@ -26,6 +26,6 @@ public class Group extends BaseEntity {
@Column(name = "name", nullable = false)
private String name;

@OneToMany(mappedBy = "group", cascade = CascadeType.REMOVE)
private List<GroupPermission> permissions = new ArrayList<>();
@OneToMany(mappedBy = "role", cascade = CascadeType.REMOVE)
private List<RolePermission> permissions = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
Expand All @@ -10,19 +11,19 @@
import jakarta.persistence.Table;

@Entity
@Table(name = "group_permission")
public class GroupPermission {
@Table(name = "role_permission")
public class RolePermission {

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(optional = false)
@JoinColumn(name = "group_id", nullable = false)
private Group group;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "role_id", nullable = false)
private Role role;

@ManyToOne(optional = false)
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "permission_id", nullable = false)
private Permission permission;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pgms.coredomain.domain.member.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.pgms.coredomain.domain.member.Admin;

public interface AdminRepository extends JpaRepository<Admin, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pgms.coredomain.domain.member.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.pgms.coredomain.domain.member.Member;

public interface MemberRepository extends JpaRepository<Member, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pgms.coredomain.domain.member.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.pgms.coredomain.domain.member.Permission;

public interface PermissionRepository extends JpaRepository<Permission, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pgms.coredomain.domain.member.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.pgms.coredomain.domain.member.RolePermission;

public interface RolePermissionRepository extends JpaRepository<RolePermission, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pgms.coredomain.domain.member.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.pgms.coredomain.domain.member.Role;

public interface RoleRepository extends JpaRepository<Role, Long> {
}

0 comments on commit a702524

Please sign in to comment.