From 6c65da369e1b636287d2bc6f96d5592c14677f7a Mon Sep 17 00:00:00 2001 From: 2522001 Date: Tue, 28 May 2024 16:18:30 +0900 Subject: [PATCH 1/6] =?UTF-8?q?fix:=20#24=20-=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=EC=9D=BC=20=EB=B3=80=EC=88=98=EB=AA=85=20=ED=86=B5=EC=9D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/controller/CommentController.java | 4 +- .../comment/domain/Comment.java | 3 +- .../comment/dto/CommentRequest.java | 2 +- .../comment/dto/CommentResponse.java | 11 ++--- .../comment/service/CommentService.java | 2 +- .../issue/controller/IssueController.java | 3 +- .../issue/domain/Issue.java | 4 +- .../issue/dto/IssueRequest.java | 3 +- .../issue/dto/IssueResponse.java | 42 +++++++++---------- .../issue/dto/IssueStatisticsResponse.java | 8 ++-- .../issue/service/IssueService.java | 8 +--- .../project/domain/Project.java | 4 +- .../issuetrackingsystem/user/domain/User.java | 4 +- 13 files changed, 46 insertions(+), 52 deletions(-) diff --git a/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java b/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java index 56188b0..05d2028 100644 --- a/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java +++ b/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java @@ -8,8 +8,6 @@ import se.issuetrackingsystem.comment.dto.CommentResponse; import se.issuetrackingsystem.comment.service.CommentService; import se.issuetrackingsystem.comment.domain.Comment; -import se.issuetrackingsystem.issue.domain.Issue; -import se.issuetrackingsystem.issue.dto.IssueResponse; import java.util.ArrayList; import java.util.List; @@ -24,7 +22,7 @@ public class CommentController { @PostMapping public void commentCreate(@RequestParam("issueId") Long issueid,@RequestBody CommentRequest commentRequest){ - this.commentService.create(issueid,commentRequest.getMessage(),commentRequest.getAuthorid()); + this.commentService.create(issueid,commentRequest.getMessage(),commentRequest.getAuthorId()); } @DeleteMapping diff --git a/src/main/java/se/issuetrackingsystem/comment/domain/Comment.java b/src/main/java/se/issuetrackingsystem/comment/domain/Comment.java index 842329b..f323d8f 100644 --- a/src/main/java/se/issuetrackingsystem/comment/domain/Comment.java +++ b/src/main/java/se/issuetrackingsystem/comment/domain/Comment.java @@ -13,13 +13,14 @@ @Setter @Entity public class Comment { + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String message; - private LocalDateTime created_at; + private LocalDateTime createdAt; @ManyToOne private User author; diff --git a/src/main/java/se/issuetrackingsystem/comment/dto/CommentRequest.java b/src/main/java/se/issuetrackingsystem/comment/dto/CommentRequest.java index 562c97c..1bcac7c 100644 --- a/src/main/java/se/issuetrackingsystem/comment/dto/CommentRequest.java +++ b/src/main/java/se/issuetrackingsystem/comment/dto/CommentRequest.java @@ -12,5 +12,5 @@ public class CommentRequest { private String message; @NotBlank - private Long authorid; + private Long authorId; } diff --git a/src/main/java/se/issuetrackingsystem/comment/dto/CommentResponse.java b/src/main/java/se/issuetrackingsystem/comment/dto/CommentResponse.java index 112bea3..cd71782 100644 --- a/src/main/java/se/issuetrackingsystem/comment/dto/CommentResponse.java +++ b/src/main/java/se/issuetrackingsystem/comment/dto/CommentResponse.java @@ -3,32 +3,33 @@ import lombok.Getter; import lombok.Setter; import se.issuetrackingsystem.comment.domain.Comment; -import se.issuetrackingsystem.user.domain.User; import java.time.LocalDateTime; @Getter @Setter public class CommentResponse { + private Long id; private String message; - private LocalDateTime created_at; + private LocalDateTime createdAt; - private Long authorid; + private Long authorId; private String username; private String role; + public CommentResponse(Comment comment) { this.id = comment.getId(); this.message = comment.getMessage(); if (comment.getAuthor() != null) { - this.authorid = comment.getAuthor().getId(); + this.authorId = comment.getAuthor().getId(); this.username = comment.getAuthor().getUsername(); this.role = comment.getAuthor().getRole(); } - this.created_at = comment.getCreated_at(); + this.createdAt = comment.getCreatedAt(); } } diff --git a/src/main/java/se/issuetrackingsystem/comment/service/CommentService.java b/src/main/java/se/issuetrackingsystem/comment/service/CommentService.java index cb730d9..e7214b7 100644 --- a/src/main/java/se/issuetrackingsystem/comment/service/CommentService.java +++ b/src/main/java/se/issuetrackingsystem/comment/service/CommentService.java @@ -29,7 +29,7 @@ public Comment create(Long issueid, String content, Long authorid){ User user = this.userRepository.findById(authorid).get(); comment.setAuthor(user); comment.setMessage(content); - comment.setCreated_at(LocalDateTime.now()); + comment.setCreatedAt(LocalDateTime.now()); this.commentRepository.save(comment); return comment; } diff --git a/src/main/java/se/issuetrackingsystem/issue/controller/IssueController.java b/src/main/java/se/issuetrackingsystem/issue/controller/IssueController.java index d5b7d27..8699cb4 100644 --- a/src/main/java/se/issuetrackingsystem/issue/controller/IssueController.java +++ b/src/main/java/se/issuetrackingsystem/issue/controller/IssueController.java @@ -14,7 +14,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.Optional; @Slf4j @RequestMapping("api/v1/issues") @@ -57,7 +56,7 @@ public void issueModify(@RequestBody IssueRequest issueRequest,@RequestParam("is @PostMapping("/assignees") public void issueSetAssignee(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueid){ - this.issueService.setAssignee(issueid,issueRequest.getUserid(),issueRequest.getAssigneeid()); + this.issueService.setAssignee(issueid,issueRequest.getUserid(),issueRequest.getAssigneeId()); } @GetMapping("/{status}") diff --git a/src/main/java/se/issuetrackingsystem/issue/domain/Issue.java b/src/main/java/se/issuetrackingsystem/issue/domain/Issue.java index 74f3fe2..c32df96 100644 --- a/src/main/java/se/issuetrackingsystem/issue/domain/Issue.java +++ b/src/main/java/se/issuetrackingsystem/issue/domain/Issue.java @@ -43,9 +43,9 @@ public class Issue { @Enumerated(EnumType.STRING) private Priority priority = Priority.MAJOR; - private LocalDateTime updated_at; + private LocalDateTime updatedAt; - private LocalDateTime created_at; + private LocalDateTime createdAt; @OneToMany(mappedBy = "issue", cascade = CascadeType.REMOVE) private List commentList; diff --git a/src/main/java/se/issuetrackingsystem/issue/dto/IssueRequest.java b/src/main/java/se/issuetrackingsystem/issue/dto/IssueRequest.java index 56718a7..3f2ad22 100644 --- a/src/main/java/se/issuetrackingsystem/issue/dto/IssueRequest.java +++ b/src/main/java/se/issuetrackingsystem/issue/dto/IssueRequest.java @@ -1,6 +1,5 @@ package se.issuetrackingsystem.issue.dto; -import jakarta.validation.constraints.NotEmpty; import lombok.Getter; import lombok.Setter; import se.issuetrackingsystem.issue.domain.Issue; @@ -18,6 +17,6 @@ public class IssueRequest { private Long userid; - private Long assigneeid; + private Long assigneeId; } diff --git a/src/main/java/se/issuetrackingsystem/issue/dto/IssueResponse.java b/src/main/java/se/issuetrackingsystem/issue/dto/IssueResponse.java index 9e57fab..e29180a 100644 --- a/src/main/java/se/issuetrackingsystem/issue/dto/IssueResponse.java +++ b/src/main/java/se/issuetrackingsystem/issue/dto/IssueResponse.java @@ -11,25 +11,6 @@ @Setter public class IssueResponse { - public IssueResponse(Issue issue){ - this.setTitle(issue.getTitle()); - this.setId(issue.getId()); - this.setDescription(issue.getDescription()); - if(issue.getReporter()!=null) { - this.setReporter(issue.getReporter().getUsername()); - } - if(issue.getAssignee()!=null) { - this.setReporter(issue.getAssignee().getUsername()); - } - if(issue.getFixer()!=null) { - this.setReporter(issue.getFixer().getUsername()); - } - this.setPriority(issue.getPriority()); - this.setCreated_at(issue.getCreated_at()); - this.setUpdated_at(issue.getUpdated_at()); - this.setStatus(issue.getStatus()); - } - @NotEmpty private Long id; @@ -49,6 +30,25 @@ public IssueResponse(Issue issue){ private Issue.Priority priority; - private LocalDateTime created_at; - private LocalDateTime updated_at; + private LocalDateTime createdAt; + private LocalDateTime updatedAt; + + public IssueResponse(Issue issue){ + this.setTitle(issue.getTitle()); + this.setId(issue.getId()); + this.setDescription(issue.getDescription()); + if(issue.getReporter()!=null) { + this.setReporter(issue.getReporter().getUsername()); + } + if(issue.getAssignee()!=null) { + this.setReporter(issue.getAssignee().getUsername()); + } + if(issue.getFixer()!=null) { + this.setReporter(issue.getFixer().getUsername()); + } + this.setPriority(issue.getPriority()); + this.setCreatedAt(issue.getCreatedAt()); + this.setUpdatedAt(issue.getUpdatedAt()); + this.setStatus(issue.getStatus()); + } } diff --git a/src/main/java/se/issuetrackingsystem/issue/dto/IssueStatisticsResponse.java b/src/main/java/se/issuetrackingsystem/issue/dto/IssueStatisticsResponse.java index 7709e69..e773720 100644 --- a/src/main/java/se/issuetrackingsystem/issue/dto/IssueStatisticsResponse.java +++ b/src/main/java/se/issuetrackingsystem/issue/dto/IssueStatisticsResponse.java @@ -9,10 +9,10 @@ @Getter public class IssueStatisticsResponse { - private Map statusDistribution; - private Map reporterDistribution; - private Map assigneeDistribution; - private List topCommentedIssues; + private final Map statusDistribution; + private final Map reporterDistribution; + private final Map assigneeDistribution; + private final List topCommentedIssues; public IssueStatisticsResponse(Map statusDistribution, Map reporterDistribution, diff --git a/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java b/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java index a4943a3..a0d6047 100644 --- a/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java +++ b/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java @@ -1,20 +1,16 @@ package se.issuetrackingsystem.issue.service; -import jakarta.validation.constraints.Null; import lombok.Getter; import lombok.RequiredArgsConstructor; -import lombok.Setter; import org.springframework.stereotype.Service; import se.issuetrackingsystem.common.exception.CustomException; import se.issuetrackingsystem.common.exception.ErrorCode; import se.issuetrackingsystem.issue.domain.Issue; -import se.issuetrackingsystem.issue.dto.IssueRequest; import se.issuetrackingsystem.issue.dto.IssueStatisticsResponse; import se.issuetrackingsystem.issue.repository.IssueRepository; import se.issuetrackingsystem.project.domain.Project; import se.issuetrackingsystem.project.repository.ProjectRepository; import se.issuetrackingsystem.user.domain.Dev; -import se.issuetrackingsystem.user.domain.PL; import se.issuetrackingsystem.user.domain.ProjectContributor; import se.issuetrackingsystem.user.domain.User; import se.issuetrackingsystem.user.repository.ProjectContributorRepository; @@ -42,7 +38,7 @@ public Issue create(Long projectid, String title, String description, Long repor issue.setPriority(priority); } issue.setReporter(userRepository.findById(reporterid).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND))); - issue.setCreated_at(LocalDateTime.now()); + issue.setCreatedAt(LocalDateTime.now()); this.issueRepository.save(issue); return issue; } @@ -76,7 +72,7 @@ public Issue modify(Long issueid,String title ,String description, Issue.Priorit Issue issue = this.issueRepository.findById(issueid).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); issue.setTitle(title); issue.setDescription(description); - issue.setUpdated_at(LocalDateTime.now()); + issue.setUpdatedAt(LocalDateTime.now()); issue.setPriority(priority); this.issueRepository.save(issue); return issue; diff --git a/src/main/java/se/issuetrackingsystem/project/domain/Project.java b/src/main/java/se/issuetrackingsystem/project/domain/Project.java index bc083fe..8f88be1 100644 --- a/src/main/java/se/issuetrackingsystem/project/domain/Project.java +++ b/src/main/java/se/issuetrackingsystem/project/domain/Project.java @@ -31,12 +31,12 @@ public class Project { private List projectContributors = new ArrayList<>(); @Column(nullable = false) - private LocalDateTime createdDate; + private LocalDateTime createdAt; public Project(String title, Admin admin) { this.title = title; this.admin = admin; - this.createdDate = LocalDateTime.now(); + this.createdAt = LocalDateTime.now(); } public void addContributor(ProjectContributor projectContributor) { diff --git a/src/main/java/se/issuetrackingsystem/user/domain/User.java b/src/main/java/se/issuetrackingsystem/user/domain/User.java index 87abd0b..ca1b8e5 100644 --- a/src/main/java/se/issuetrackingsystem/user/domain/User.java +++ b/src/main/java/se/issuetrackingsystem/user/domain/User.java @@ -24,7 +24,7 @@ public abstract class User { private String password; @Column(nullable = false) - private LocalDateTime createdDate; + private LocalDateTime createdAt; abstract boolean canManageProject(); // Admin abstract boolean canManageComment(); // PL, Dev, Tester @@ -37,7 +37,7 @@ public abstract class User { public User(String username, String password) { this.username = username; this.password = password; - this.createdDate = LocalDateTime.now(); + this.createdAt = LocalDateTime.now(); } public String getRole() { From 686c4a4fd913ba5321f4f6eff5736ebe49977ee2 Mon Sep 17 00:00:00 2001 From: 2522001 Date: Tue, 28 May 2024 16:36:12 +0900 Subject: [PATCH 2/6] =?UTF-8?q?refactor:=20#24=20-=20=ED=8C=A8=ED=82=A4?= =?UTF-8?q?=EC=A7=80=20=EA=B5=AC=EC=A1=B0=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit impl 삭제 --- .../issue/service/IssueService.java | 1 + .../project/service/ProjectService.java | 84 +++++++++++++++- .../service/impl/ProjectServiceImpl.java | 95 ------------------- .../user/service/UserService.java | 77 ++++++++++++++- .../user/service/impl/UserServiceImpl.java | 86 ----------------- ...eImplTest.java => ProjectServiceTest.java} | 6 +- ...viceImplTest.java => UserServiceTest.java} | 6 +- 7 files changed, 159 insertions(+), 196 deletions(-) delete mode 100644 src/main/java/se/issuetrackingsystem/project/service/impl/ProjectServiceImpl.java delete mode 100644 src/main/java/se/issuetrackingsystem/user/service/impl/UserServiceImpl.java rename src/test/java/se/issuetrackingsystem/project/service/{impl/ProjectServiceImplTest.java => ProjectServiceTest.java} (97%) rename src/test/java/se/issuetrackingsystem/user/service/{impl/UserServiceImplTest.java => UserServiceTest.java} (97%) diff --git a/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java b/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java index a0d6047..695ec55 100644 --- a/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java +++ b/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java @@ -23,6 +23,7 @@ @RequiredArgsConstructor @Service public class IssueService { + private final IssueRepository issueRepository; private final UserRepository userRepository; private final ProjectRepository projectRepository; diff --git a/src/main/java/se/issuetrackingsystem/project/service/ProjectService.java b/src/main/java/se/issuetrackingsystem/project/service/ProjectService.java index 5b4a7dc..7c9f767 100644 --- a/src/main/java/se/issuetrackingsystem/project/service/ProjectService.java +++ b/src/main/java/se/issuetrackingsystem/project/service/ProjectService.java @@ -1,15 +1,91 @@ package se.issuetrackingsystem.project.service; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import se.issuetrackingsystem.common.exception.CustomException; +import se.issuetrackingsystem.common.exception.ErrorCode; +import se.issuetrackingsystem.project.domain.Project; import se.issuetrackingsystem.project.dto.ProjectRequest; import se.issuetrackingsystem.project.dto.ProjectResponse; +import se.issuetrackingsystem.project.repository.ProjectRepository; +import se.issuetrackingsystem.user.domain.*; +import se.issuetrackingsystem.user.repository.ProjectContributorRepository; +import se.issuetrackingsystem.user.repository.UserRepository; import java.util.List; -public interface ProjectService { +@Service +@RequiredArgsConstructor +public class ProjectService { - ProjectResponse createProject(ProjectRequest request); + private final ProjectRepository projectRepository; + private final UserRepository userRepository; + private final ProjectContributorRepository projectContributorRepository; - List getProjects(Long userId); + @Transactional + public ProjectResponse createProject(ProjectRequest request) { - ProjectResponse getProject(Long projectId); + String title = request.getTitle(); + Long adminId = request.getAdminId(); + List contributorIds = request.getContributorIds(); + + Admin admin = (Admin) userRepository.findById(adminId) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + + Project project = new Project(title, admin); + projectRepository.save(project); + + addContributors(contributorIds, project); + + return new ProjectResponse(project); + } + + @Transactional(readOnly = true) + public List getProjects(Long userId) { + + User user = userRepository.findById(userId) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + + List projects; + + if (user instanceof Admin) { + projects = projectRepository.findByAdminId(userId) + .orElseThrow(() -> new CustomException(ErrorCode.PROJECT_NOT_FOUND)); + } else { + projects = projectRepository.findByContributorId(userId) + .orElseThrow(() -> new CustomException(ErrorCode.PROJECT_NOT_FOUND)); + } + + return projects + .stream() + .map(ProjectResponse::new) + .toList(); + } + + @Transactional(readOnly = true) + public ProjectResponse getProject(Long projectId) { + + Project project = projectRepository.findById(projectId) + .orElseThrow(() -> new CustomException(ErrorCode.PROJECT_NOT_FOUND)); + + return new ProjectResponse(project); + } + + protected void addContributors(List contributorIds, Project project) { + List projectContributors = contributorIds + .stream() + .map(id -> { + Contributor contributor = (Contributor) userRepository.findById(id) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + ProjectContributorPK projectContributorPK = new ProjectContributorPK(contributor.getId(), project.getId()); + return new ProjectContributor(projectContributorPK, project, contributor); + }) + .toList(); + + projectContributorRepository.saveAll(projectContributors); + + projectContributors.forEach(project::addContributor); + projectRepository.save(project); + } } diff --git a/src/main/java/se/issuetrackingsystem/project/service/impl/ProjectServiceImpl.java b/src/main/java/se/issuetrackingsystem/project/service/impl/ProjectServiceImpl.java deleted file mode 100644 index 651454f..0000000 --- a/src/main/java/se/issuetrackingsystem/project/service/impl/ProjectServiceImpl.java +++ /dev/null @@ -1,95 +0,0 @@ -package se.issuetrackingsystem.project.service.impl; - -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import se.issuetrackingsystem.common.exception.CustomException; -import se.issuetrackingsystem.common.exception.ErrorCode; -import se.issuetrackingsystem.project.domain.Project; -import se.issuetrackingsystem.project.dto.ProjectRequest; -import se.issuetrackingsystem.project.dto.ProjectResponse; -import se.issuetrackingsystem.project.repository.ProjectRepository; -import se.issuetrackingsystem.project.service.ProjectService; -import se.issuetrackingsystem.user.domain.*; -import se.issuetrackingsystem.user.repository.ProjectContributorRepository; -import se.issuetrackingsystem.user.repository.UserRepository; - -import java.util.List; - -@Service -@RequiredArgsConstructor -public class ProjectServiceImpl implements ProjectService { - - private final ProjectRepository projectRepository; - private final UserRepository userRepository; - private final ProjectContributorRepository projectContributorRepository; - - @Override - @Transactional - public ProjectResponse createProject(ProjectRequest request) { - - String title = request.getTitle(); - Long adminId = request.getAdminId(); - List contributorIds = request.getContributorIds(); - - Admin admin = (Admin) userRepository.findById(adminId) - .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); - - Project project = new Project(title, admin); - projectRepository.save(project); - - addContributors(contributorIds, project); - - return new ProjectResponse(project); - } - - @Override - @Transactional(readOnly = true) - public List getProjects(Long userId) { - - User user = userRepository.findById(userId) - .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); - - List projects; - - if (user instanceof Admin) { - projects = projectRepository.findByAdminId(userId) - .orElseThrow(() -> new CustomException(ErrorCode.PROJECT_NOT_FOUND)); - } else { - projects = projectRepository.findByContributorId(userId) - .orElseThrow(() -> new CustomException(ErrorCode.PROJECT_NOT_FOUND)); - } - - return projects - .stream() - .map(ProjectResponse::new) - .toList(); - } - - @Override - @Transactional(readOnly = true) - public ProjectResponse getProject(Long projectId) { - - Project project = projectRepository.findById(projectId) - .orElseThrow(() -> new CustomException(ErrorCode.PROJECT_NOT_FOUND)); - - return new ProjectResponse(project); - } - - protected void addContributors(List contributorIds, Project project) { - List projectContributors = contributorIds - .stream() - .map(id -> { - Contributor contributor = (Contributor) userRepository.findById(id) - .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); - ProjectContributorPK projectContributorPK = new ProjectContributorPK(contributor.getId(), project.getId()); - return new ProjectContributor(projectContributorPK, project, contributor); - }) - .toList(); - - projectContributorRepository.saveAll(projectContributors); - - projectContributors.forEach(project::addContributor); - projectRepository.save(project); - } -} diff --git a/src/main/java/se/issuetrackingsystem/user/service/UserService.java b/src/main/java/se/issuetrackingsystem/user/service/UserService.java index e3ee8c4..0be7f47 100644 --- a/src/main/java/se/issuetrackingsystem/user/service/UserService.java +++ b/src/main/java/se/issuetrackingsystem/user/service/UserService.java @@ -1,16 +1,83 @@ package se.issuetrackingsystem.user.service; +import lombok.RequiredArgsConstructor; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import se.issuetrackingsystem.common.exception.CustomException; +import se.issuetrackingsystem.common.exception.ErrorCode; +import se.issuetrackingsystem.user.domain.*; import se.issuetrackingsystem.user.dto.LoginRequest; import se.issuetrackingsystem.user.dto.RegisterRequest; import se.issuetrackingsystem.user.dto.UserResponse; +import se.issuetrackingsystem.user.repository.UserRepository; +import se.issuetrackingsystem.user.service.UserService; import java.util.List; +import java.util.Optional; -public interface UserService { +@Service +@RequiredArgsConstructor +public class UserService { - UserResponse register(RegisterRequest request); + private final UserRepository userRepository; + PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); - UserResponse login(LoginRequest request); + @Transactional + public UserResponse register(RegisterRequest request) { - List getUsers(); -} \ No newline at end of file + String username = request.getUsername(); + String password = request.getPassword(); + String role = request.getRole(); + + if (isDuplicateUsername(username)) { + throw new CustomException(ErrorCode.USERNAME_FORBIDDEN); + } + + User user = createUser(role, username, passwordEncoder.encode(password)); + userRepository.save(user); + + return new UserResponse(user); + } + + @Transactional + public UserResponse login(LoginRequest request) { + + String username = request.getUsername(); + + User user = userRepository.findByUsername(username) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + + if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) { + throw new CustomException(ErrorCode.PASSWORD_FORBIDDEN); + } + + return new UserResponse(user); + } + + @Transactional(readOnly = true) + public List getUsers() { + + List users = userRepository.findAll(); + return users + .stream() + .map(UserResponse::new) + .toList(); + } + + private boolean isDuplicateUsername(String username) { + Optional user = userRepository.findByUsername(username); + return user.isPresent(); + } + + private User createUser(String role, String username, String password) { + return switch (role) { + case "Admin" -> new Admin(username, password); + case "PL" -> new PL(username, password); + case "Dev" -> new Dev(username, password); + case "Tester" -> new Tester(username, password); + default -> throw new CustomException(ErrorCode.ROLE_FORBIDDEN); + }; + } +} diff --git a/src/main/java/se/issuetrackingsystem/user/service/impl/UserServiceImpl.java b/src/main/java/se/issuetrackingsystem/user/service/impl/UserServiceImpl.java deleted file mode 100644 index 30ccb24..0000000 --- a/src/main/java/se/issuetrackingsystem/user/service/impl/UserServiceImpl.java +++ /dev/null @@ -1,86 +0,0 @@ -package se.issuetrackingsystem.user.service.impl; - -import lombok.RequiredArgsConstructor; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import se.issuetrackingsystem.common.exception.CustomException; -import se.issuetrackingsystem.common.exception.ErrorCode; -import se.issuetrackingsystem.user.domain.*; -import se.issuetrackingsystem.user.dto.LoginRequest; -import se.issuetrackingsystem.user.dto.RegisterRequest; -import se.issuetrackingsystem.user.dto.UserResponse; -import se.issuetrackingsystem.user.repository.UserRepository; -import se.issuetrackingsystem.user.service.UserService; - -import java.util.List; -import java.util.Optional; - -@Service -@RequiredArgsConstructor -public class UserServiceImpl implements UserService { - - private final UserRepository userRepository; - PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); - - @Override - @Transactional - public UserResponse register(RegisterRequest request) { - - String username = request.getUsername(); - String password = request.getPassword(); - String role = request.getRole(); - - if (isDuplicateUsername(username)) { - throw new CustomException(ErrorCode.USERNAME_FORBIDDEN); - } - - User user = createUser(role, username, passwordEncoder.encode(password)); - userRepository.save(user); - - return new UserResponse(user); - } - - @Override - @Transactional - public UserResponse login(LoginRequest request) { - - String username = request.getUsername(); - - User user = userRepository.findByUsername(username) - .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); - - if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) { - throw new CustomException(ErrorCode.PASSWORD_FORBIDDEN); - } - - return new UserResponse(user); - } - - @Override - @Transactional(readOnly = true) - public List getUsers() { - - List users = userRepository.findAll(); - return users - .stream() - .map(UserResponse::new) - .toList(); - } - - private boolean isDuplicateUsername(String username) { - Optional user = userRepository.findByUsername(username); - return user.isPresent(); - } - - private User createUser(String role, String username, String password) { - return switch (role) { - case "Admin" -> new Admin(username, password); - case "PL" -> new PL(username, password); - case "Dev" -> new Dev(username, password); - case "Tester" -> new Tester(username, password); - default -> throw new CustomException(ErrorCode.ROLE_FORBIDDEN); - }; - } -} diff --git a/src/test/java/se/issuetrackingsystem/project/service/impl/ProjectServiceImplTest.java b/src/test/java/se/issuetrackingsystem/project/service/ProjectServiceTest.java similarity index 97% rename from src/test/java/se/issuetrackingsystem/project/service/impl/ProjectServiceImplTest.java rename to src/test/java/se/issuetrackingsystem/project/service/ProjectServiceTest.java index 4b5a425..b06f57d 100644 --- a/src/test/java/se/issuetrackingsystem/project/service/impl/ProjectServiceImplTest.java +++ b/src/test/java/se/issuetrackingsystem/project/service/ProjectServiceTest.java @@ -1,4 +1,4 @@ -package se.issuetrackingsystem.project.service.impl; +package se.issuetrackingsystem.project.service; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -27,10 +27,10 @@ @ExtendWith(SpringExtension.class) @SpringBootTest @Transactional -class ProjectServiceImplTest { +class ProjectServiceTest { @Autowired - private ProjectServiceImpl projectService; + private ProjectService projectService; @Autowired private UserRepository userRepository; diff --git a/src/test/java/se/issuetrackingsystem/user/service/impl/UserServiceImplTest.java b/src/test/java/se/issuetrackingsystem/user/service/UserServiceTest.java similarity index 97% rename from src/test/java/se/issuetrackingsystem/user/service/impl/UserServiceImplTest.java rename to src/test/java/se/issuetrackingsystem/user/service/UserServiceTest.java index d19ec80..6c01f5a 100644 --- a/src/test/java/se/issuetrackingsystem/user/service/impl/UserServiceImplTest.java +++ b/src/test/java/se/issuetrackingsystem/user/service/UserServiceTest.java @@ -1,4 +1,4 @@ -package se.issuetrackingsystem.user.service.impl; +package se.issuetrackingsystem.user.service; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -24,10 +24,10 @@ @ExtendWith(SpringExtension.class) @SpringBootTest @Transactional -class UserServiceImplTest { +class UserServiceTest { @Autowired - UserServiceImpl userService; + UserService userService; @Autowired UserRepository userRepository; From 70a03ec881926fe0201a6930ceadac52d4be0fb9 Mon Sep 17 00:00:00 2001 From: 2522001 Date: Tue, 28 May 2024 17:18:01 +0900 Subject: [PATCH 3/6] =?UTF-8?q?fix:=20#24=20-=20=EB=B3=80=EC=88=98?= =?UTF-8?q?=EB=AA=85=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit id → Id --- .../comment/controller/CommentController.java | 16 +++--- .../comment/service/CommentService.java | 18 +++---- .../issue/controller/IssueController.java | 40 +++++++-------- .../issue/service/IssueService.java | 50 +++++++++---------- 4 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java b/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java index 05d2028..d5dc2b8 100644 --- a/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java +++ b/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java @@ -21,23 +21,23 @@ public class CommentController { private final CommentService commentService; @PostMapping - public void commentCreate(@RequestParam("issueId") Long issueid,@RequestBody CommentRequest commentRequest){ - this.commentService.create(issueid,commentRequest.getMessage(),commentRequest.getAuthorId()); + public void commentCreate(@RequestParam("issueId") Long issueId,@RequestBody CommentRequest commentRequest){ + this.commentService.create(issueId,commentRequest.getMessage(),commentRequest.getAuthorId()); } @DeleteMapping - public void commentDelete(@RequestParam("commentId") Long commentid){ - this.commentService.delete(commentid); + public void commentDelete(@RequestParam("commentId") Long commentId){ + this.commentService.delete(commentId); } @PatchMapping - public void commentModify(@RequestParam("commentId") Long commentid, @RequestBody CommentRequest commentRequest){ - this.commentService.modify(commentid,commentRequest.getMessage()); + public void commentModify(@RequestParam("commentId") Long commentId, @RequestBody CommentRequest commentRequest){ + this.commentService.modify(commentId,commentRequest.getMessage()); } @GetMapping - public ResponseEntity> commentListGet(@RequestParam("issueId")Long issueid){ - List comments = this.commentService.getList(issueid); + public ResponseEntity> commentListGet(@RequestParam("issueId")Long issueId){ + List comments = this.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 e7214b7..a2663ae 100644 --- a/src/main/java/se/issuetrackingsystem/comment/service/CommentService.java +++ b/src/main/java/se/issuetrackingsystem/comment/service/CommentService.java @@ -22,11 +22,11 @@ public class CommentService { private final IssueRepository issueRepository; private final UserRepository userRepository; - public Comment create(Long issueid, String content, Long authorid){ + public Comment create(Long issueId, String content, Long authorId){ Comment comment = new Comment(); - Issue issue = this.issueRepository.findById(issueid).get(); + Issue issue = this.issueRepository.findById(issueId).get(); comment.setIssue(issue); - User user = this.userRepository.findById(authorid).get(); + User user = this.userRepository.findById(authorId).get(); comment.setAuthor(user); comment.setMessage(content); comment.setCreatedAt(LocalDateTime.now()); @@ -34,20 +34,20 @@ public Comment create(Long issueid, String content, Long authorid){ return comment; } - public Comment modify(Long commentid,String content){ - Comment comment = this.commentRepository.findById(commentid).orElseThrow(()->new CustomException(ErrorCode.COMMENT_NOT_FOUND)); + public Comment modify(Long commentId,String content){ + Comment comment = this.commentRepository.findById(commentId).orElseThrow(()->new CustomException(ErrorCode.COMMENT_NOT_FOUND)); comment.setMessage(content); this.commentRepository.save(comment); return comment; } - public void delete(Long commentid){ - Comment comment = this.commentRepository.findById(commentid).orElseThrow(()->new CustomException(ErrorCode.COMMENT_NOT_FOUND)); + public void delete(Long commentId){ + Comment comment = this.commentRepository.findById(commentId).orElseThrow(()->new CustomException(ErrorCode.COMMENT_NOT_FOUND)); this.commentRepository.delete(comment); } - public List getList(Long issueid){ - Issue issue = this.issueRepository.findById(issueid).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); + public List getList(Long issueId){ + Issue issue = this.issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); List comments = this.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 8699cb4..f833e12 100644 --- a/src/main/java/se/issuetrackingsystem/issue/controller/IssueController.java +++ b/src/main/java/se/issuetrackingsystem/issue/controller/IssueController.java @@ -23,13 +23,13 @@ public class IssueController { private final IssueService issueService; @PostMapping - public void issueCreate(@RequestBody IssueRequest issueRequest, @RequestParam("projectId") Long projectid){ - this.issueService.create(projectid,issueRequest.getTitle(),issueRequest.getDescription(),issueRequest.getUserid(),issueRequest.getPriority()); + public void issueCreate(@RequestBody IssueRequest issueRequest, @RequestParam("projectId") Long projectId){ + this.issueService.create(projectId,issueRequest.getTitle(),issueRequest.getDescription(),issueRequest.getUserid(),issueRequest.getPriority()); } @GetMapping - public ResponseEntity> issueCheck(@RequestParam("projectId") Long projectid){ - List issues = this.issueService.getList(projectid); + public ResponseEntity> issueCheck(@RequestParam("projectId") Long projectId){ + List issues = this.issueService.getList(projectId); List responses = new ArrayList<>(); for(Issue i : issues){ responses.add(new IssueResponse(i)); @@ -38,30 +38,30 @@ public ResponseEntity> issueCheck(@RequestParam("projectId") } @GetMapping("/details") - public ResponseEntity issueDetail(@RequestParam("issueId") Long issueid){ - Issue issue = this.issueService.getIssue(issueid); + public ResponseEntity issueDetail(@RequestParam("issueId") Long issueId){ + Issue issue = this.issueService.getIssue(issueId); IssueResponse issueResponse = new IssueResponse(issue); return ResponseEntity.ok(issueResponse); } @DeleteMapping - public void issueDelete(@RequestParam("issueId") Long issueid){ - this.issueService.delete(issueid); + public void issueDelete(@RequestParam("issueId") Long issueId){ + this.issueService.delete(issueId); } @PatchMapping - public void issueModify(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueid){ - this.issueService.modify(issueid,issueRequest.getTitle(),issueRequest.getDescription(),issueRequest.getPriority()); + public void issueModify(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueId){ + this.issueService.modify(issueId,issueRequest.getTitle(),issueRequest.getDescription(),issueRequest.getPriority()); } @PostMapping("/assignees") - public void issueSetAssignee(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueid){ - this.issueService.setAssignee(issueid,issueRequest.getUserid(),issueRequest.getAssigneeId()); + public void issueSetAssignee(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueId){ + this.issueService.setAssignee(issueId,issueRequest.getUserid(),issueRequest.getAssigneeId()); } @GetMapping("/{status}") - public ResponseEntity> issueCheckByStatus(@PathVariable("status") Issue.Status status,@RequestParam("projectId") Long projectid){ - List issues = this.issueService.getList(projectid,status); + public ResponseEntity> issueCheckByStatus(@PathVariable("status") Issue.Status status,@RequestParam("projectId") Long projectId){ + List issues = this.issueService.getList(projectId,status); List responses = new ArrayList<>(); for(Issue i : issues){ responses.add(new IssueResponse(i)); @@ -70,13 +70,13 @@ public ResponseEntity> issueCheckByStatus(@PathVariable("sta } @PatchMapping("/status") - public void issueChangeStatus(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueid){ - this.issueService.changeStatus(issueRequest.getUserid(),issueid); + public void issueChangeStatus(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueId){ + this.issueService.changeStatus(issueRequest.getUserid(),issueId); } @GetMapping("/assigned") - public ResponseEntity> issueCheckByAssignee(@RequestParam("userId") Long userid){ - List issues = this.issueService.getListByAssignee(userid); + public ResponseEntity> issueCheckByAssignee(@RequestParam("userId") Long userId){ + List issues = this.issueService.getListByAssignee(userId); List responses = new ArrayList<>(); for(Issue i : issues){ responses.add(new IssueResponse(i)); @@ -85,8 +85,8 @@ public ResponseEntity> issueCheckByAssignee(@RequestParam("u } @GetMapping("/candidates") - public ResponseEntity issueCandidate(@RequestParam("issueId") Long issueid) { - User user = this.issueService.candidateUser(issueid).get(); + public ResponseEntity issueCandidate(@RequestParam("issueId") Long issueId) { + User user = this.issueService.candidateUser(issueId).get(); return ResponseEntity.ok(user); } diff --git a/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java b/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java index 695ec55..5e11a6f 100644 --- a/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java +++ b/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java @@ -29,48 +29,48 @@ public class IssueService { private final ProjectRepository projectRepository; private final ProjectContributorRepository projectContributorRepository; - public Issue create(Long projectid, String title, String description, Long reporterid, Issue.Priority priority){ + public Issue create(Long projectId, String title, String description, Long reporterId, Issue.Priority priority){ Issue issue = new Issue(); - Project project = this.projectRepository.findById(projectid).orElseThrow(()->new CustomException(ErrorCode.PROJECT_NOT_FOUND)); + Project project = projectRepository.findById(projectId).orElseThrow(()->new CustomException(ErrorCode.PROJECT_NOT_FOUND)); issue.setProject(project); issue.setTitle(title); issue.setDescription(description); - if(priority!=null) { + if (priority!=null) { issue.setPriority(priority); } - issue.setReporter(userRepository.findById(reporterid).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND))); + issue.setReporter(userRepository.findById(reporterId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND))); issue.setCreatedAt(LocalDateTime.now()); this.issueRepository.save(issue); return issue; } - public Issue getIssue(Long id){ - return this.issueRepository.findById(id).orElseThrow(() -> new CustomException(ErrorCode.ISSUE_NOT_FOUND)); + public Issue getIssue(Long issueId){ + return this.issueRepository.findById(issueId).orElseThrow(() -> new CustomException(ErrorCode.ISSUE_NOT_FOUND)); } - public List getList(Long projectid){ + public List getList(Long projectId){ List issues; - Project project = this.projectRepository.findById(projectid).orElseThrow(() -> new CustomException(ErrorCode.PROJECT_NOT_FOUND)); + Project project = this.projectRepository.findById(projectId).orElseThrow(() -> new CustomException(ErrorCode.PROJECT_NOT_FOUND)); issues = this.issueRepository.findAllByProject(project); return issues; } - public List getListByAssignee(Long userid){ + public List getListByAssignee(Long userId){ List issues; - User user = this.userRepository.findById(userid).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + User user = this.userRepository.findById(userId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); issues = this.issueRepository.findAllByAssignee(user); return issues; } - public List getList(Long projectid, Issue.Status status){ + public List getList(Long projectId, Issue.Status status){ List issues; - Project project = this.projectRepository.findById(projectid).orElseThrow(() -> new CustomException(ErrorCode.PROJECT_NOT_FOUND)); + Project project = this.projectRepository.findById(projectId).orElseThrow(() -> new CustomException(ErrorCode.PROJECT_NOT_FOUND)); issues = this.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){ + Issue issue = this.issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); issue.setTitle(title); issue.setDescription(description); issue.setUpdatedAt(LocalDateTime.now()); @@ -79,32 +79,32 @@ public Issue modify(Long issueid,String title ,String description, Issue.Priorit return issue; } - public Issue delete(Long issueid){ - Issue issue = this.issueRepository.findById(issueid).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); + public Issue delete(Long issueId){ + Issue issue = this.issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); this.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)); + 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())) { throw new CustomException(ErrorCode.ROLE_FORBIDDEN); } - issue.setAssignee(userRepository.findById(assigneeid).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND))); + issue.setAssignee(userRepository.findById(assigneeId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND))); issue.setStatus(Issue.Status.ASSIGNED); this.issueRepository.save(issue); return issue; } - public void changeStatus(Long userid, Long issueid){ - Issue issue = this.issueRepository.findById(issueid).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); + public void 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))); + 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); @@ -120,9 +120,9 @@ else if(issue.getStatus()== Issue.Status.REOPENED){ } this.issueRepository.save(issue); } - public Optional candidateUser(Long issueid) + public Optional candidateUser(Long issueId) { - Issue issue = this.issueRepository.findById(issueid).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); + Issue issue = this.issueRepository.findById(issueId).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND)); Project project = issue.getProject(); List users = this.userRepository.findAll(); ArrayList devs = new ArrayList<>(); From afef09473e67a37376ecd121cfd0fb1397995d92 Mon Sep 17 00:00:00 2001 From: "Feel5nL\\Feel5nL" Date: Tue, 28 May 2024 20:22:32 +0900 Subject: [PATCH 4/6] =?UTF-8?q?fix:=20#24=20return=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/controller/CommentController.java | 16 +++-- .../comment/service/CommentService.java | 3 +- .../issue/controller/IssueController.java | 27 +++++--- .../issue/service/IssueService.java | 3 +- .../CommentControllerTests.java | 61 +++++++++++++++++++ .../IssueServiceTests.java | 17 +++++- 6 files changed, 109 insertions(+), 18 deletions(-) create mode 100644 src/test/java/se/issuetrackingsystem/CommentControllerTests.java diff --git a/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java b/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java index d5dc2b8..cef1115 100644 --- a/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java +++ b/src/main/java/se/issuetrackingsystem/comment/controller/CommentController.java @@ -21,18 +21,24 @@ public class CommentController { private final CommentService commentService; @PostMapping - public void commentCreate(@RequestParam("issueId") Long issueId,@RequestBody CommentRequest commentRequest){ + public ResponseEntity> commentCreate(@RequestParam("issueId") Long issueId,@RequestBody CommentRequest commentRequest){ this.commentService.create(issueId,commentRequest.getMessage(),commentRequest.getAuthorId()); + List comments = this.commentService.getList(issueId); + List responses = new ArrayList<>(); + for(Comment i : comments){ + responses.add(new CommentResponse(i)); + } + return ResponseEntity.ok(responses); } @DeleteMapping - public void commentDelete(@RequestParam("commentId") Long commentId){ - this.commentService.delete(commentId); + public ResponseEntity commentDelete(@RequestParam("commentId") Long commentId){ + return ResponseEntity.ok(this.commentService.delete(commentId)); } @PatchMapping - public void commentModify(@RequestParam("commentId") Long commentId, @RequestBody CommentRequest commentRequest){ - this.commentService.modify(commentId,commentRequest.getMessage()); + public ResponseEntity commentModify(@RequestParam("commentId") Long commentId, @RequestBody CommentRequest commentRequest){ + return ResponseEntity.ok(this.commentService.modify(commentId, commentRequest.getMessage())); } @GetMapping diff --git a/src/main/java/se/issuetrackingsystem/comment/service/CommentService.java b/src/main/java/se/issuetrackingsystem/comment/service/CommentService.java index a2663ae..608845f 100644 --- a/src/main/java/se/issuetrackingsystem/comment/service/CommentService.java +++ b/src/main/java/se/issuetrackingsystem/comment/service/CommentService.java @@ -41,9 +41,10 @@ public Comment modify(Long commentId,String content){ return comment; } - public void delete(Long commentId){ + public Comment delete(Long commentId){ Comment comment = this.commentRepository.findById(commentId).orElseThrow(()->new CustomException(ErrorCode.COMMENT_NOT_FOUND)); this.commentRepository.delete(comment); + return comment; } public List getList(Long issueId){ diff --git a/src/main/java/se/issuetrackingsystem/issue/controller/IssueController.java b/src/main/java/se/issuetrackingsystem/issue/controller/IssueController.java index f833e12..17f855c 100644 --- a/src/main/java/se/issuetrackingsystem/issue/controller/IssueController.java +++ b/src/main/java/se/issuetrackingsystem/issue/controller/IssueController.java @@ -23,8 +23,14 @@ public class IssueController { private final IssueService issueService; @PostMapping - public void issueCreate(@RequestBody IssueRequest issueRequest, @RequestParam("projectId") Long projectId){ + 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); + List responses = new ArrayList<>(); + for(Issue i : issues){ + responses.add(new IssueResponse(i)); + } + return ResponseEntity.ok(responses); } @GetMapping @@ -45,18 +51,20 @@ public ResponseEntity issueDetail(@RequestParam("issueId") Long i } @DeleteMapping - public void issueDelete(@RequestParam("issueId") Long issueId){ - this.issueService.delete(issueId); + public ResponseEntity issueDelete(@RequestParam("issueId") Long issueId){ + return ResponseEntity.ok(new IssueResponse(issueService.delete(issueId))); } @PatchMapping - public void issueModify(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueId){ - this.issueService.modify(issueId,issueRequest.getTitle(),issueRequest.getDescription(),issueRequest.getPriority()); + 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); } @PostMapping("/assignees") - public void issueSetAssignee(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueId){ - this.issueService.setAssignee(issueId,issueRequest.getUserid(),issueRequest.getAssigneeId()); + public ResponseEntity issueSetAssignee(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueId){ + Issue issue = this.issueService.setAssignee(issueId,issueRequest.getUserid(),issueRequest.getAssigneeId()); + return ResponseEntity.ok(issue); } @GetMapping("/{status}") @@ -70,8 +78,9 @@ public ResponseEntity> issueCheckByStatus(@PathVariable("sta } @PatchMapping("/status") - public void issueChangeStatus(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueId){ - this.issueService.changeStatus(issueRequest.getUserid(),issueId); + public ResponseEntity issueChangeStatus(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueId){ + Issue issue = this.issueService.changeStatus(issueRequest.getUserid(),issueId); + return ResponseEntity.ok(issue); } @GetMapping("/assigned") diff --git a/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java b/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java index 5e11a6f..5f89042 100644 --- a/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java +++ b/src/main/java/se/issuetrackingsystem/issue/service/IssueService.java @@ -97,7 +97,7 @@ public Issue setAssignee(Long issueId, Long userId, Long assigneeId){ return issue; } - public void changeStatus(Long userId, Long issueId){ + 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); @@ -119,6 +119,7 @@ else if(issue.getStatus()== Issue.Status.REOPENED){ issue.setStatus(Issue.Status.CLOSE); } this.issueRepository.save(issue); + return issue; } public Optional candidateUser(Long issueId) { diff --git a/src/test/java/se/issuetrackingsystem/CommentControllerTests.java b/src/test/java/se/issuetrackingsystem/CommentControllerTests.java new file mode 100644 index 0000000..7bac1a8 --- /dev/null +++ b/src/test/java/se/issuetrackingsystem/CommentControllerTests.java @@ -0,0 +1,61 @@ +package se.issuetrackingsystem; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import se.issuetrackingsystem.comment.controller.CommentController; +import se.issuetrackingsystem.comment.domain.Comment; +import se.issuetrackingsystem.comment.dto.CommentRequest; +import se.issuetrackingsystem.comment.service.CommentService; + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@WebMvcTest(CommentController.class) +public class CommentControllerTests { + + @Autowired + MockMvc mvc; + + @MockBean + CommentService commentService; + + @Autowired + ObjectMapper objectMapper; + + @Test + @DisplayName("코멘트 생성") + void testCommentCreate() throws Exception { + CommentRequest request = new CommentRequest(); + request.setMessage("Test Message"); + request.setAuthorid(1L); + + when(commentService.create(anyLong(),anyString(),anyLong())).thenReturn(new Comment()); + + mvc.perform(post("/api/v1/comments") + .param("issueId", "1") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(request))) + .andExpect(status().isOk()); + } + + @Test + @DisplayName("코멘트 삭제") + void testCommentDelete() throws Exception { + //given + doNothing().when(commentService).delete(anyLong()); + + //when then + mvc.perform(delete("/api/v1/comments") + .param("commentId", "1")) + .andExpect(status().isOk()); + } + +} diff --git a/src/test/java/se/issuetrackingsystem/IssueServiceTests.java b/src/test/java/se/issuetrackingsystem/IssueServiceTests.java index fca5cab..5d36c3f 100644 --- a/src/test/java/se/issuetrackingsystem/IssueServiceTests.java +++ b/src/test/java/se/issuetrackingsystem/IssueServiceTests.java @@ -1,6 +1,4 @@ package se.issuetrackingsystem; - -import org.hamcrest.core.Is; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -79,7 +77,22 @@ void issueCreateNoProject() { //when then assertThrows(CustomException.class, ()-> issueService.create(project.getId(), "Issue1", "issue", user.getId(), Issue.Priority.MINOR)); + } + + @Test + @DisplayName("이슈 생성 실패-리포터_없음") + void issueCreateNoReporter() { + //given + Project project = mock(Project.class); + User user = mock(User.class); + + when(projectRepository.findById(project.getId())).thenReturn(Optional.of(project)); + when(userRepository.findById(user.getId())).thenReturn(Optional.empty()); + when(issueRepository.save(any(Issue.class))).thenAnswer(i -> i.getArgument(0)); + //when then + assertThrows(CustomException.class, + ()-> issueService.create(project.getId(), "Issue1", "issue", user.getId(), Issue.Priority.MINOR)); } @Test From d0eaf681640b0c6f5595fe77aa29f325fb129eba Mon Sep 17 00:00:00 2001 From: "Feel5nL\\Feel5nL" Date: Tue, 28 May 2024 20:23:20 +0900 Subject: [PATCH 5/6] =?UTF-8?q?fix:=20#24=20return=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/se/issuetrackingsystem/CommentControllerTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/se/issuetrackingsystem/CommentControllerTests.java b/src/test/java/se/issuetrackingsystem/CommentControllerTests.java index 7bac1a8..9bb41cd 100644 --- a/src/test/java/se/issuetrackingsystem/CommentControllerTests.java +++ b/src/test/java/se/issuetrackingsystem/CommentControllerTests.java @@ -35,7 +35,7 @@ public class CommentControllerTests { void testCommentCreate() throws Exception { CommentRequest request = new CommentRequest(); request.setMessage("Test Message"); - request.setAuthorid(1L); + request.setAuthorId(1L); when(commentService.create(anyLong(),anyString(),anyLong())).thenReturn(new Comment()); From 4f169ba73735a695c5bc00c776c0b21a3633e5ed Mon Sep 17 00:00:00 2001 From: 2522001 Date: Tue, 28 May 2024 20:40:32 +0900 Subject: [PATCH 6/6] =?UTF-8?q?fix:=20#24=20-=20Contributor=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/service/ProjectService.java | 2 +- .../user/domain/Admin.java | 14 ++++----- .../user/domain/Contributor.java | 30 ------------------- .../issuetrackingsystem/user/domain/Dev.java | 23 ++++++++++---- .../issuetrackingsystem/user/domain/PL.java | 23 ++++++++++---- .../user/domain/ProjectContributor.java | 4 +-- .../user/domain/Tester.java | 23 ++++++++++---- .../issuetrackingsystem/user/domain/User.java | 6 ++-- 8 files changed, 65 insertions(+), 60 deletions(-) delete mode 100644 src/main/java/se/issuetrackingsystem/user/domain/Contributor.java diff --git a/src/main/java/se/issuetrackingsystem/project/service/ProjectService.java b/src/main/java/se/issuetrackingsystem/project/service/ProjectService.java index 7c9f767..94ea4ca 100644 --- a/src/main/java/se/issuetrackingsystem/project/service/ProjectService.java +++ b/src/main/java/se/issuetrackingsystem/project/service/ProjectService.java @@ -76,7 +76,7 @@ protected void addContributors(List contributorIds, Project project) { List projectContributors = contributorIds .stream() .map(id -> { - Contributor contributor = (Contributor) userRepository.findById(id) + User contributor = userRepository.findById(id) .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); ProjectContributorPK projectContributorPK = new ProjectContributorPK(contributor.getId(), project.getId()); return new ProjectContributor(projectContributorPK, project, contributor); diff --git a/src/main/java/se/issuetrackingsystem/user/domain/Admin.java b/src/main/java/se/issuetrackingsystem/user/domain/Admin.java index 546dc21..2756bcd 100644 --- a/src/main/java/se/issuetrackingsystem/user/domain/Admin.java +++ b/src/main/java/se/issuetrackingsystem/user/domain/Admin.java @@ -18,37 +18,37 @@ public class Admin extends User { private List projects = new ArrayList<>(); @Override - boolean canManageProject() { + public boolean canManageProject() { return true; } @Override - boolean canManageComment() { + public boolean canManageComment() { return false; } @Override - boolean canManageIssue() { + public boolean canManageIssue() { return false; } @Override - boolean canSetAssignee() { + public boolean canSetAssignee() { return false; } @Override - boolean canChangeIssueStateAssignedToFixed() { + public boolean canChangeIssueStateAssignedToFixed() { return false; } @Override - boolean canChangeIssueStateFixedToResolved() { + public boolean canChangeIssueStateFixedToResolved() { return false; } @Override - boolean canChangeIssueStateResolvedToClosed() { + public boolean canChangeIssueStateResolvedToClosed() { return false; } diff --git a/src/main/java/se/issuetrackingsystem/user/domain/Contributor.java b/src/main/java/se/issuetrackingsystem/user/domain/Contributor.java deleted file mode 100644 index 311a01c..0000000 --- a/src/main/java/se/issuetrackingsystem/user/domain/Contributor.java +++ /dev/null @@ -1,30 +0,0 @@ -package se.issuetrackingsystem.user.domain; - -import jakarta.persistence.*; -import lombok.NoArgsConstructor; - -import java.util.List; - -@Entity -@Inheritance(strategy = InheritanceType.SINGLE_TABLE) -@DiscriminatorColumn(name = "role") -@NoArgsConstructor -public abstract class Contributor extends User { - - @OneToMany(mappedBy = "contributor") - private List projectContributors; - - @Override - public boolean canManageProject() { - return false; - } - - @Override - public boolean canManageComment() { - return true; - } - - public Contributor(String username, String password) { - super(username, password); - } -} diff --git a/src/main/java/se/issuetrackingsystem/user/domain/Dev.java b/src/main/java/se/issuetrackingsystem/user/domain/Dev.java index 8888122..5e3cbc2 100644 --- a/src/main/java/se/issuetrackingsystem/user/domain/Dev.java +++ b/src/main/java/se/issuetrackingsystem/user/domain/Dev.java @@ -7,29 +7,40 @@ @Entity @DiscriminatorValue("Dev") @NoArgsConstructor -public class Dev extends Contributor { +public class Dev extends User { + + @Override + public boolean canManageProject() { + return false; + } + + @Override + public boolean canManageComment() { + return true; + } + @Override - boolean canManageIssue() { + public boolean canManageIssue() { return false; } @Override - boolean canSetAssignee() { + public boolean canSetAssignee() { return false; } @Override - boolean canChangeIssueStateAssignedToFixed() { + public boolean canChangeIssueStateAssignedToFixed() { return true; } @Override - boolean canChangeIssueStateFixedToResolved() { + public boolean canChangeIssueStateFixedToResolved() { return false; } @Override - boolean canChangeIssueStateResolvedToClosed() { + public boolean canChangeIssueStateResolvedToClosed() { return false; } diff --git a/src/main/java/se/issuetrackingsystem/user/domain/PL.java b/src/main/java/se/issuetrackingsystem/user/domain/PL.java index 87263bf..a717f0c 100644 --- a/src/main/java/se/issuetrackingsystem/user/domain/PL.java +++ b/src/main/java/se/issuetrackingsystem/user/domain/PL.java @@ -7,29 +7,40 @@ @Entity @DiscriminatorValue("PL") @NoArgsConstructor -public class PL extends Contributor { +public class PL extends User { + + @Override + public boolean canManageProject() { + return false; + } + + @Override + public boolean canManageComment() { + return true; + } + @Override - boolean canManageIssue() { + public boolean canManageIssue() { return false; } @Override - boolean canSetAssignee() { + public boolean canSetAssignee() { return true; } @Override - boolean canChangeIssueStateAssignedToFixed() { + public boolean canChangeIssueStateAssignedToFixed() { return false; } @Override - boolean canChangeIssueStateFixedToResolved() { + public boolean canChangeIssueStateFixedToResolved() { return false; } @Override - boolean canChangeIssueStateResolvedToClosed() { + public boolean canChangeIssueStateResolvedToClosed() { return true; } diff --git a/src/main/java/se/issuetrackingsystem/user/domain/ProjectContributor.java b/src/main/java/se/issuetrackingsystem/user/domain/ProjectContributor.java index 4da1d82..5037919 100644 --- a/src/main/java/se/issuetrackingsystem/user/domain/ProjectContributor.java +++ b/src/main/java/se/issuetrackingsystem/user/domain/ProjectContributor.java @@ -21,9 +21,9 @@ public class ProjectContributor { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "contributor_id") @MapsId("contributorId") - private Contributor contributor; + private User contributor; - public ProjectContributor(ProjectContributorPK projectContributorPK, Project project, Contributor contributor) { + public ProjectContributor(ProjectContributorPK projectContributorPK, Project project, User contributor) { this.projectContributorPK = projectContributorPK; this.project = project; this.contributor = contributor; diff --git a/src/main/java/se/issuetrackingsystem/user/domain/Tester.java b/src/main/java/se/issuetrackingsystem/user/domain/Tester.java index 871b785..45866c0 100644 --- a/src/main/java/se/issuetrackingsystem/user/domain/Tester.java +++ b/src/main/java/se/issuetrackingsystem/user/domain/Tester.java @@ -7,29 +7,40 @@ @Entity @DiscriminatorValue("Tester") @NoArgsConstructor -public class Tester extends Contributor { +public class Tester extends User { + + @Override + public boolean canManageProject() { + return false; + } + + @Override + public boolean canManageComment() { + return true; + } + @Override - boolean canManageIssue() { + public boolean canManageIssue() { return true; } @Override - boolean canSetAssignee() { + public boolean canSetAssignee() { return false; } @Override - boolean canChangeIssueStateAssignedToFixed() { + public boolean canChangeIssueStateAssignedToFixed() { return false; } @Override - boolean canChangeIssueStateFixedToResolved() { + public boolean canChangeIssueStateFixedToResolved() { return true; } @Override - boolean canChangeIssueStateResolvedToClosed() { + public boolean canChangeIssueStateResolvedToClosed() { return false; } diff --git a/src/main/java/se/issuetrackingsystem/user/domain/User.java b/src/main/java/se/issuetrackingsystem/user/domain/User.java index ca1b8e5..e86f021 100644 --- a/src/main/java/se/issuetrackingsystem/user/domain/User.java +++ b/src/main/java/se/issuetrackingsystem/user/domain/User.java @@ -4,10 +4,9 @@ import lombok.*; import java.time.LocalDateTime; +import java.util.List; @Entity(name = "users") -@Inheritance(strategy = InheritanceType.SINGLE_TABLE) -@DiscriminatorColumn(name = "role") @Getter @NoArgsConstructor public abstract class User { @@ -26,6 +25,9 @@ public abstract class User { @Column(nullable = false) private LocalDateTime createdAt; + @OneToMany(mappedBy = "contributor") + private List projectContributors; + abstract boolean canManageProject(); // Admin abstract boolean canManageComment(); // PL, Dev, Tester abstract boolean canManageIssue(); // Tester