Exemplo n.º 1
0
        public async Task UpdateProductAsync(string code, string eTag, Product.Update product)
        {
            using (var content = new StringContent(JsonConvert.SerializeObject(product), Encoding.UTF8, "application/json"))
            {
                var request = new HttpRequestMessage(HttpMethod.Put, new Uri(this.BaseAddress, code));
                request.Headers.TryAddWithoutValidation(HttpConstants.Headers.IfMatch, eTag);
                request.Content = content;

                using (var response = await this.SendAsync(request))
                {
                    if (response.StatusCode != HttpStatusCode.Accepted)
                    {
                        // TODO : Better threat failures for UI
                        var result = await response.Content.ReadAsStringAsync();

                        throw new InvalidOperationException($"Product was not updated due to '{response.ReasonPhrase}'\n {result}.");
                    }
                }
            }
        }
        public async Task <IActionResult> Put([FromHeader(Name = "If-Match")] string eTag, string code, Product.Update model)
        {
            int productVersion = await _productOrchestrator.GetProductVersionByCodeAsync(code);

            if (!_eTagProvider.ValidatePayloadHash(productVersion, eTag))
            {
                throw new ConcurrencyValidationException($"Product '{code}' state has changed, reload it for latest version.");
            }

            await _productOrchestrator.UpdateProductAsync(code, model);

            return(AcceptedAtAction(nameof(GetAsync), new { code }, null));
        }