Exemplo n.º 1
0
        public async Task <IActionResult> Post(ProductInputModel model)
        {
            var product = model.ToProduct();

            var addResult = await _productDataRepository.AddProductAsync(product);

            if (addResult.IsSuccess)
            {
                return(CreatedAtAction(nameof(Get), new { id = product.Id }, ProductOutputModel.FromProduct(product)));
            }

            if (addResult.IsDuplicate)
            {
                var existingUrl = Url.Action(nameof(Get),
                                             ControllerContext.ActionDescriptor.ControllerName,
                                             new { id = product.Id },
                                             Request.Scheme,
                                             Request.Host.ToUriComponent());

                HttpContext.Response.Headers.Add("Location", existingUrl);
                return(StatusCode(StatusCodes.Status409Conflict));
            }

            ModelState.AddValidationResultErrors(addResult.ValidationResult.Errors);

            return(ValidationProblem());
        }
Exemplo n.º 2
0
        public async Task <ActionResult <ProductOutputModel> > Get([Required] Guid id)
        {
            var product = await _productDataRepository.GetByIdAsync(id);

            if (product is null)
            {
                return(NotFound());
            }

            return(ProductOutputModel.FromProduct(product));
        }
Exemplo n.º 3
0
        public async Task <ActionResult <ProductOutputModel[]> > GetAll()
        {
            var products = await _productDataRepository.GetProductsAsync();

            return(products.Select(p => ProductOutputModel.FromProduct(p)).ToArray());
        }