diff --git a/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java b/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java index cef1115..c29cd2a 100644 --- a/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java +++ b/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java @@ -22,8 +22,8 @@ public class CommentController { @PostMapping public ResponseEntity> commentCreate(@RequestParam("issueId") Long issueId,@RequestBody CommentRequest commentRequest){ - this.commentService.create(issueId,commentRequest.getMessage(),commentRequest.getAuthorId()); - List comments = this.commentService.getList(issueId); + commentService.create(issueId,commentRequest.getMessage(),commentRequest.getAuthorId()); + List comments = commentService.getList(issueId); List responses = new ArrayList<>(); for(Comment i : comments){ responses.add(new CommentResponse(i)); @@ -33,17 +33,17 @@ public ResponseEntity> commentCreate(@RequestParam("issueI @DeleteMapping public ResponseEntity commentDelete(@RequestParam("commentId") Long commentId){ - return ResponseEntity.ok(this.commentService.delete(commentId)); + return ResponseEntity.ok(commentService.delete(commentId)); } @PatchMapping public ResponseEntity commentModify(@RequestParam("commentId") Long commentId, @RequestBody CommentRequest commentRequest){ - return ResponseEntity.ok(this.commentService.modify(commentId, commentRequest.getMessage())); + return ResponseEntity.ok(commentService.modify(commentId, commentRequest.getMessage())); } @GetMapping public ResponseEntity> commentListGet(@RequestParam("issueId")Long issueId){ - List comments = this.commentService.getList(issueId); + List comments = commentService.getList(issueId); List responses = new ArrayList<>(); for(Comment i : comments){ responses.add(new CommentResponse(i)); diff --git a/src/main/java/se/issuetrackingsystem/comment/service/CommentService.java b/src/main/java/se/issuetrackingsystem/comment/service/CommentService.java index 608845f..d77eb7e 100644 --- a/src/main/java/se/issuetrackingsystem/comment/service/CommentService.java +++ b/src/main/java/se/issuetrackingsystem/comment/service/CommentService.java @@ -24,32 +24,32 @@ public class CommentService { public Comment create(Long issueId, String content, Long authorId){ Comment comment = new Comment(); - Issue issue = this.issueRepository.findById(issueId).get(); + Issue issue = issueRepository.findById(issueId).get(); comment.setIssue(issue); - User user = this.userRepository.findById(authorId).get(); + User user = userRepository.findById(authorId).get(); comment.setAuthor(user); comment.setMessage(content); comment.setCreatedAt(LocalDateTime.now()); - this.commentRepository.save(comment); + commentRepository.save(comment); return comment; } public Comment modify(Long commentId,String content){ - Comment comment = this.commentRepository.findById(commentId).orElseThrow(()->new CustomException(ErrorCode.COMMENT_NOT_FOUND)); + Comment comment = commentRepository.findById(commentId).orElseThrow(()->new CustomException(ErrorCode.COMMENT_NOT_FOUND)); comment.setMessage(content); - this.commentRepository.save(comment); + commentRepository.save(comment); return comment; } public Comment delete(Long commentId){ - Comment comment = this.commentRepository.findById(commentId).orElseThrow(()->new CustomException(ErrorCode.COMMENT_NOT_FOUND)); - this.commentRepository.delete(comment); + Comment comment = commentRepository.findById(commentId).orElseThrow(()->new CustomException(ErrorCode.COMMENT_NOT_FOUND)); + commentRepository.delete(comment); return comment; } public List getList(Long issueId){ - Issue issue = this.issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); - List comments = this.commentRepository.findAllByIssue(issue); + Issue issue = issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); + List comments = commentRepository.findAllByIssue(issue); return comments; } } diff --git a/src/main/java/se/issuetrackingsystem/issue/controller/IssueController.java b/src/main/java/se/issuetrackingsystem/issue/controller/IssueController.java index 17f855c..a841672 100644 --- a/src/main/java/se/issuetrackingsystem/issue/controller/IssueController.java +++ b/src/main/java/se/issuetrackingsystem/issue/controller/IssueController.java @@ -11,6 +11,7 @@ import se.issuetrackingsystem.issue.dto.IssueRequest; import se.issuetrackingsystem.issue.dto.IssueResponse; import se.issuetrackingsystem.user.domain.User; +import se.issuetrackingsystem.user.dto.UserResponse; import java.util.ArrayList; import java.util.List; @@ -24,8 +25,8 @@ public class IssueController { @PostMapping public ResponseEntity> issueCreate(@RequestBody IssueRequest issueRequest, @RequestParam("projectId") Long projectId){ - this.issueService.create(projectId,issueRequest.getTitle(),issueRequest.getDescription(),issueRequest.getUserid(),issueRequest.getPriority()); - List issues = this.issueService.getList(projectId); + issueService.create(projectId,issueRequest.getTitle(),issueRequest.getDescription(),issueRequest.getUserid(),issueRequest.getPriority()); + List issues = issueService.getList(projectId); List responses = new ArrayList<>(); for(Issue i : issues){ responses.add(new IssueResponse(i)); @@ -35,7 +36,7 @@ public ResponseEntity> issueCreate(@RequestBody IssueRequest @GetMapping public ResponseEntity> issueCheck(@RequestParam("projectId") Long projectId){ - List issues = this.issueService.getList(projectId); + List issues = issueService.getList(projectId); List responses = new ArrayList<>(); for(Issue i : issues){ responses.add(new IssueResponse(i)); @@ -45,7 +46,7 @@ public ResponseEntity> issueCheck(@RequestParam("projectId") @GetMapping("/details") public ResponseEntity issueDetail(@RequestParam("issueId") Long issueId){ - Issue issue = this.issueService.getIssue(issueId); + Issue issue = issueService.getIssue(issueId); IssueResponse issueResponse = new IssueResponse(issue); return ResponseEntity.ok(issueResponse); } @@ -56,20 +57,20 @@ public ResponseEntity issueDelete(@RequestParam("issueId") Long i } @PatchMapping - public ResponseEntity issueModify(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueId){ - Issue issue = this.issueService.modify(issueId,issueRequest.getTitle(),issueRequest.getDescription(),issueRequest.getPriority()); - return ResponseEntity.ok(issue); + public ResponseEntity issueModify(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueId){ + Issue issue = issueService.modify(issueId,issueRequest.getTitle(),issueRequest.getDescription(),issueRequest.getPriority(),issueRequest.getUserid()); + return ResponseEntity.ok(new IssueResponse(issue)); } @PostMapping("/assignees") - public ResponseEntity issueSetAssignee(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueId){ - Issue issue = this.issueService.setAssignee(issueId,issueRequest.getUserid(),issueRequest.getAssigneeId()); - return ResponseEntity.ok(issue); + public ResponseEntity issueSetAssignee(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueId){ + Issue issue = issueService.setAssignee(issueId,issueRequest.getUserid(),issueRequest.getAssigneeId()); + return ResponseEntity.ok(new IssueResponse(issue)); } @GetMapping("/{status}") public ResponseEntity> issueCheckByStatus(@PathVariable("status") Issue.Status status,@RequestParam("projectId") Long projectId){ - List issues = this.issueService.getList(projectId,status); + List issues = issueService.getList(projectId,status); List responses = new ArrayList<>(); for(Issue i : issues){ responses.add(new IssueResponse(i)); @@ -78,14 +79,14 @@ public ResponseEntity> issueCheckByStatus(@PathVariable("sta } @PatchMapping("/status") - public ResponseEntity issueChangeStatus(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueId){ - Issue issue = this.issueService.changeStatus(issueRequest.getUserid(),issueId); - return ResponseEntity.ok(issue); + public ResponseEntity issueChangeStatus(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueId){ + Issue issue = issueService.changeStatus(issueRequest.getUserid(),issueId); + return ResponseEntity.ok(new IssueResponse(issue)); } @GetMapping("/assigned") public ResponseEntity> issueCheckByAssignee(@RequestParam("userId") Long userId){ - List issues = this.issueService.getListByAssignee(userId); + List issues = issueService.getListByAssignee(userId); List responses = new ArrayList<>(); for(Issue i : issues){ responses.add(new IssueResponse(i)); @@ -94,9 +95,9 @@ public ResponseEntity> issueCheckByAssignee(@RequestParam("u } @GetMapping("/candidates") - public ResponseEntity issueCandidate(@RequestParam("issueId") Long issueId) { - User user = this.issueService.candidateUser(issueId).get(); - return ResponseEntity.ok(user); + public ResponseEntity issueCandidate(@RequestParam("issueId") Long issueId) { + User user = issueService.candidateUser(issueId).get(); + return ResponseEntity.ok(new UserResponse(user)); } @GetMapping("/statistics/{projectId}") diff --git a/src/main/java/se/issuetrackingsystem/issue/dto/IssueResponse.java b/src/main/java/se/issuetrackingsystem/issue/dto/IssueResponse.java index e29180a..3b5d31c 100644 --- a/src/main/java/se/issuetrackingsystem/issue/dto/IssueResponse.java +++ b/src/main/java/se/issuetrackingsystem/issue/dto/IssueResponse.java @@ -41,10 +41,10 @@ public IssueResponse(Issue issue){ this.setReporter(issue.getReporter().getUsername()); } if(issue.getAssignee()!=null) { - this.setReporter(issue.getAssignee().getUsername()); + this.setAssignee(issue.getAssignee().getUsername()); } if(issue.getFixer()!=null) { - this.setReporter(issue.getFixer().getUsername()); + this.setFixer(issue.getFixer().getUsername()); } this.setPriority(issue.getPriority()); this.setCreatedAt(issue.getCreatedAt()); diff --git a/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java b/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java index 5f89042..d38e1f2 100644 --- a/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java +++ b/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java @@ -12,6 +12,7 @@ import se.issuetrackingsystem.project.repository.ProjectRepository; import se.issuetrackingsystem.user.domain.Dev; import se.issuetrackingsystem.user.domain.ProjectContributor; +import se.issuetrackingsystem.user.domain.Tester; import se.issuetrackingsystem.user.domain.User; import se.issuetrackingsystem.user.repository.ProjectContributorRepository; import se.issuetrackingsystem.user.repository.UserRepository; @@ -32,100 +33,128 @@ public class IssueService { public Issue create(Long projectId, String title, String description, Long reporterId, Issue.Priority priority){ Issue issue = new Issue(); Project project = projectRepository.findById(projectId).orElseThrow(()->new CustomException(ErrorCode.PROJECT_NOT_FOUND)); + User user = userRepository.findById(reporterId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + if(!user.canManageIssue()){ + throw new CustomException(ErrorCode.ROLE_FORBIDDEN); + } issue.setProject(project); issue.setTitle(title); issue.setDescription(description); if (priority!=null) { issue.setPriority(priority); } - issue.setReporter(userRepository.findById(reporterId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND))); + issue.setReporter(user); issue.setCreatedAt(LocalDateTime.now()); - this.issueRepository.save(issue); + issueRepository.save(issue); return issue; } public Issue getIssue(Long issueId){ - return this.issueRepository.findById(issueId).orElseThrow(() -> new CustomException(ErrorCode.ISSUE_NOT_FOUND)); + return issueRepository.findById(issueId).orElseThrow(() -> new CustomException(ErrorCode.ISSUE_NOT_FOUND)); } public List getList(Long projectId){ List issues; - Project project = this.projectRepository.findById(projectId).orElseThrow(() -> new CustomException(ErrorCode.PROJECT_NOT_FOUND)); - issues = this.issueRepository.findAllByProject(project); + Project project = projectRepository.findById(projectId).orElseThrow(() -> new CustomException(ErrorCode.PROJECT_NOT_FOUND)); + issues = issueRepository.findAllByProject(project); return issues; } public List getListByAssignee(Long userId){ List issues; - User user = this.userRepository.findById(userId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); - issues = this.issueRepository.findAllByAssignee(user); + User user = userRepository.findById(userId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + issues = issueRepository.findAllByAssignee(user); return issues; } public List getList(Long projectId, Issue.Status status){ List issues; - Project project = this.projectRepository.findById(projectId).orElseThrow(() -> new CustomException(ErrorCode.PROJECT_NOT_FOUND)); - issues = this.issueRepository.findAllByProjectAndStatus(project,status); + Project project = projectRepository.findById(projectId).orElseThrow(() -> new CustomException(ErrorCode.PROJECT_NOT_FOUND)); + issues = issueRepository.findAllByProjectAndStatus(project,status); return issues; } - public Issue modify(Long issueId, String title ,String description, Issue.Priority priority){ - Issue issue = this.issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); + public Issue modify(Long issueId, String title ,String description, Issue.Priority priority,Long userid){ + Issue issue = issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); + User user = userRepository.findById(userid).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + if(!user.canManageIssue()){ + throw new CustomException(ErrorCode.ROLE_FORBIDDEN); + } issue.setTitle(title); issue.setDescription(description); issue.setUpdatedAt(LocalDateTime.now()); issue.setPriority(priority); - this.issueRepository.save(issue); + issueRepository.save(issue); return issue; } public Issue delete(Long issueId){ - Issue issue = this.issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); - this.issueRepository.delete(issue); + Issue issue = issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); + issueRepository.delete(issue); return issue; } public Issue setAssignee(Long issueId, Long userId, Long assigneeId){ - Issue issue = this.issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); - User user = this.userRepository.findById(userId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); - if (!"PL".equals(user.getRole())) { + Issue issue = issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); + User user = userRepository.findById(userId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + User assignee = userRepository.findById(assigneeId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + if (!user.canSetAssignee()||!assignee.canChangeIssueStateAssignedToFixed()) { throw new CustomException(ErrorCode.ROLE_FORBIDDEN); } - issue.setAssignee(userRepository.findById(assigneeId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND))); + issue.setAssignee(assignee); issue.setStatus(Issue.Status.ASSIGNED); - this.issueRepository.save(issue); + issueRepository.save(issue); return issue; } public Issue changeStatus(Long userId, Long issueId){ - Issue issue = this.issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); - if(issue.getStatus()== Issue.Status.NEW){ - issue.setStatus(Issue.Status.ASSIGNED); - } - else if(issue.getStatus()== Issue.Status.ASSIGNED){ - issue.setStatus(Issue.Status.FIXED); - issue.setFixer(this.userRepository.findById(userId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND))); - } - else if(issue.getStatus()== Issue.Status.FIXED){ - issue.setStatus(Issue.Status.RESOLVED); - } - else if(issue.getStatus()== Issue.Status.RESOLVED){ - issue.setStatus(Issue.Status.CLOSE); - } - else if(issue.getStatus()== Issue.Status.CLOSE){ - issue.setStatus(Issue.Status.REOPENED); - } - else if(issue.getStatus()== Issue.Status.REOPENED){ - issue.setStatus(Issue.Status.CLOSE); + Issue issue = issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); + User user = userRepository.findById(userId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + switch (issue.getStatus()){ + case NEW : + throw new CustomException(ErrorCode.METHOD_NOT_ALLOWED); + case ASSIGNED: + if(!user.canChangeIssueStateAssignedToFixed()){ + throw new CustomException(ErrorCode.ROLE_FORBIDDEN); + } + else { + issue.setStatus(Issue.Status.FIXED); + issue.setFixer(user); + } + break; + case FIXED: + if(!user.canChangeIssueStateFixedToResolved()){ + throw new CustomException(ErrorCode.ROLE_FORBIDDEN); + } + else { + issue.setStatus(Issue.Status.RESOLVED); + } + break; + case RESOLVED,REOPENED: + if (!user.canChangeIssueStateResolvedToClosed()) { + throw new CustomException(ErrorCode.ROLE_FORBIDDEN); + } + else { + issue.setStatus(Issue.Status.CLOSE); + } + break; + case CLOSE: + if(!user.canChangeIssueStateResolvedToClosed()){ + throw new CustomException(ErrorCode.ROLE_FORBIDDEN); + } + else { + issue.setStatus(Issue.Status.REOPENED); + } + break; } - this.issueRepository.save(issue); + issueRepository.save(issue); return issue; } public Optional candidateUser(Long issueId) { - Issue issue = this.issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); + Issue issue = issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); Project project = issue.getProject(); - List users = this.userRepository.findAll(); + List users = userRepository.findAll(); ArrayList devs = new ArrayList<>(); ArrayList projectDevs = new ArrayList<>(); //모든 developer 추가 @@ -139,7 +168,7 @@ public Optional candidateUser(Long issueId) List projs; for(User u : devs){ projs = new ArrayList<>(); - proconts = this.projectContributorRepository.findAllByContributor(u); + proconts = projectContributorRepository.findAllByContributor(u); for(ProjectContributor pc : proconts){ projs.add(pc.getProject()); } @@ -157,7 +186,7 @@ public Optional candidateUser(Long issueId) Integer maxP = 0; Optional result = Optional.empty(); for(User d : devs){ - devInfo tempInfo = new devInfo(d,this.issueRepository.findAllByFixer(d)); + devInfo tempInfo = new devInfo(d,issueRepository.findAllByFixer(d)); for(String s : titleWords){ if(tempInfo.getIssuesTitleWords().containsKey(s)){ tempInfo.addPoints(tempInfo.getIssuesTitleWords().get(s)*10); @@ -173,7 +202,9 @@ public Optional candidateUser(Long issueId) result=Optional.ofNullable(tempInfo.getUser()); } } - + if(result.isEmpty()){ + throw new CustomException(ErrorCode.USER_NOT_FOUND); + } return result; } @@ -186,8 +217,8 @@ public class devInfo { private Integer points; devInfo(User dev, List issues) { - this.points = 0; - this.user = dev; + points = 0; + user = dev; fixedIssues = new ArrayList<>(issues); ArrayList sTemp; @@ -214,7 +245,7 @@ public class devInfo { } public void addPoints(Integer value) { - this.points += value; + points += value; } } public IssueStatisticsResponse getIssueStatistics(Long projectId) { diff --git a/src/main/java/se/issuetrackingsystem/user/domain/User.java b/src/main/java/se/issuetrackingsystem/user/domain/User.java index e86f021..8c2fbe9 100644 --- a/src/main/java/se/issuetrackingsystem/user/domain/User.java +++ b/src/main/java/se/issuetrackingsystem/user/domain/User.java @@ -28,13 +28,13 @@ public abstract class User { @OneToMany(mappedBy = "contributor") private List projectContributors; - abstract boolean canManageProject(); // Admin - abstract boolean canManageComment(); // PL, Dev, Tester - abstract boolean canManageIssue(); // Tester - abstract boolean canSetAssignee(); // PL - abstract boolean canChangeIssueStateAssignedToFixed(); // Dev - abstract boolean canChangeIssueStateFixedToResolved(); // Tester - abstract boolean canChangeIssueStateResolvedToClosed(); // PL + public boolean canManageProject(){ return false; } // Admin + public boolean canManageComment(){ return false; } // PL, Dev, Tester + public boolean canManageIssue(){ return false; } // Tester + public boolean canSetAssignee(){ return false; } // PL + public boolean canChangeIssueStateAssignedToFixed(){ return false; } // Dev + public boolean canChangeIssueStateFixedToResolved(){ return false; } // Tester + public boolean canChangeIssueStateResolvedToClosed(){ return false; } // PL public User(String username, String password) { this.username = username; diff --git a/src/test/java/se/issuetrackingsystem/IssueServiceTests.java b/src/test/java/se/issuetrackingsystem/IssueServiceTests.java index 5d36c3f..765a002 100644 --- a/src/test/java/se/issuetrackingsystem/IssueServiceTests.java +++ b/src/test/java/se/issuetrackingsystem/IssueServiceTests.java @@ -191,9 +191,10 @@ void issueModify() { issue.setPriority(Issue.Priority.MINOR); when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue)); + when(userRepository.findById(any())).thenReturn(Optional.of(new Tester())); //when - Issue result = issueService.modify(issue.getId(), "Hello", "Banana", Issue.Priority.MAJOR); + Issue result = issueService.modify(issue.getId(), "Hello", "Banana", Issue.Priority.MAJOR,1L); //then assertEquals(result.getTitle(), "Hello");