public async Task UpdateItem_Updates() { var originalItem = new Item { Id = 1, Name = "Original Name", Description = "Original Description", }; var item = new UpdateItemDTO { Id = 1, Name = "New Item Name", Description = "New Item Description", }; this.itemRepository.Setup(r => r.GetByIdAsync(It.IsAny <int>())) .Returns(Task.FromResult(originalItem)); await this.itemService.UpdateItemAsync(item, 2); itemRepository.Verify(r => r.UpdateAsync(It.Is <Item>(i => i.Id == item.Id && i.Name == item.Name && i.Description == item.Description), 2), Times.Once); }
public async Task UpdateItemAsync(UpdateItemDTO model) { var updatedBy = this.HttpContext.User.Claims.FirstOrDefault()?.Value; async Task <string> NewImage(string path, string image) { this.m_imageService.DeleteImage(path); return(await this.m_imageService.GetImagePathAsync(image)); } var item = await this.Repository.GetByIdAsync(model.Id); if (item == null) { throw new Exception("Item not found."); } item.Active = model.Active; item.Amount = model.Amount; item.Brand = model.Brand.ToLower(); item.Color = model.Color.ToLower(); item.Description = model.Description; item.Discount = model.Discount; item.PreviewImagePath = model.PreviewImagePath == null ? item.PreviewImagePath : await NewImage(item.PreviewImagePath, model.PreviewImagePath); item.MinPreviewImagePath = string.IsNullOrEmpty(model.PreviewImagePath) ? item.MinPreviewImagePath : await this.m_imageService.GetMinImagePathAsync(item.PreviewImagePath); item.ImagePath1 = string.IsNullOrEmpty(model.ImagePath1) ? item.ImagePath1 : await NewImage(item.ImagePath1, model.ImagePath1); item.ImagePath2 = string.IsNullOrEmpty(model.ImagePath2) ? item.ImagePath2 : await NewImage(item.ImagePath2, model.ImagePath2); item.ImagePath3 = string.IsNullOrEmpty(model.ImagePath3) ? item.ImagePath3 : await NewImage(item.ImagePath3, model.ImagePath3); item.CategoryId = model.Kind; item.Name = model.Name; item.Price = model.Price; item.Sex = model.Sex; item.Size = model.Size.ToLower(); item.Status = model.Status; item.SubCategoryId = model.Subkind; item.UpdatedBy = updatedBy; await this.Repository.UpdateAsync(item); if (item.Id <= 0) { throw new Exception("Updating error."); } }
public void Update(UpdateItemDTO model) { var item = _unitOfWork.Items.GetWhere(i => i.Id == model.Id).FirstOrDefault(); item.Description = model.Description; item.Title = model.Title; _unitOfWork.SaveChanges(); }
public async Task <IActionResult> Put([FromBody] UpdateItemDTO item) { try { await this.m_itemService.UpdateItemAsync(item); return(this.Ok()); } catch (Exception exception) { return(this.BadRequest(exception.Message)); } }
public ActionResult Put([FromBody] UpdateItemDTO model) { try { _itemsService.Update(model); return(Ok(true)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task UpdateItem_Updates() { var item = new UpdateItemDTO { Id = 123, Name = "Edited Item", Description = "This item was edited", }; await this.itemController.UpdateItem(item); this.itemService.Verify(s => s.UpdateItemAsync(item, It.IsAny <int>()), Times.Once); }
public async Task UpdateItem_NoName_Throws(string name) { var item = new UpdateItemDTO { Id = 123, Name = name, Description = null, }; Task noName() => this.itemService.UpdateItemAsync(item, 2); var ex = await Assert.ThrowsAsync <ArgumentNullException>(noName); Assert.Equal("Name", ex.ParamName); }
public ActionResult UpdateItem(Guid id, UpdateItemDTO updateItemDTO) { var existingItem = _repository.GetItem(id); if (existingItem is null) { return(NotFound()); } Item updatedItem = existingItem with { Name = updateItemDTO.Name, Price = updateItemDTO.Price }; _repository.UpdateItem(updatedItem); return(NoContent()); }
public async Task <IActionResult> Put(Guid id, UpdateItemDTO updateItemDTO) { var existingItem = await _itemsRepository.Get(id); if (existingItem == null) { return(NotFound()); } existingItem.Name = updateItemDTO.Name; existingItem.Description = updateItemDTO.Description; existingItem.Price = updateItemDTO.Price; await _itemsRepository.Update(existingItem); await _publishEndpoint.Publish(new CatalogItemUpdated(existingItem.Id, existingItem.Name, existingItem.Description)); return(NoContent()); }
public async Task EditItem_Saves() { this.CreateItems(1); var name = "New Name"; var description = "New Description"; var dto = new UpdateItemDTO() { Id = 1, Name = "New Name", Description = "New Description", }; var result = await this.itemController.UpdateItem(dto); Assert.IsType <OkResult>(result); var newItem = this.dataContext.Items.FirstOrDefault(); Assert.NotNull(newItem); Assert.Equal(1, newItem.Id); Assert.Equal(name, newItem.Name); Assert.Equal(description, newItem.Description); }