public async Task <IActionResult> Put([FromBody] ProjectType projectType)
        {
            if (projectType is null)
            {
                throw new ArgumentNullException(nameof(projectType));
            }

            var validation = new ProjectTypeValidator().Validate(projectType);

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

            var existingProjectType = await projectTypesRepository
                                      .GetAsync(projectType.Id)
                                      .ConfigureAwait(false);

            if (existingProjectType is null)
            {
                return(ErrorResult
                       .NotFound($"A ProjectType with the ID '{projectType.Id}' could not be found in this TeamCloud Instance")
                       .ActionResult());
            }

            var providers = await providersRepository
                            .ListAsync()
                            .ToListAsync()
                            .ConfigureAwait(false);

            var validProviders = projectType.Providers
                                 .All(p => providers.Any(provider => provider.Id == p.Id));

            if (!validProviders)
            {
                var validProviderIds = string.Join(", ", providers.Select(p => p.Id));
                return(ErrorResult
                       .BadRequest(new ValidationError {
                    Field = "projectType", Message = $"All provider ids on a ProjectType must match the id of a registered Provider on the TeamCloud instance. Valid provider ids are: {validProviderIds}"
                })
                       .ActionResult());
            }

            existingProjectType.PopulateFromExternalModel(projectType);

            var updateResult = await orchestrator
                               .UpdateAsync(existingProjectType)
                               .ConfigureAwait(false);

            var returnUpdateResult = updateResult.PopulateExternalModel();

            return(DataResult <ProjectType>
                   .Ok(returnUpdateResult)
                   .ActionResult());
        }
        public async Task <IActionResult> Post([FromBody] ProjectType projectType)
        {
            if (projectType is null)
            {
                throw new ArgumentNullException(nameof(projectType));
            }

            var validation = new ProjectTypeValidator().Validate(projectType);

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

            var existingProjectType = await projectTypesRepository
                                      .GetAsync(projectType.Id)
                                      .ConfigureAwait(false);

            if (existingProjectType != null)
            {
                return(ErrorResult
                       .Conflict($"A ProjectType with id '{projectType.Id}' already exists.  Please try your request again with a unique id or call PUT to update the existing ProjectType.")
                       .ActionResult());
            }

            var teamCloud = await teamCloudRepository
                            .GetAsync()
                            .ConfigureAwait(false);

            var validProviders = projectType.Providers
                                 .All(projectTypeProvider => teamCloud.Providers.Any(teamCloudProvider => teamCloudProvider.Id == projectTypeProvider.Id));

            if (!validProviders)
            {
                var validProviderIds = string.Join(", ", teamCloud.Providers.Select(p => p.Id));
                return(ErrorResult
                       .BadRequest(new ValidationError {
                    Field = "projectType", Message = $"All provider ids on a ProjectType must match the id of a registered Provider on the TeamCloud instance. Valid provider ids are: {validProviderIds}"
                })
                       .ActionResult());
            }

            var addResult = await orchestrator
                            .AddAsync(projectType)
                            .ConfigureAwait(false);

            var baseUrl  = HttpContext.GetApplicationBaseUrl();
            var location = new Uri(baseUrl, $"api/projectTypes/{addResult.Id}").ToString();

            return(DataResult <ProjectType>
                   .Created(addResult, location)
                   .ActionResult());
        }
Esempio n. 3
0
        public async Task <IActionResult> Put([FromRoute] string projectTypeId, [FromBody] ProjectType projectType)
        {
            if (projectType is null)
            {
                throw new ArgumentNullException(nameof(projectType));
            }

            if (string.IsNullOrWhiteSpace(projectTypeId))
            {
                return(ErrorResult
                       .BadRequest($"The identifier '{projectTypeId}' provided in the url path is invalid.  Must be a valid project type ID.", ResultErrorCode.ValidationError)
                       .ActionResult());
            }

            var validation = new ProjectTypeValidator().Validate(projectType);

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

            if (!projectTypeId.Equals(projectType.Id, StringComparison.OrdinalIgnoreCase))
            {
                return(ErrorResult
                       .BadRequest(new ValidationError {
                    Field = "id", Message = $"ProjectType's id does match the identifier provided in the path."
                })
                       .ActionResult());
            }

            var existingProjectType = await projectTypesRepository
                                      .GetAsync(projectType.Id)
                                      .ConfigureAwait(false);

            if (existingProjectType is null)
            {
                return(ErrorResult
                       .NotFound($"A ProjectType with the ID '{projectType.Id}' could not be found in this TeamCloud Instance")
                       .ActionResult());
            }

            var providers = await providersRepository
                            .ListAsync()
                            .ToListAsync()
                            .ConfigureAwait(false);

            var validProviders = projectType.Providers
                                 .All(p => providers.Any(provider => provider.Id == p.Id));

            if (!validProviders)
            {
                var validProviderIds = string.Join(", ", providers.Select(p => p.Id));
                return(ErrorResult
                       .BadRequest(new ValidationError {
                    Field = "projectType", Message = $"All provider ids on a ProjectType must match the id of a registered Provider on the TeamCloud instance. Valid provider ids are: {validProviderIds}"
                })
                       .ActionResult());
            }

            existingProjectType
            .PopulateFromExternalModel(projectType);

            var updateResult = await orchestrator
                               .UpdateAsync(existingProjectType)
                               .ConfigureAwait(false);

            var returnUpdateResult = updateResult.PopulateExternalModel();

            return(DataResult <ProjectType>
                   .Ok(returnUpdateResult)
                   .ActionResult());
        }