Example #1
0
        /*action: Patch
         * content-type: application/json-patch+json
         * sample body:
         * [{
         *      "op": "replace",
         *      "path": "/colordescription",
         *      "value": "patch desc repalce"
         *  }]
         */
        public async Task <IActionResult> PartiallyUpdateProductColor(int productid, int colorid,
                                                                      JsonPatchDocument <ProductColorForUpdDto> patchDocument)
        {
            if (!_productService.ExistsProduct(productid))
            {
                return(NotFound());
            }

            if (productid <= 0 || colorid <= 0)
            {
                return(NotFound());
            }

            var productcolor = await _productColorService.GetProductColorByProductAndColorIdAsync(productid, colorid);


            if (productcolor == null) // with upsert - we can add new productcolor here
            {
                var productcolorUpdDto = new ProductColorForUpdDto();
                patchDocument.ApplyTo(productcolorUpdDto, ModelState);
                if (!TryValidateModel(productcolorUpdDto))
                {
                    return(ValidationProblem(ModelState));
                }
                var productcolorToAdd = _mapper.Map <ProductColor>(productcolorUpdDto);
                productcolorToAdd.ProductId = productid;
                productcolorToAdd.ColorId   = colorid;
                await _productColorService.AddProductColorAsync(productcolorToAdd);

                await _productColorService.CommitAsync();

                var productcolorDto = _mapper.Map <ProductColorDto>(productcolorToAdd);
                return(CreatedAtRoute("GetProductcolor", new { productid, colorid }, productcolorDto));
            }

            var productcolorToPatch = _mapper.Map <ProductColorForUpdDto>(productcolor);

            patchDocument.ApplyTo(productcolorToPatch, ModelState);
            //after apply pathdocument, we need to validate ojbect
            if (!TryValidateModel(productcolorToPatch))
            {
                return(ValidationProblem(ModelState));
            }

            _mapper.Map(productcolorToPatch, productcolor);
            _productColorService.UpdProductColor(productcolor);
            await _productColorService.CommitAsync();

            return(NoContent());
        }
Example #2
0
        public async Task <IActionResult> UpdateProductColor(int productid, int colorid, [FromBody] ProductColorForUpdDto productcolorForUpdDto)
        {
            if (!_productService.ExistsProduct(productid))
            {
                return(NotFound());
            }

            if (productid <= 0 || colorid <= 0)
            {
                return(NotFound());
            }

            //since we already know colorid, we will do upsert:
            //if productid and colorid not exists in product color, we can add it, if exists, we will do update
            if (!_productColorService.ExistsProductColor(productid, colorid))
            {
                var productcolorForCreate = _mapper.Map <ProductColor>(productcolorForUpdDto);
                productcolorForCreate.ProductId = productid;
                productcolorForCreate.ColorId   = colorid;
                await _productColorService.AddProductColorAsync(productcolorForCreate);

                _productColorService.Commit();
                var productcolorDto = _mapper.Map <ProductColorDto>(productcolorForCreate);
                return(CreatedAtRoute("GetProductcolor", new { productid, colorid }, productcolorDto));
            }

            var productColorEntity = await _productColorService.GetProductColorByProductAndColorIdAsync(productid, colorid);

            _mapper.Map(productcolorForUpdDto, productColorEntity);
            _productColorService.UpdProductColor(productColorEntity);
            await _productColorService.CommitAsync();

            return(NoContent());
        }