Esempio n. 1
0
        public async Task <IActionResult> Put([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 oldProviderData = await providerDataRepository
                                  .GetAsync(providerData.Id)
                                  .ConfigureAwait(false);

            if (oldProviderData is null)
            {
                return(ErrorResult
                       .NotFound($"The Provider Data '{providerData.Id}' could not be found..")
                       .ActionResult());
            }

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

            newProviderData.PopulateFromExternalModel(providerData);

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

            var returnUpdateResult = updateResult.PopulateExternalModel();

            return(DataResult <ProviderData>
                   .Ok(returnUpdateResult)
                   .ActionResult());
        }
Esempio 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());
        }