重构AnnouncementController和ProjectController

master
Lzx_88 2022-12-29 21:16:01 +08:00
parent dd681a9f20
commit 2cf9f1af63
4 changed files with 65 additions and 26 deletions

View File

@ -2,14 +2,17 @@ package cn.edu.hfut.rmdjzz.projectmanagement.controller;
import cn.edu.hfut.rmdjzz.projectmanagement.annotation.ProjectAuthorize;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.Announcement;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.ProjectGroup;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.dto.AnnouncementDTO;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.BadRequestException;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.ForbiddenException;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IAnnouncementService;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IProjectGroupService;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IStaffService;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.TokenUtils;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseList;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseMap;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -29,23 +32,36 @@ public class AnnouncementController {
@SneakyThrows
@GetMapping
public ResponseList<AnnouncementDTO> getAnnouncementList(@RequestHeader(TokenUtils.HEADER_TOKEN) String token, @PathVariable Integer projectId) {
if (projectGroupService.getProjectAccessLevel(token, projectId) == 0) {
throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
public ResponseList<AnnouncementDTO> getAnnouncementList(
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@PathVariable Integer projectId,
@PathVariable Integer staffId
) {
// if (projectGroupService.getProjectAccessLevel(token, projectId) == 0) {
// throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
// }
ProjectGroup designatedStaff = projectGroupService.getOne(
Wrappers.<ProjectGroup>lambdaQuery()
.eq(ProjectGroup::getStaffId, staffId)
.eq(ProjectGroup::getProjectId, projectId));
if (designatedStaff == null) { //说明是超级管理员
designatedStaff = new ProjectGroup(staffId, projectId, IStaffService.LEVEL_1, 1);
}
return ResponseList.ofSuccess(announcementService.getAnnouncementList(projectId));
}
@SneakyThrows
@ProjectAuthorize("a==0")
@GetMapping("/{announcementId}")
public ResponseMap getAnnouncementById(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token,
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@RequestAttribute Integer accessLevel,
@PathVariable Integer projectId,
@PathVariable Long announcementId
) {
if (projectGroupService.getProjectAccessLevel(token, projectId) == 0) {
throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
}
// if (accessLevel == 0) {
// throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
// }
return ResponseMap.ofSuccess(announcementService.getAnnouncementById(projectId, announcementId));
}
@ -91,11 +107,14 @@ public class AnnouncementController {
@SneakyThrows
@DeleteMapping("/{announcementId}")
public ResponseMap deleteAnnouncement(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token,
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@PathVariable Integer projectId,
@PathVariable Long announcementId
@PathVariable Long announcementId,
@RequestBody Announcement announcement
) {
if (announcementService.deleteAnnouncement(token, projectId, announcementId)) {
announcement.setProjectId(projectId);
announcement.setAnnouncementId(announcementId);
if (announcementService.removeById(announcement)) {
return ResponseMap.ofSuccess();
}
throw new BadRequestException(BadRequestException.OPERATE_FAILED);

View File

@ -38,22 +38,24 @@ public class ProjectController {
@SneakyThrows
@GetMapping
public ResponseList<ProjectDTO> getProjectListOfStaff(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token,
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@PathVariable Integer staffId,
@Valid RequestPage page,
@Parameter(description = "参数列表见Project实体类时间可以用xxxxStart与xxxxEnd来确定区间"
, required = true) @RequestParam("paramMap") Map<String, Object> paramMap
) {
Page<ProjectDTO> result = projectService.pageMyProjects(token, page, paramMap);
Page<ProjectDTO> result = projectService.pageMyProjects(staffId, page, paramMap);
return ResponseList.ofSuccess(result);
}
@SneakyThrows
@GetMapping("/{projectId}")
public ResponseMap getOneProjectBasicInfo(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token,
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@PathVariable Integer staffId,
@PathVariable("projectId") Integer projectId
) {
if (projectGroupService.getProjectAccessLevel(token, projectId) == 0) {
if (projectGroupService.getProjectAccessLevel(staffId, projectId) == 0) {
throw new BadRequestException(BadRequestException.WRONG_PARAMETERS);
}
return ResponseMap.ofSuccess(projectService.getById(projectId));
@ -70,42 +72,48 @@ public class ProjectController {
@SneakyThrows
@PostMapping("/complete")
public ResponseMap completeProject(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token,
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@PathVariable Integer staffId,
@Parameter(description = "只需要传projectId即可{\"projectId\": 1}")
@RequestBody Map<String, Object> map
) {
Integer targetProjectId = (Integer) map.get("projectId");
projectService.setProjectCompleted(token, targetProjectId);
projectService.setProjectCompleted(staffId, targetProjectId);
return ResponseMap.ofSuccess();
}
@SneakyThrows
@PostMapping
public ResponseMap createProject(@RequestHeader(TokenUtils.HEADER_TOKEN) String token, @RequestBody Project project) {
projectService.createProject(token, project);
public ResponseMap createProject(
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@PathVariable Integer staffId,
@RequestBody Project project) {
projectService.createProject(staffId, project);
return ResponseMap.ofSuccess();
}
@SneakyThrows
@PutMapping("/{projectId}")
public ResponseMap updateProject(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token,
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@PathVariable Integer projectId,
@PathVariable Integer staffId,
@RequestBody Project project
) {
project.setProjectId(projectId);
if (!projectService.checkOpenStatus(projectId))
throw new BadRequestException(IProjectService.PROJECT_UNOPENED);
projectService.updateProject(token, project);
projectService.updateProject(staffId, project);
return ResponseMap.ofSuccess();
}
@SneakyThrows
@GetMapping("/{projectId}/stats")
public ResponseMap getProjectProcess(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@PathVariable Integer projectId
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@PathVariable Integer projectId,
@PathVariable Integer staffId
) {
return ResponseMap.ofSuccess(projectService.getProjectProcess(token, projectId));
return ResponseMap.ofSuccess(projectService.getProjectProcess(staffId, projectId));
}
}

View File

@ -33,7 +33,7 @@ public interface IProjectGroupService extends IService<ProjectGroup> {
/**
* @return 0AccessLevel11
*/
Integer getProjectAccessLevel(String staffId, Integer projectId);
Integer getProjectAccessLevel(Integer staffId, Integer projectId);
Integer getProjectAccessLevel(Integer staffId, Integer staffGlobalLevel, Integer projectId);

View File

@ -22,15 +22,27 @@ public interface IProjectService extends IService<Project> {
Long countMyProjects(String token);
ProjectProcessDTO getProjectProcess(Integer staffId, Integer projectId) throws ForbiddenException, BadRequestException;
Page<ProjectDTO> pageMyProjects(Integer staffId, RequestPage page, Map<String, Object> params);
Boolean setProjectCompleted(Integer staffId, Integer projectId) throws BadRequestException, ForbiddenException;
ProjectProcessDTO getProjectProcess(String token, Integer projectId) throws ForbiddenException, BadRequestException;
Page<ProjectDTO> pageMyProjects(String token, RequestPage page, Map<String, Object> params);
Page<ProjectDTO> pageMyProjects(
String token,
RequestPage page,
Map<String, Object> params
);
Boolean setProjectCompleted(String token, Integer projectId) throws BadRequestException, ForbiddenException;
Boolean createProject(Integer staffId, Project project) throws BadRequestException, ForbiddenException;
Boolean createProject(String token, Project project) throws BadRequestException, ForbiddenException;
Boolean checkOpenStatus(Integer projectId);
Boolean updateProject(String token, Project project) throws BadRequestException, ForbiddenException;
Boolean updateProject(Integer staffId, Project project) throws BadRequestException, ForbiddenException;
}