Ejemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] ProjectDefinition projectDefinition)
        {
            if (projectDefinition is null)
            {
                throw new ArgumentNullException(nameof(projectDefinition));
            }

            var validation = new ProjectDefinitionValidator().Validate(projectDefinition);

            if (!validation.IsValid)
            {
                return(ErrorResult
                       .BadRequest(validation)
                       .ToActionResult());
            }

            var nameExists = await ProjectRepository
                             .NameExistsAsync(projectDefinition.Name)
                             .ConfigureAwait(false);

            if (nameExists)
            {
                return(ErrorResult
                       .Conflict($"A Project with name '{projectDefinition.Name}' already exists. Project names must be unique. Please try your request again with a unique name.")
                       .ToActionResult());
            }

            var projectId = Guid.NewGuid().ToString();

            var users = await ResolveUsersAsync(projectDefinition, projectId)
                        .ConfigureAwait(false);

            var project = new ProjectDocument
            {
                Id         = projectId,
                Users      = users,
                Name       = projectDefinition.Name,
                Tags       = projectDefinition.Tags,
                Properties = projectDefinition.Properties
            };

            if (!string.IsNullOrEmpty(projectDefinition.ProjectType))
            {
                project.Type = await projectTypeRepository
                               .GetAsync(projectDefinition.ProjectType)
                               .ConfigureAwait(false);

                if (project.Type is null)
                {
                    return(ErrorResult
                           .BadRequest(new ValidationError {
                        Field = "projectType", Message = $"A Project Type with the ID '{projectDefinition.ProjectType}' could not be found in this TeamCloud Instance. Please try your request again with a valid Project Type ID for 'projectType'."
                    })
                           .ToActionResult());
                }
            }
            else
            {
                project.Type = await projectTypeRepository
                               .GetDefaultAsync()
                               .ConfigureAwait(false);

                if (project.Type is null)
                {
                    return(ErrorResult
                           .BadRequest(new ValidationError {
                        Field = "projectType", Message = $"No value was provided for 'projectType' and there is no a default Project Type set for this TeamCloud Instance. Please try your request again with a valid Project Type ID for 'projectType'."
                    })
                           .ToActionResult());
                }
            }

            var currentUser = users.FirstOrDefault(u => u.Id == UserService.CurrentUserId);

            var command = new OrchestratorProjectCreateCommand(currentUser, project);

            return(await Orchestrator
                   .InvokeAndReturnActionResultAsync <ProjectDocument, Project>(command, Request)
                   .ConfigureAwait(false));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Post([FromBody] ProjectDefinition projectDefinition)
        {
            if (projectDefinition is null)
            {
                throw new ArgumentNullException(nameof(projectDefinition));
            }

            var validation = new ProjectDefinitionValidator().Validate(projectDefinition);

            if (!validation.IsValid)
            {
                return(ErrorResult
                       .BadRequest(validation)
                       .ActionResult());
            }

            var users = await ResolveUsersAsync(projectDefinition)
                        .ConfigureAwait(false);

            var nameExists = await projectsRepository
                             .NameExistsAsync(projectDefinition.Name)
                             .ConfigureAwait(false);

            if (nameExists)
            {
                return(ErrorResult
                       .Conflict($"A Project with name '{projectDefinition.Name}' already exists. Project names must be unique. Please try your request again with a unique name.")
                       .ActionResult());
            }

            var project = new Project
            {
                Id    = Guid.NewGuid(),
                Users = users,
                Name  = projectDefinition.Name,
                Tags  = projectDefinition.Tags
            };

            if (!string.IsNullOrEmpty(projectDefinition.ProjectType))
            {
                project.Type = await projectTypesRepository
                               .GetAsync(projectDefinition.ProjectType)
                               .ConfigureAwait(false);

                if (project.Type is null)
                {
                    return(ErrorResult
                           .BadRequest(new ValidationError {
                        Field = "projectType", Message = $"A Project Type with the ID '{projectDefinition.ProjectType}' could not be found in this TeamCloud Instance. Please try your request again with a valid Project Type ID for 'projectType'."
                    })
                           .ActionResult());
                }
            }
            else
            {
                project.Type = await projectTypesRepository
                               .GetDefaultAsync()
                               .ConfigureAwait(false);

                if (project.Type is null)
                {
                    return(ErrorResult
                           .BadRequest(new ValidationError {
                        Field = "projectType", Message = $"No value was provided for 'projectType' and there is no a default Project Type set for this TeamCloud Instance. Please try your request again with a valid Project Type ID for 'projectType'."
                    })
                           .ActionResult());
                }
            }

            var command = new OrchestratorProjectCreateCommand(CurrentUser, project);

            var commandResult = await orchestrator
                                .InvokeAsync(command)
                                .ConfigureAwait(false);

            if (commandResult.Links.TryGetValue("status", out var statusUrl))
            {
                return(StatusResult
                       .Accepted(commandResult.CommandId.ToString(), statusUrl, commandResult.RuntimeStatus.ToString(), commandResult.CustomStatus)
                       .ActionResult());
            }

            throw new Exception("This shouldn't happen, but we need to decide to do when it does.");
        }