public async Task <IActionResult> Update(int id, [FromBody] ProductToUpdateDto productToUpdate) { var existingProduct = _repositoryManager.Product .FindByCondition(x => x.Id == id).FirstOrDefault(); if (existingProduct == null) { return(BadRequest($"Product with id {id} not found")); } var restaurant = _repositoryManager.Restaurant .FindByCondition(x => x.Id == productToUpdate.RestaurantId) .FirstOrDefault(); if (restaurant == null) { return(BadRequest($"Product.Restaurant with id {productToUpdate.RestaurantId} not found")); } existingProduct.Restaurant = restaurant; existingProduct.Title = productToUpdate.Title; existingProduct.Price = productToUpdate.Price; existingProduct.Image = productToUpdate.Image; _repositoryManager.Product.Update(existingProduct); await _repositoryManager.SaveAsync(); return(NoContent()); }
public async Task <ActionResult> UpdateProduct([FromForm] ProductToUpdateDto productUpdateData) { var product = await unitOfWork.Repository <Product>().GetByIdAsync(productUpdateData.Id); if (product == null) { return(Ok()); } product.Name = productUpdateData.Name ?? product.Name; product.Description = productUpdateData.Description ?? product.Description; product.Price = productUpdateData.Price ?? product.Price; product.ProductGenderBase = await productGenderRepo.GetEntityWithSpec(new GetGenderByName(productUpdateData.ProductGenderBase)); ProductType type = await productTypesRepo.GetEntityWithSpec(new ProductTypeByProductTypeName(productUpdateData.ProductType)); if (type == null) { } else { product.ProductTypeId = type.Id; } string path = Path.Combine(env.WebRootPath, product.PictureUrl); if (productUpdateData.ProductImage != null) { using (var stream = new FileStream(path, FileMode.Create)) { await productUpdateData.ProductImage.CopyToAsync(stream); } } var user = await userManager.FindByEmailFromClaimsPrinciple(HttpContext.User); AdminActionHistory reg = new AdminActionHistory(); reg.AdminEmail = user.Email; reg.Date = DateTime.Now; reg.Operation = AdminActionOperations.Update; reg.AdminAction = "Updated product: " + product.Name; reg.UserId = user.Id; reg.Product = product; adminActionHistoryRepo.Add(reg); unitOfWork.Repository <Product>().Update(product); await unitOfWork.Complete(); return(Ok()); }
public async Task <IActionResult> Put(long id, ProductToUpdateDto productToUpdate) { Product productFromDb = await this._productsRepository.SelectAsync(id); if (productFromDb == null) { var product = this._mapper.Map <Product>(productToUpdate); long newId = await this._productsRepository.InsertAsync(product); return(Created(newId.ToString(), product)); } UpdateProduct(productToUpdate, productFromDb); await this._productsRepository.UpdateAsync(productFromDb.Id, productFromDb); return(Ok(productFromDb)); }
private void UpdateProduct(ProductToUpdateDto src, Product dest) { if (src.Name != null) { dest.Name = src.Name; } if (src.Price != null) { dest.Price = src.Price.Value; } if (src.Available != null) { dest.Available = src.Available.Value; } if (src.Description != null) { dest.Description = src.Description; } }
public async Task <IActionResult> CreateProduct([FromBody] ProductToUpdateDto productToAdd) { if (productToAdd is null || string.IsNullOrEmpty(productToAdd.Title)) { return(BadRequest("Invalid product")); } var product = new Product() { Image = productToAdd.Image, Price = productToAdd.Price, Title = productToAdd.Title, Restaurant = new Restaurant { Id = productToAdd.RestaurantId } }; _repositoryManager.Product.CreateProduct(product); await _repositoryManager.SaveAsync(); return(Ok()); }
public async Task <ActionResult <ProductDto> > UpdateProduct(string productId, ProductToUpdateDto updateDto) { var product = await _repository.GetByIdAsync(productId); if (product == null) { return(NotFound(new ApiResponse(404))); } _mapper.Map(updateDto, product); _repository.Update(product); if (await _unitOfWork.Complete() <= 0) { return(BadRequest(new ApiResponse(400, $"An error occured while trying to update"))); } return(CreatedAtRoute("GetProduct", new { productId = product.Id }, _mapper.Map <ProductDto>(product))); }
protected override void OnInitialized() { //test var p7 = new ProductDto { Id = new Guid("f749e0cd-1917-401e-a8da-27e2d88da463"), Name = "Remera Kill em' All", Description = "This is a wider card with supporting text below as a natural lead-in to additional content.", ImageSource = @"img\merch\metallica.png", Price = 500 }; var p8 = new ProductDto { Id = new Guid("52c007e5-4b58-4e61-91fc-b8b95a430bb9"), Name = "Remera Ride The Lighting", Description = "This is a wider card with supporting text below as a natural lead-in to additional content.", ImageSource = @"img\merch\RideTheLighting.png", Price = 600 }; var p9 = new ProductDto { Id = new Guid("59cc79df-7ea9-4a4a-ab2b-5e598014152b"), Name = "Remera Vintage", Description = "This is a wider card with supporting text below as a natural lead-in to additional content.", ImageSource = @"img\merch\Vintage.png", Price = 750, ProductType = "merchandising" }; var p10 = new ProductDto { Id = new Guid("f749e0cd-1917-401e-a8da-27e2d88da463"), Name = "Taza Personalizada", Description = "This is a wider card with supporting text below as a natural lead-in to additional content.", ImageSource = @"img\merch\Mug.png", Price = 500 }; ProductList.Add(p7); ProductList.Add(p8); ProductList.Add(p9); ProductList.Add(p10); var prod = ProductList.FirstOrDefault(x => x.Id == Id); if (prod != null) { Model = new ProductToUpdateDto { Id = Id, Price = prod.Price, Name = prod.Name, ImageSource = prod.ImageSource, Description = prod.Description, }; } //fin test switch (prod.ProductType) { case _digitalDesigns: Directory = new DirectoryInfo(@$ "{Environment.CurrentDirectory}\wwwroot\img\digitalDesigns"); break; case _paintings: Directory = new DirectoryInfo(@$ "{Environment.CurrentDirectory}\wwwroot\img\paintings"); break; case _merchandising: Directory = new DirectoryInfo(@$ "{Environment.CurrentDirectory}\wwwroot\img\merch"); break; } }