Skip to content

Commit

Permalink
[feat] 관리자가 유저 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
yj-leez committed Jan 11, 2024
1 parent 8762985 commit 2afe119
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,4 +25,10 @@ public class UserManageController {
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
Expand Up @@ -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<UserManageDto> getUserList(UserSearchCond cond, User user){
public List<UserManageDto> 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);
}

Expand All @@ -31,4 +38,32 @@ public List<UserManageDto> 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<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
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

0 comments on commit 2afe119

Please sign in to comment.