diff --git a/src/main/java/wowmarket/wow_server/admin/permission/controller/AdminPermissionController.java b/src/main/java/wowmarket/wow_server/admin/permission/controller/AdminPermissionController.java new file mode 100644 index 0000000..f794127 --- /dev/null +++ b/src/main/java/wowmarket/wow_server/admin/permission/controller/AdminPermissionController.java @@ -0,0 +1,51 @@ +package wowmarket.wow_server.admin.permission.controller; + +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import wowmarket.wow_server.admin.permission.dto.request.PermissionProjectsRequest; +import wowmarket.wow_server.admin.permission.dto.response.ProjectListResponse; +import wowmarket.wow_server.admin.permission.service.AdminPermissionService; + +@RestController +@RequestMapping("/admin/permission") +@RequiredArgsConstructor +public class AdminPermissionController { + private final AdminPermissionService adminPermissionService; + + @GetMapping("/project") + public ProjectListResponse getProjectList(@RequestParam(name = "pageNo", defaultValue = "1", required = true) int pageNo) { + Sort sort = Sort.by( + Sort.Order.desc("permission"), + Sort.Order.asc("created_time") + ); + PageRequest pageable = PageRequest.of(pageNo - 1, 9, sort); + return adminPermissionService.getProjectList(pageNo, pageable); + } + + @PostMapping("/project") + public ResponseEntity updateProjectPermission( + @RequestParam(name = "permission", defaultValue = "APPROVED", required = true) String permission, + @RequestBody PermissionProjectsRequest permissionProjectsRequest) { + return adminPermissionService.updateProjectPermission(permission, permissionProjectsRequest); + } + + @GetMapping("/demand") + public ProjectListResponse getDemandProjectList(@RequestParam(name = "pageNo", defaultValue = "1", required = true) int pageNo) { + Sort sort = Sort.by( + Sort.Order.desc("permission"), + Sort.Order.asc("created_time") + ); + PageRequest pageable = PageRequest.of(pageNo - 1, 9, sort); + return adminPermissionService.getDemandProjectList(pageNo, pageable); + } + + @PostMapping("/demand") + public ResponseEntity updateDemandProjectPermission( + @RequestParam(name = "permission", defaultValue = "APPROVED", required = true) String permission, + @RequestBody PermissionProjectsRequest permissionProjectsRequest) { + return adminPermissionService.updateDemandProjectPermission(permission, permissionProjectsRequest); + } +} diff --git a/src/main/java/wowmarket/wow_server/admin/permission/dto/request/PermissionProjectsRequest.java b/src/main/java/wowmarket/wow_server/admin/permission/dto/request/PermissionProjectsRequest.java new file mode 100644 index 0000000..038ab52 --- /dev/null +++ b/src/main/java/wowmarket/wow_server/admin/permission/dto/request/PermissionProjectsRequest.java @@ -0,0 +1,12 @@ +package wowmarket.wow_server.admin.permission.dto.request; + +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Getter +@NoArgsConstructor +public class PermissionProjectsRequest { + private List projectIds; +} diff --git a/src/main/java/wowmarket/wow_server/admin/permission/dto/response/ProjectInfo.java b/src/main/java/wowmarket/wow_server/admin/permission/dto/response/ProjectInfo.java new file mode 100644 index 0000000..efaf332 --- /dev/null +++ b/src/main/java/wowmarket/wow_server/admin/permission/dto/response/ProjectInfo.java @@ -0,0 +1,34 @@ +package wowmarket.wow_server.admin.permission.dto.response; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import wowmarket.wow_server.domain.DemandProject; +import wowmarket.wow_server.domain.Project; + +import java.time.LocalDateTime; + +@Getter +@NoArgsConstructor +public class ProjectInfo { + private Long projectId; + private String projectName; + private String projectUniv; + private LocalDateTime createdTime; + private String permission; + + public ProjectInfo(Project project) { + this.projectId = project.getId(); + this.projectName = project.getProjectName(); + this.projectUniv = project.getUser().getUniv(); + this.createdTime = project.getCreated_time(); + this.permission = project.getPermission().toString(); + } + + public ProjectInfo(DemandProject demandProject) { + this.projectId = demandProject.getId(); + this.projectName = demandProject.getProjectName(); + this.projectUniv = demandProject.getUser().getUniv(); + this.createdTime = demandProject.getCreated_time(); + this.permission = demandProject.getPermission().toString(); + } +} diff --git a/src/main/java/wowmarket/wow_server/admin/permission/dto/response/ProjectListResponse.java b/src/main/java/wowmarket/wow_server/admin/permission/dto/response/ProjectListResponse.java new file mode 100644 index 0000000..cd4d708 --- /dev/null +++ b/src/main/java/wowmarket/wow_server/admin/permission/dto/response/ProjectListResponse.java @@ -0,0 +1,20 @@ +package wowmarket.wow_server.admin.permission.dto.response; + +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Getter +@NoArgsConstructor +public class ProjectListResponse { + private int totalPage; + private int currentPage; + private List projectList; + + public ProjectListResponse(int totalPage, int currentPage, List projectList) { + this.totalPage = totalPage; + this.currentPage = currentPage; + this.projectList = projectList; + } +} diff --git a/src/main/java/wowmarket/wow_server/admin/permission/service/AdminPermissionService.java b/src/main/java/wowmarket/wow_server/admin/permission/service/AdminPermissionService.java new file mode 100644 index 0000000..0f94fe6 --- /dev/null +++ b/src/main/java/wowmarket/wow_server/admin/permission/service/AdminPermissionService.java @@ -0,0 +1,59 @@ +package wowmarket.wow_server.admin.permission.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import wowmarket.wow_server.admin.permission.dto.request.PermissionProjectsRequest; +import wowmarket.wow_server.admin.permission.dto.response.ProjectInfo; +import wowmarket.wow_server.admin.permission.dto.response.ProjectListResponse; +import wowmarket.wow_server.domain.DemandProject; +import wowmarket.wow_server.domain.Permission; +import wowmarket.wow_server.domain.Project; +import wowmarket.wow_server.repository.DemandProjectRepository; +import wowmarket.wow_server.repository.ProjectRepository; + +import java.time.LocalDateTime; +import java.time.ZoneId; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class AdminPermissionService { + private final ProjectRepository projectRepository; + private final DemandProjectRepository demandProjectRepository; + + public ProjectListResponse getProjectList(int pageNo, PageRequest pageable) { + LocalDateTime currentDate = LocalDateTime.now(ZoneId.of("Asia/Seoul")); + + Page findProjects = projectRepository.findProjectsAdmin(currentDate, pageable); + Page projectList = findProjects.map(ProjectInfo::new); + + return new ProjectListResponse(projectList.getTotalPages(), projectList.getNumber(), projectList.getContent()); + } + + @Transactional + public ResponseEntity updateProjectPermission(String permission, PermissionProjectsRequest permissionProjectsRequest) { + permissionProjectsRequest.getProjectIds() + .forEach(projectId -> projectRepository.updatePermission(Permission.valueOf(permission), projectId)); + return ResponseEntity.ok().build(); + } + + public ProjectListResponse getDemandProjectList(int pageNo, PageRequest pageable) { + LocalDateTime currentDate = LocalDateTime.now(ZoneId.of("Asia/Seoul")); + + Page findDemandProjects = demandProjectRepository.findDemandProjectsPermission(currentDate, pageable); + Page projectList = findDemandProjects.map(ProjectInfo::new); + + return new ProjectListResponse(projectList.getTotalPages(), projectList.getNumber(), projectList.getContent()); + } + + @Transactional + public ResponseEntity updateDemandProjectPermission(String permission, PermissionProjectsRequest permissionProjectsRequest) { + permissionProjectsRequest.getProjectIds() + .forEach(projectId -> demandProjectRepository.updatePermission(Permission.valueOf(permission), projectId)); + return ResponseEntity.ok().build(); + } +} diff --git a/src/main/java/wowmarket/wow_server/repository/DemandProjectRepository.java b/src/main/java/wowmarket/wow_server/repository/DemandProjectRepository.java index 6bbdc00..6ee33e3 100644 --- a/src/main/java/wowmarket/wow_server/repository/DemandProjectRepository.java +++ b/src/main/java/wowmarket/wow_server/repository/DemandProjectRepository.java @@ -8,7 +8,7 @@ import org.springframework.data.repository.query.Param; import org.springframework.transaction.annotation.Transactional; import wowmarket.wow_server.domain.DemandProject; -import wowmarket.wow_server.domain.Project; +import wowmarket.wow_server.domain.Permission; import java.time.LocalDateTime; @@ -63,4 +63,11 @@ Page findBySearchUserUniv(@Param("currentDate") LocalDateTime cur "WHERE dp.id = :demandProjectId") void updateDemandProjectUnLike(@Param("demandProjectId") Long demandProjectId); + @Query("SELECT dp FROM DemandProject dp " + + "WHERE dp.isEnd = false AND dp.startDate <= :currentDate AND dp.endDate >= :currentDate") + Page findDemandProjectsPermission(@Param("currentDate") LocalDateTime currentDate, Pageable pageable); + + @Modifying + @Query("UPDATE DemandProject dp SET dp.permission = :permission WHERE dp.id = :demandProjectId") + void updatePermission(@Param("permission") Permission permission, @Param("demandProjectId") Long demandProjectId); } diff --git a/src/main/java/wowmarket/wow_server/repository/ProjectRepository.java b/src/main/java/wowmarket/wow_server/repository/ProjectRepository.java index 746759a..0816270 100644 --- a/src/main/java/wowmarket/wow_server/repository/ProjectRepository.java +++ b/src/main/java/wowmarket/wow_server/repository/ProjectRepository.java @@ -7,9 +7,9 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.transaction.annotation.Transactional; +import wowmarket.wow_server.domain.Permission; import wowmarket.wow_server.domain.Project; -import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; @@ -64,4 +64,12 @@ Page findBySearchUserUniv(@Param("currentDate") LocalDateTime currentDa @Query("UPDATE Project p SET p.likeCnt = p.likeCnt - 1 " + "WHERE p.id = :projectId") void updateProjectUnLike(@Param("projectId") Long projectId); + + @Query("SELECT p FROM Project p " + + "WHERE p.isDel = false AND p.isEnd = false AND p.endDate >= :currentDate") + Page findProjectsAdmin(@Param("currentDate") LocalDateTime currentDate, Pageable pageable); + + @Modifying + @Query("UPDATE Project p SET p.permission = :permission WHERE p.id = :projectId") + void updatePermission(@Param("permission") Permission permission, @Param("projectId") Long projectId); } \ No newline at end of file