Skip to content

Commit

Permalink
[feat] 관리자 페이지 회원 관리
Browse files Browse the repository at this point in the history
[feat] 관리자 페이지 회원 관리
  • Loading branch information
yj-leez authored Jan 12, 2024
2 parents 6914f42 + 2afe119 commit ba3c7a9
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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;
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<UserManageDto> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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.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<UserManageDto> getUserList(UserSearchCond cond, User admin){
// 관리자만 조회 가능
if (!admin.getRole().toString().equals("ROLE_ADMIN")){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}

// 동적 쿼리로 조건 따라 유저 검색
List<User> userList = userRepository.getUserList(cond.getEmail(), cond.getName(), cond.getUniv());

// Dto로 필요 데이터만 담아 리턴
return userList.stream()
.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<Project> projects = projectRepository.findByUser_Id(userId);
projects.forEach(project -> {
project.setDel(true);
projectRepository.save(project);
});

List<DemandProject> demandProjects = demandProjectRepository.findByUser_Id(userId);
demandProjects.forEach(demandProject -> {
demandProject.setDel(true);
demandProjectRepository.save(demandProject);
});

}
}
4 changes: 4 additions & 0 deletions src/main/java/wowmarket/wow_server/domain/DemandProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/wowmarket/wow_server/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DemandProject, Long> {

Expand Down Expand Up @@ -41,6 +43,8 @@ Page<DemandProject> findBySearchUserUniv(@Param("currentDate") LocalDateTime cur

Page<DemandProject> findDemandProjectByUser_Id(Long seller_id, Pageable pageable);

List<DemandProject> findByUser_Id(Long sellerId);

@Query(nativeQuery = true, value = "select * from demand_project where demand_project_id =?")
DemandProject findByDemandProject_Id(Long demandProjectId);

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/wowmarket/wow_server/repository/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,4 +38,12 @@ public interface UserRepository extends JpaRepository<User, Long> {
@Query(nativeQuery = true, value = "SELECT * FROM user where role = 'ROLE_ADMIN'")
List<User> 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<User> getUserList(@Param("email") String email, @Param("name") String name, @Param("univ") String univ);


}

0 comments on commit ba3c7a9

Please sign in to comment.