重构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.annotation.ProjectAuthorize;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.Announcement; 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.entity.dto.AnnouncementDTO;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.BadRequestException; import cn.edu.hfut.rmdjzz.projectmanagement.exception.BadRequestException;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.ForbiddenException; import cn.edu.hfut.rmdjzz.projectmanagement.exception.ForbiddenException;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IAnnouncementService; import cn.edu.hfut.rmdjzz.projectmanagement.service.IAnnouncementService;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IProjectGroupService; 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.TokenUtils;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseList; import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseList;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseMap; import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseMap;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -29,23 +32,36 @@ public class AnnouncementController {
@SneakyThrows @SneakyThrows
@GetMapping @GetMapping
public ResponseList<AnnouncementDTO> getAnnouncementList(@RequestHeader(TokenUtils.HEADER_TOKEN) String token, @PathVariable Integer projectId) { public ResponseList<AnnouncementDTO> getAnnouncementList(
if (projectGroupService.getProjectAccessLevel(token, projectId) == 0) { // @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT); @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)); return ResponseList.ofSuccess(announcementService.getAnnouncementList(projectId));
} }
@SneakyThrows @SneakyThrows
@ProjectAuthorize("a==0")
@GetMapping("/{announcementId}") @GetMapping("/{announcementId}")
public ResponseMap getAnnouncementById( public ResponseMap getAnnouncementById(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token, // @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@RequestAttribute Integer accessLevel,
@PathVariable Integer projectId, @PathVariable Integer projectId,
@PathVariable Long announcementId @PathVariable Long announcementId
) { ) {
if (projectGroupService.getProjectAccessLevel(token, projectId) == 0) { // if (accessLevel == 0) {
throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT); // throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
} // }
return ResponseMap.ofSuccess(announcementService.getAnnouncementById(projectId, announcementId)); return ResponseMap.ofSuccess(announcementService.getAnnouncementById(projectId, announcementId));
} }
@ -91,11 +107,14 @@ public class AnnouncementController {
@SneakyThrows @SneakyThrows
@DeleteMapping("/{announcementId}") @DeleteMapping("/{announcementId}")
public ResponseMap deleteAnnouncement( public ResponseMap deleteAnnouncement(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token, // @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@PathVariable Integer projectId, @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(); return ResponseMap.ofSuccess();
} }
throw new BadRequestException(BadRequestException.OPERATE_FAILED); throw new BadRequestException(BadRequestException.OPERATE_FAILED);

View File

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