From be6f970049f35076810ecc4cb23dc493079fe294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98yj-leez=E2=80=99?= Date: Thu, 11 Jan 2024 21:21:57 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=EA=B4=80=EB=A6=AC=EC=9E=90=20?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=A7=80=20=ED=9A=8C=EC=9B=90=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserManageController.java | 25 ++++++++++++++ .../admin/userManage/dto/UserManageDto.java | 27 +++++++++++++++ .../admin/userManage/dto/UserSearchCond.java | 11 ++++++ .../userManage/service/UserManageService.java | 34 +++++++++++++++++++ .../wowmarket/wow_server/domain/User.java | 4 +++ .../wow_server/repository/UserRepository.java | 10 ++++++ 6 files changed, 111 insertions(+) create mode 100644 src/main/java/wowmarket/wow_server/admin/userManage/controller/UserManageController.java create mode 100644 src/main/java/wowmarket/wow_server/admin/userManage/dto/UserManageDto.java create mode 100644 src/main/java/wowmarket/wow_server/admin/userManage/dto/UserSearchCond.java create mode 100644 src/main/java/wowmarket/wow_server/admin/userManage/service/UserManageService.java diff --git a/src/main/java/wowmarket/wow_server/admin/userManage/controller/UserManageController.java b/src/main/java/wowmarket/wow_server/admin/userManage/controller/UserManageController.java new file mode 100644 index 0000000..a2f469e --- /dev/null +++ b/src/main/java/wowmarket/wow_server/admin/userManage/controller/UserManageController.java @@ -0,0 +1,25 @@ +package wowmarket.wow_server.admin.userManage.controller; + +import lombok.RequiredArgsConstructor; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import wowmarket.wow_server.admin.userManage.dto.UserManageDto; +import wowmarket.wow_server.admin.userManage.dto.UserSearchCond; +import wowmarket.wow_server.admin.userManage.service.UserManageService; +import wowmarket.wow_server.domain.User; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/admin") +public class UserManageController { + private final UserManageService userManageService; + + @GetMapping("/manage/user") + public List getUserList(UserSearchCond cond, @AuthenticationPrincipal User user){ + return userManageService.getUserList(cond, user); + } +} diff --git a/src/main/java/wowmarket/wow_server/admin/userManage/dto/UserManageDto.java b/src/main/java/wowmarket/wow_server/admin/userManage/dto/UserManageDto.java new file mode 100644 index 0000000..7f4a06b --- /dev/null +++ b/src/main/java/wowmarket/wow_server/admin/userManage/dto/UserManageDto.java @@ -0,0 +1,27 @@ +package wowmarket.wow_server.admin.userManage.dto; + + +import lombok.Getter; +import wowmarket.wow_server.domain.User; + +@Getter +public class UserManageDto { + private String email; + private String name; + private String univ; + + private UserManageDto(String email, String name, String univ){ + this.email = email; + this.name = name; + this.univ = univ; + } + + + public static UserManageDto from(User user){ + UserManageDto userManageDto = new UserManageDto( + user.getEmail(), + user.getName(), + user.getUniv()); + return userManageDto; + } +} diff --git a/src/main/java/wowmarket/wow_server/admin/userManage/dto/UserSearchCond.java b/src/main/java/wowmarket/wow_server/admin/userManage/dto/UserSearchCond.java new file mode 100644 index 0000000..4859c80 --- /dev/null +++ b/src/main/java/wowmarket/wow_server/admin/userManage/dto/UserSearchCond.java @@ -0,0 +1,11 @@ +package wowmarket.wow_server.admin.userManage.dto; + +import lombok.Data; + +@Data +public class UserSearchCond { + private String email; + private String name; + private String univ; + +} diff --git a/src/main/java/wowmarket/wow_server/admin/userManage/service/UserManageService.java b/src/main/java/wowmarket/wow_server/admin/userManage/service/UserManageService.java new file mode 100644 index 0000000..d24ad43 --- /dev/null +++ b/src/main/java/wowmarket/wow_server/admin/userManage/service/UserManageService.java @@ -0,0 +1,34 @@ +package wowmarket.wow_server.admin.userManage.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import org.springframework.web.server.ResponseStatusException; +import wowmarket.wow_server.admin.userManage.dto.UserManageDto; +import wowmarket.wow_server.admin.userManage.dto.UserSearchCond; +import wowmarket.wow_server.domain.User; +import wowmarket.wow_server.repository.UserRepository; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +public class UserManageService { + private final UserRepository userRepository; + + public List getUserList(UserSearchCond cond, User user){ + // 관리자만 조회 가능 + if (!user.getRole().toString().equals("ROLE_ADMIN")){ + throw new ResponseStatusException(HttpStatus.BAD_REQUEST); + } + + // 동적 쿼리로 조건 따라 유저 검색 + List userList = userRepository.getUserList(cond.getEmail(), cond.getName(), cond.getUniv()); + + // Dto로 필요 데이터만 담아 리턴 + return userList.stream() + .map(UserManageDto::from) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/wowmarket/wow_server/domain/User.java b/src/main/java/wowmarket/wow_server/domain/User.java index 970d36f..1f1fa53 100644 --- a/src/main/java/wowmarket/wow_server/domain/User.java +++ b/src/main/java/wowmarket/wow_server/domain/User.java @@ -57,6 +57,10 @@ public class User extends BaseEntity implements UserDetails { @Column(columnDefinition = "integer default 0", nullable = false) private int demandLike; + @Column(columnDefinition="tinyint(0) default 0") + @Setter + private boolean isDel; + public void updateUserRole(Role role){ this.role = role; } diff --git a/src/main/java/wowmarket/wow_server/repository/UserRepository.java b/src/main/java/wowmarket/wow_server/repository/UserRepository.java index 8c23871..7f5ff9b 100644 --- a/src/main/java/wowmarket/wow_server/repository/UserRepository.java +++ b/src/main/java/wowmarket/wow_server/repository/UserRepository.java @@ -4,6 +4,8 @@ import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import wowmarket.wow_server.admin.userManage.dto.UserManageDto; +import wowmarket.wow_server.admin.userManage.dto.UserSearchCond; import wowmarket.wow_server.domain.User; import java.util.List; @@ -36,4 +38,12 @@ public interface UserRepository extends JpaRepository { @Query(nativeQuery = true, value = "SELECT * FROM user where role = 'ROLE_ADMIN'") List findAdmin(); + @Query("select u from User u where " + + "(:email is null or u.email like concat('%', :email, '%')) and " + + "(:name is null or u.name like concat('%', :name, '%')) and " + + "(:univ is null or u.univ like concat('%', :univ, '%')) and " + + "u.isDel = false") + List getUserList(@Param("email") String email, @Param("name") String name, @Param("univ") String univ); + + } From 2afe119757af4932c85e3f89e0e34684958263ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98yj-leez=E2=80=99?= Date: Thu, 11 Jan 2024 21:52:56 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[feat]=20=EA=B4=80=EB=A6=AC=EC=9E=90?= =?UTF-8?q?=EA=B0=80=20=EC=9C=A0=EC=A0=80=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserManageController.java | 9 +++++ .../userManage/service/UserManageService.java | 39 ++++++++++++++++++- .../wow_server/domain/DemandProject.java | 4 ++ .../repository/DemandProjectRepository.java | 4 ++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/main/java/wowmarket/wow_server/admin/userManage/controller/UserManageController.java b/src/main/java/wowmarket/wow_server/admin/userManage/controller/UserManageController.java index a2f469e..7c9f13a 100644 --- a/src/main/java/wowmarket/wow_server/admin/userManage/controller/UserManageController.java +++ b/src/main/java/wowmarket/wow_server/admin/userManage/controller/UserManageController.java @@ -1,8 +1,11 @@ package wowmarket.wow_server.admin.userManage.controller; import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import wowmarket.wow_server.admin.userManage.dto.UserManageDto; @@ -22,4 +25,10 @@ public class UserManageController { public List getUserList(UserSearchCond cond, @AuthenticationPrincipal User user){ return userManageService.getUserList(cond, user); } + + @PutMapping("/restrict/user") + public ResponseEntity deleteUser(String email, @AuthenticationPrincipal User user){ + userManageService.deleteUser(email, user); + return new ResponseEntity(HttpStatus.OK); + } } diff --git a/src/main/java/wowmarket/wow_server/admin/userManage/service/UserManageService.java b/src/main/java/wowmarket/wow_server/admin/userManage/service/UserManageService.java index d24ad43..5d54e54 100644 --- a/src/main/java/wowmarket/wow_server/admin/userManage/service/UserManageService.java +++ b/src/main/java/wowmarket/wow_server/admin/userManage/service/UserManageService.java @@ -6,20 +6,27 @@ import org.springframework.web.server.ResponseStatusException; import wowmarket.wow_server.admin.userManage.dto.UserManageDto; import wowmarket.wow_server.admin.userManage.dto.UserSearchCond; +import wowmarket.wow_server.domain.DemandProject; +import wowmarket.wow_server.domain.Project; import wowmarket.wow_server.domain.User; +import wowmarket.wow_server.repository.DemandProjectRepository; +import wowmarket.wow_server.repository.ProjectRepository; import wowmarket.wow_server.repository.UserRepository; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class UserManageService { private final UserRepository userRepository; + private final ProjectRepository projectRepository; + private final DemandProjectRepository demandProjectRepository; - public List getUserList(UserSearchCond cond, User user){ + public List getUserList(UserSearchCond cond, User admin){ // 관리자만 조회 가능 - if (!user.getRole().toString().equals("ROLE_ADMIN")){ + if (!admin.getRole().toString().equals("ROLE_ADMIN")){ throw new ResponseStatusException(HttpStatus.BAD_REQUEST); } @@ -31,4 +38,32 @@ public List getUserList(UserSearchCond cond, User user){ .map(UserManageDto::from) .collect(Collectors.toList()); } + + public void deleteUser(String email, User admin) { + // 관리자만 삭제 가능 + if (!admin.getRole().toString().equals("ROLE_ADMIN")){ + throw new ResponseStatusException(HttpStatus.BAD_REQUEST); + } + + // 유저 isDel 변경 + User user = userRepository.findByEmail(email) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST)); + user.setDel(true); + userRepository.save(user); + + // 유저 아이디가 일치하는 프로젝트 isDel 변경 + Long userId = user.getId(); + List projects = projectRepository.findByUser_Id(userId); + projects.forEach(project -> { + project.setDel(true); + projectRepository.save(project); + }); + + List demandProjects = demandProjectRepository.findByUser_Id(userId); + demandProjects.forEach(demandProject -> { + demandProject.setDel(true); + demandProjectRepository.save(demandProject); + }); + + } } diff --git a/src/main/java/wowmarket/wow_server/domain/DemandProject.java b/src/main/java/wowmarket/wow_server/domain/DemandProject.java index d8f37ef..be9aa5a 100644 --- a/src/main/java/wowmarket/wow_server/domain/DemandProject.java +++ b/src/main/java/wowmarket/wow_server/domain/DemandProject.java @@ -66,6 +66,10 @@ public class DemandProject extends BaseEntity{ @Column(columnDefinition = "tinyint(0) default 1") private boolean sellToAll; // 0-> 소속 대학 학생, 1-> 전체 학생 + @Column(columnDefinition="tinyint(0) default 0") + @Setter + private boolean isDel; + public void setUser(User user){ this.user = user;} public void setCategory(Category category){ this.category = category; diff --git a/src/main/java/wowmarket/wow_server/repository/DemandProjectRepository.java b/src/main/java/wowmarket/wow_server/repository/DemandProjectRepository.java index 8c22ab4..af0aad4 100644 --- a/src/main/java/wowmarket/wow_server/repository/DemandProjectRepository.java +++ b/src/main/java/wowmarket/wow_server/repository/DemandProjectRepository.java @@ -9,8 +9,10 @@ import org.springframework.transaction.annotation.Transactional; import wowmarket.wow_server.domain.DemandProject; import wowmarket.wow_server.domain.Permission; +import wowmarket.wow_server.domain.Project; import java.time.LocalDateTime; +import java.util.List; public interface DemandProjectRepository extends JpaRepository { @@ -41,6 +43,8 @@ Page findBySearchUserUniv(@Param("currentDate") LocalDateTime cur Page findDemandProjectByUser_Id(Long seller_id, Pageable pageable); + List findByUser_Id(Long sellerId); + @Query(nativeQuery = true, value = "select * from demand_project where demand_project_id =?") DemandProject findByDemandProject_Id(Long demandProjectId);