Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.croz.charter.controller;
- import lombok.extern.slf4j.Slf4j;
- import net.croz.charter.core.api.project.InitProjectCommand;
- import net.croz.charter.core.api.project.resource.AddResourcesCommand;
- import net.croz.charter.core.api.project.resource.BindRoleToResourcesCommand;
- import net.croz.charter.core.api.project.role.AddRolesCommand;
- import net.croz.charter.core.api.project.role.BindUsersToRoleCommand;
- import net.croz.charter.dto.project.ProjectDTO;
- import net.croz.charter.dto.project.ProjectResourcesDTO;
- import net.croz.charter.dto.project.RoleDTO;
- import net.croz.charter.query.project.Project;
- import net.croz.charter.query.project.resource.ProjectResource;
- import net.croz.charter.query.project.role.Role;
- import net.croz.charter.services.repository.RepositoryService;
- import org.axonframework.commandhandling.gateway.CommandGateway;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.*;
- import java.util.Collection;
- import java.util.List;
- @RestController
- @Slf4j
- @RequestMapping("/api/projects")
- public class ProjectController {
- @Autowired
- private CommandGateway gateway;
- @Autowired
- RepositoryService repositoryService;
- @PostMapping
- public ResponseEntity<Void> createNewProject(@RequestBody ProjectDTO project) {
- gateway.sendAndWait(new InitProjectCommand(project.getName(), project));
- return new ResponseEntity<>(HttpStatus.CREATED);
- }
- @PostMapping(value = "/roles")
- public ResponseEntity<Void> addRoles(@RequestParam(name = "project-name") String projectName,
- @RequestBody RoleDTO role) {
- repositoryService.checkIfProjectExists(projectName);
- gateway.sendAndWait(new AddRolesCommand(projectName, role));
- return new ResponseEntity<>(HttpStatus.OK);
- }
- @PostMapping(value = "/project/{projectName}/resources")
- public ResponseEntity<String> addResourcesToProject(@PathVariable(name = "projectName") String projectName,
- @RequestBody ProjectResourcesDTO projectResourcesDTO) {
- try {
- repositoryService.checkIfProjectExists(projectName);
- gateway.sendAndWait(new AddResourcesCommand(projectName, projectResourcesDTO.getResources()));
- return new ResponseEntity<>(HttpStatus.OK);
- } catch (Exception e) {
- log.error("error: {}, message: {}", e.getClass(), e.getMessage());
- return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
- }
- }
- @PostMapping(value = "/project/{projectName}/{roleName}/resources")
- public ResponseEntity<String> addResources(@PathVariable(name = "projectName") String projectName,
- @PathVariable(name = "roleName") String role,
- @RequestBody List<String> resources) {
- try {
- repositoryService.checkIfProjectExists(projectName);
- resources.forEach(resourceName -> repositoryService.checkIfResourceExists(resourceName));
- repositoryService.checkIfRoleExists(projectName, role);
- gateway.sendAndWait(new BindRoleToResourcesCommand(projectName, role, resources));
- return new ResponseEntity<>(HttpStatus.OK);
- } catch (Exception e) {
- log.error("error: {}, message: {}", e.getClass(), e.getMessage());
- return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
- }
- }
- @PostMapping(value = "/project/{projectName}/users")
- public ResponseEntity<String> addUserToRole(@PathVariable(name = "projectName") String projectName,
- @RequestParam(value = "role") String role,
- @RequestBody List<String> users) {
- try {
- repositoryService.checkIfProjectExists(projectName);
- users.forEach(userName -> repositoryService.checkIfUserExists(userName));
- repositoryService.checkIfRoleExists(projectName, role);
- gateway.sendAndWait(new BindUsersToRoleCommand(projectName, role, users));
- return new ResponseEntity<>(HttpStatus.OK);
- } catch (Exception e) {
- log.error("error: {}, message: {}", e.getClass(), e.getMessage());
- return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
- }
- }
- @RequestMapping(value = "/project")
- public ResponseEntity<Collection<Project>> getByStatus(@RequestParam(value = "status") String status) {
- try {
- return new ResponseEntity<>(
- repositoryService.projectRepository.findAllByStatus(status.toUpperCase()), HttpStatus.OK);
- } catch (Exception e) {
- log.error("error: {}, message: {}", e.getClass(), e.getMessage());
- return new ResponseEntity(e.getMessage(), HttpStatus.SERVICE_UNAVAILABLE);
- }
- }
- @RequestMapping(value = "/project/user")
- public ResponseEntity<Collection<Project>> getPmProjects(@RequestParam(value = "pm") String pmName) {
- try {
- repositoryService.checkIfUserExists(pmName);
- return new ResponseEntity<>(
- repositoryService.userRepository.findByUsername(pmName).getProjects(),
- HttpStatus.OK);
- } catch (Exception e) {
- log.error("error: {}, message: {}", e.getClass(), e.getMessage());
- return new ResponseEntity(e.getMessage(), HttpStatus.SERVICE_UNAVAILABLE);
- }
- }
- @RequestMapping(value = "/project/{projectName}/resources")
- public ResponseEntity<Collection<ProjectResource>> getProjectResources(@PathVariable(name = "projectName") String projectName) {
- try {
- repositoryService.checkIfProjectExists(projectName);
- return new ResponseEntity<>(
- repositoryService.projectResourceRepository.findByProject(
- repositoryService.projectRepository.findByName(projectName)), HttpStatus.OK);
- } catch (Exception e) {
- log.error("error: {}, message: {}", e.getClass(), e.getMessage());
- return new ResponseEntity(e.getMessage(), HttpStatus.SERVICE_UNAVAILABLE);
- }
- }
- @RequestMapping(value = "/project/{projectName}/roles")
- public ResponseEntity<Collection<Role>> getProjectRoles(@PathVariable(name = "projectName") String projectName) {
- try {
- repositoryService.checkIfProjectExists(projectName);
- return new ResponseEntity(
- repositoryService.projectResourceRepository.findByProject(
- repositoryService.projectRepository.findByName(projectName)), HttpStatus.OK);
- } catch (Exception e) {
- log.error("error: {}, message: {}", e.getClass(), e.getMessage());
- return new ResponseEntity(e.getMessage(), HttpStatus.SERVICE_UNAVAILABLE);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement