public IActionResult CreateProduct([FromBody] ProductForCreationDTO product)
        {
            if (product == null)
            {
                return(BadRequest());
            }

            // Validate data
            if (!ModelState.IsValid)
            {
                // return 422
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var productEntity = Mapper.Map <Product>(product);

            _supermarketRepository.AddProduct(productEntity);

            if (!_supermarketRepository.Save())
            {
                throw new Exception("Creating a product failed on Save.");
            }

            var productToReturn = Mapper.Map <ProductDTO>(productEntity);

            var links = CreateLinksForProduct(productToReturn.ProductId, null);

            var linkedResourceToReturn = productToReturn.ShapeData(null)
                                         as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);

            return(CreatedAtRoute("GetProduct", new { ProductId = linkedResourceToReturn["ProductId"] }, linkedResourceToReturn));
        }
예제 #2
0
        public async Task <IActionResult> CreateProductForCategory(Guid categoryId, [FromBody] ProductForCreationDTO product)
        {
            var productEntity = _mapper.Map <Product>(product);
            await _productService.CreateProductForCategoryAsync(categoryId, productEntity);

            var productToReturn = _mapper.Map <ProductDTO>(productEntity);

            return(CreatedAtRoute("GetProductForCategory",
                                  new { categoryId, productId = productToReturn.Id }, productToReturn));
        }
예제 #3
0
        public async Task <IActionResult> CreateProduct([FromBody] ProductForCreationDTO product)
        {
            if (product is null)
            {
                logger.LogError($"Product object sent from client is null");
                return(BadRequest("Product object is null"));
            }

            Product productEntity = mapper.Map <ProductForCreationDTO, Product>(product);

            repository.Product.Create(productEntity);
            await repository.SaveAsync();

            ProductDTO createdProduct = mapper.Map <Product, ProductDTO>(productEntity);

            return(Ok(createdProduct));
        }
예제 #4
0
 public CreateDraftProductCommand(ProductForCreationDTO product) =>
 this.Model = product ?? throw new ArgumentNullException(nameof(product));
 public async Task <ActionResult> Post([FromBody] ProductForCreationDTO model) =>
 CreatedAtRoute("GetProductById", new { id = await this.Mediator.Send(new CreateDraftProductCommand(model)) }, model);