public ActionResult ProductUpdate(ProductToUpdate p)
 {
     if (ModelState.IsValid)
     {
         productRepository
         .UpdateProductProperty
             (p.columnName, p.productId, p.value, p.productMagName);
         return(Ok());
     }
     else
     {
         return(BadRequest("Field cannot be empty!"));
     }
 }
Ejemplo n.º 2
0
 public IActionResult UpdateProduct(string id, [FromBody] ProductToUpdate product)
 {
     return(NoContent());
 }
Ejemplo n.º 3
0
        public async Task <ReturnResponse> UpdateProduct(int productId, ProductToUpdate productToUpdate)
        {
            if (productToUpdate == null)
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.ObjectNull,
                    StatusMessage = Utils.StatusMessageObjectNull
                });
            }

            if (productId != productToUpdate.ProductId)
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.BadRequest,
                    StatusMessage = Utils.StatusMessageBadRequest
                });
            }

            var productDetails = await _dataContext.Product.Where(a => a.ProductId == productId).FirstOrDefaultAsync();

            if (productDetails == null)
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.NotFound,
                    StatusMessage = Utils.StatusMessageNotFound
                });
            }

            var category = await _globalRepository.Get <Category>(productToUpdate.CategoryId);

            if (category == null)
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.NotFound,
                    StatusMessage = Utils.StatusMessageNotFound
                });
            }

            var productToStore = _mapper.Map(productToUpdate, productDetails);

            //FINALLY UPLOAD THE IMAGE THAT CAME WITH THE PRODUCT IF ANY
            var cloudinaryResult = _cloudinaryRepository.UploadFilesToCloudinary(productToUpdate.AttachmentFile);

            if (cloudinaryResult.StatusCode != Utils.Success)
            {
                if (cloudinaryResult.StatusCode != Utils.ObjectNull)
                {
                    return(new ReturnResponse()
                    {
                        StatusCode = Utils.CloudinaryFileUploadError,
                        StatusMessage = Utils.StatusMessageCloudinaryFileUploadError
                    });
                }
            }
            else
            {
                var cloudinaryUploadResult = (RawUploadResult)cloudinaryResult.ObjectValue;

                //DELETE THE OLD ATTACHED FILE
                var cloudinaryDeleteResult = _cloudinaryRepository.DeleteFilesFromCloudinary(new List <string>()
                {
                    productToStore.AttachmentPublicId
                });

                if (cloudinaryDeleteResult.StatusCode != Utils.Success)
                {
                    //IF THE OLD ATTACHED FILE IN THE CLOUDINARY DIDNT DELETE SUCCESSFULLY THEN REMOVE THE NEWLY ADDED FILE
                    var cloudinaryDelResult = _cloudinaryRepository.DeleteFilesFromCloudinary(new List <string>()
                    {
                        cloudinaryUploadResult.PublicId
                    });

                    if (cloudinaryDelResult.StatusCode != Utils.Success)
                    {
                        return(new ReturnResponse()
                        {
                            StatusCode = Utils.CloudinaryFileDeleteError,
                            StatusMessage = Utils.StatusMessageCloudinaryFileDeleteError
                        });
                    }
                    else
                    {
                        return(new ReturnResponse()
                        {
                            StatusCode = Utils.NotSucceeded,
                            StatusMessage = Utils.StatusMessageNotSucceeded
                        });
                    }
                }
                else
                {
                    productToStore.AttachmentPublicId = cloudinaryUploadResult.PublicId;
                    productToStore.AttachmentLink     = cloudinaryUploadResult.SecureUrl.ToString().Split(cloudinaryUploadResult.PublicId)[0] + cloudinaryUploadResult.PublicId + Path.GetExtension(productToUpdate.AttachmentFile.FileName);
                    productToStore.AttachmentFileName = productToUpdate.AttachmentFile.FileName;
                }
            }

            var updateResult = _globalRepository.Update(productToStore);

            if (!updateResult)
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.NotSucceeded,
                    StatusMessage = Utils.StatusMessageNotSucceeded
                });
            }

            var saveResult = await _globalRepository.SaveAll();

            if (!saveResult.HasValue)
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.SaveError,
                    StatusMessage = Utils.StatusMessageSaveError
                });
            }

            if (!saveResult.Value)
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.SaveNoRowAffected,
                    StatusMessage = Utils.StatusMessageSaveNoRowAffected
                });
            }

            return(new ReturnResponse()
            {
                StatusCode = Utils.Success,
                StatusMessage = Utils.StatusMessageSuccess,
                ObjectValue = productToStore
            });
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <ReturnResponse> > PutProduct([FromRoute] int productId, [FromForm] ProductToUpdate productToUpdate)
        {
            var dbTransaction = await _dataContext.Database.BeginTransactionAsync();

            var result = await _productRepository.UpdateProduct(productId, productToUpdate);

            if (result.StatusCode == Utils.Success)
            {
                result.ObjectValue = _mapper.Map <ProductResponse>((Product)result.ObjectValue);
                await dbTransaction.CommitAsync();

                return(StatusCode(StatusCodes.Status200OK, result));
            }
            else
            {
                return(StatusCode(StatusCodes.Status400BadRequest, result));
            }
        }