Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. package net.croz.charter.controller;
  2.  
  3. import lombok.extern.slf4j.Slf4j;
  4. import net.croz.charter.core.api.project.InitProjectCommand;
  5. import net.croz.charter.core.api.project.resource.AddResourcesCommand;
  6. import net.croz.charter.core.api.project.resource.BindRoleToResourcesCommand;
  7. import net.croz.charter.core.api.project.role.AddRolesCommand;
  8. import net.croz.charter.core.api.project.role.BindUsersToRoleCommand;
  9. import net.croz.charter.dto.project.ProjectDTO;
  10. import net.croz.charter.dto.project.ProjectResourcesDTO;
  11. import net.croz.charter.dto.project.RoleDTO;
  12. import net.croz.charter.query.project.Project;
  13. import net.croz.charter.query.project.resource.ProjectResource;
  14. import net.croz.charter.query.project.role.Role;
  15. import net.croz.charter.services.repository.RepositoryService;
  16. import org.axonframework.commandhandling.gateway.CommandGateway;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.http.HttpStatus;
  19. import org.springframework.http.ResponseEntity;
  20. import org.springframework.web.bind.annotation.*;
  21.  
  22. import java.util.Collection;
  23. import java.util.List;
  24.  
  25. @RestController
  26. @Slf4j
  27. @RequestMapping("/api/projects")
  28. public class ProjectController {
  29.  
  30. @Autowired
  31. private CommandGateway gateway;
  32. @Autowired
  33. RepositoryService repositoryService;
  34.  
  35. @PostMapping
  36. public ResponseEntity<Void> createNewProject(@RequestBody ProjectDTO project) {
  37. gateway.sendAndWait(new InitProjectCommand(project.getName(), project));
  38. return new ResponseEntity<>(HttpStatus.CREATED);
  39. }
  40.  
  41. @PostMapping(value = "/roles")
  42. public ResponseEntity<Void> addRoles(@RequestParam(name = "project-name") String projectName,
  43. @RequestBody RoleDTO role) {
  44.  
  45. repositoryService.checkIfProjectExists(projectName);
  46. gateway.sendAndWait(new AddRolesCommand(projectName, role));
  47. return new ResponseEntity<>(HttpStatus.OK);
  48. }
  49.  
  50. @PostMapping(value = "/project/{projectName}/resources")
  51. public ResponseEntity<String> addResourcesToProject(@PathVariable(name = "projectName") String projectName,
  52. @RequestBody ProjectResourcesDTO projectResourcesDTO) {
  53.  
  54. try {
  55. repositoryService.checkIfProjectExists(projectName);
  56. gateway.sendAndWait(new AddResourcesCommand(projectName, projectResourcesDTO.getResources()));
  57. return new ResponseEntity<>(HttpStatus.OK);
  58. } catch (Exception e) {
  59. log.error("error: {}, message: {}", e.getClass(), e.getMessage());
  60. return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
  61. }
  62. }
  63.  
  64. @PostMapping(value = "/project/{projectName}/{roleName}/resources")
  65. public ResponseEntity<String> addResources(@PathVariable(name = "projectName") String projectName,
  66. @PathVariable(name = "roleName") String role,
  67. @RequestBody List<String> resources) {
  68.  
  69. try {
  70. repositoryService.checkIfProjectExists(projectName);
  71. resources.forEach(resourceName -> repositoryService.checkIfResourceExists(resourceName));
  72. repositoryService.checkIfRoleExists(projectName, role);
  73. gateway.sendAndWait(new BindRoleToResourcesCommand(projectName, role, resources));
  74. return new ResponseEntity<>(HttpStatus.OK);
  75. } catch (Exception e) {
  76. log.error("error: {}, message: {}", e.getClass(), e.getMessage());
  77. return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
  78. }
  79. }
  80.  
  81. @PostMapping(value = "/project/{projectName}/users")
  82. public ResponseEntity<String> addUserToRole(@PathVariable(name = "projectName") String projectName,
  83. @RequestParam(value = "role") String role,
  84. @RequestBody List<String> users) {
  85.  
  86. try {
  87. repositoryService.checkIfProjectExists(projectName);
  88. users.forEach(userName -> repositoryService.checkIfUserExists(userName));
  89. repositoryService.checkIfRoleExists(projectName, role);
  90. gateway.sendAndWait(new BindUsersToRoleCommand(projectName, role, users));
  91. return new ResponseEntity<>(HttpStatus.OK);
  92. } catch (Exception e) {
  93. log.error("error: {}, message: {}", e.getClass(), e.getMessage());
  94. return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
  95. }
  96. }
  97.  
  98. @RequestMapping(value = "/project")
  99. public ResponseEntity<Collection<Project>> getByStatus(@RequestParam(value = "status") String status) {
  100. try {
  101. return new ResponseEntity<>(
  102. repositoryService.projectRepository.findAllByStatus(status.toUpperCase()), HttpStatus.OK);
  103. } catch (Exception e) {
  104. log.error("error: {}, message: {}", e.getClass(), e.getMessage());
  105. return new ResponseEntity(e.getMessage(), HttpStatus.SERVICE_UNAVAILABLE);
  106. }
  107. }
  108.  
  109. @RequestMapping(value = "/project/user")
  110. public ResponseEntity<Collection<Project>> getPmProjects(@RequestParam(value = "pm") String pmName) {
  111. try {
  112. repositoryService.checkIfUserExists(pmName);
  113. return new ResponseEntity<>(
  114. repositoryService.userRepository.findByUsername(pmName).getProjects(),
  115. HttpStatus.OK);
  116. } catch (Exception e) {
  117. log.error("error: {}, message: {}", e.getClass(), e.getMessage());
  118. return new ResponseEntity(e.getMessage(), HttpStatus.SERVICE_UNAVAILABLE);
  119. }
  120. }
  121.  
  122. @RequestMapping(value = "/project/{projectName}/resources")
  123. public ResponseEntity<Collection<ProjectResource>> getProjectResources(@PathVariable(name = "projectName") String projectName) {
  124. try {
  125. repositoryService.checkIfProjectExists(projectName);
  126. return new ResponseEntity<>(
  127. repositoryService.projectResourceRepository.findByProject(
  128. repositoryService.projectRepository.findByName(projectName)), HttpStatus.OK);
  129. } catch (Exception e) {
  130. log.error("error: {}, message: {}", e.getClass(), e.getMessage());
  131. return new ResponseEntity(e.getMessage(), HttpStatus.SERVICE_UNAVAILABLE);
  132. }
  133. }
  134.  
  135. @RequestMapping(value = "/project/{projectName}/roles")
  136. public ResponseEntity<Collection<Role>> getProjectRoles(@PathVariable(name = "projectName") String projectName) {
  137. try {
  138. repositoryService.checkIfProjectExists(projectName);
  139. return new ResponseEntity(
  140. repositoryService.projectResourceRepository.findByProject(
  141. repositoryService.projectRepository.findByName(projectName)), HttpStatus.OK);
  142. } catch (Exception e) {
  143. log.error("error: {}, message: {}", e.getClass(), e.getMessage());
  144. return new ResponseEntity(e.getMessage(), HttpStatus.SERVICE_UNAVAILABLE);
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement