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());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Post([FromBody] ProviderData providerData)
        {
            if (providerData is null)
            {
                throw new ArgumentNullException(nameof(providerData));
            }

            var validation = new ProviderDataValidator().Validate(providerData);

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

            var provider = await providersRepository
                           .GetAsync(ProviderId)
                           .ConfigureAwait(false);

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

            var newProviderData = new ProviderDataDocument
            {
                ProviderId = provider.Id,
                Scope      = ProviderDataScope.System
            };

            newProviderData.PopulateFromExternalModel(providerData);

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

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

            var returnAddResult = addResult.PopulateExternalModel();

            return(DataResult <ProviderData>
                   .Created(returnAddResult, location)
                   .ActionResult());
        }