public async Task <ItemEditViewModel> EditByIdAsync(int id, ItemEditInputModel itemFromView) { var itemToDelete = this.itemRepository.All().FirstOrDefault(x => x.Id == id); if (itemToDelete == null) { Exception innerException = new Exception(nameof(id)); throw new NullReferenceException(string.Format(ItemConstants.NullReferenceItemId, id), innerException); } var checkForName = this.itemRepository.All().FirstOrDefault(x => x.Name == itemFromView.Name); if (checkForName != null && checkForName.Id != id) { throw new ArgumentException(string.Format(ItemConstants.ArgumentExceptionItemName, itemFromView.Name), nameof(itemFromView.Name)); } var newItem = itemFromView.To <Item>(); this.itemRepository.Delete(itemToDelete); await this.itemRepository.AddAsync(newItem); await this.itemRepository.SaveChangesAsync(); return(newItem.To <ItemEditViewModel>()); }
public async Task EditByIdAsyncReturnsItem() { var addItem = new ItemCreateInputModel { Name = SecondItemName, OrdinaryPrice = SecondtemOrdinaryPrice, ExpressAddOnPrice = SecondItemExpressAddOnPrice, FlavorAddOnPrice = SecondItemFlavorAddOnPrice, VacuumCleaningAddOnPrice = SecondItemVacuumCleaningAddOnPrice, }; await this.ItemsServiceMock.CreateAsync(addItem); var item = new ItemEditInputModel { Name = FirstItemName, OrdinaryPrice = FistItemOrdinaryPrice, ExpressAddOnPrice = SecondItemExpressAddOnPrice, FlavorAddOnPrice = SecondItemFlavorAddOnPrice, VacuumCleaningAddOnPrice = SecondItemVacuumCleaningAddOnPrice, }; var result = await this.ItemsServiceMock.EditByIdAsync(1, item); Assert.Equal(item.OrdinaryPrice, result.OrdinaryPrice); }
public async Task <IActionResult> Edit(ItemEditInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } var dto = new UpdateItemDto() { Name = input.Name, Type = input.Type, Shape = input.Shape, Weight = input.Weight, Purity = input.Purity, ItemId = input.ItemId, Images = input.Files, Quantity = input.Quantity, Dimensions = input.Dimensions, Description = input.Description, ManufacturerId = input.ManufacturerId, }; await this.itemsService.UpdateAsync(dto, await this.usersService.GetIdAsync(input.Username), Path.Combine(this.hostingEnvironment.WebRootPath, "images", "items")); return(this.RedirectToAction("Details", new { itemId = input.ItemId })); }
public async Task <IActionResult> Edit(ItemEditInputModel input) { if (!this.ModelState.IsValid) { var tabs = this.tabService.GetAll <TabIdNameViewModel>(); input.Tabs = tabs; return(this.View(input)); } await this.itemService.UpdateAsync(input.Id, input.Name, input.Price, input.TabId, input.Description); return(this.RedirectToAction("Id", "Items", new { area = string.Empty, input.Id })); }
public async Task <ItemEditViewModel> EditByIdAsync(int id, ItemEditInputModel itemFromView) { var itemToDelete = this.itemRepository.All().FirstOrDefault(x => x.Id == id); if (itemToDelete == null) { throw new NullReferenceException(string.Format(ItemConstants.NullReferenceItemId, id)); } var newItem = itemFromView.To <Item>(); this.itemRepository.Delete(itemToDelete); await this.itemRepository.AddAsync(newItem); await this.itemRepository.SaveChangesAsync(); return(newItem.To <ItemEditViewModel>()); }
public async Task EditByIdAsyncWithWrongIdReturnsError() { await this.AddTestingFirstItemToDb(); await this.AddTestingSecondItemToDb(); var item = new ItemEditInputModel { Name = FirstItemName, OrdinaryPrice = SecondtemOrdinaryPrice, ExpressAddOnPrice = SecondItemExpressAddOnPrice, FlavorAddOnPrice = SecondItemFlavorAddOnPrice, VacuumCleaningAddOnPrice = SecondItemVacuumCleaningAddOnPrice, }; var wrongId = 122; var exception = await Assert.ThrowsAsync <NullReferenceException>(() => this.ItemsServiceMock.EditByIdAsync(wrongId, item)); Assert.Equal(string.Format(string.Format(ItemConstants.NullReferenceItemId, wrongId)), exception.Message); }
public async Task <IActionResult> Edit(int id, ItemEditInputModel itemEdit) { if (!this.ModelState.IsValid) { return(this.View(AutoMapper.Mapper.Map <ItemEditViewModel>(itemEdit))); } var isNameExist = await this.itemsService.GetAllItemsAsync <ItemEditViewModel>().FirstOrDefaultAsync(x => x.Name == itemEdit.Name); if (isNameExist != null && isNameExist.Id != id) { this.ModelState.AddModelError(nameof(itemEdit.Name), string.Format(ItemConstants.ArgumentExceptionItemName, itemEdit.Name)); return(this.View(AutoMapper.Mapper.Map <ItemIndexViewModel>(itemEdit))); } var result = await this.itemsService.EditByIdAsync(id, itemEdit); return(this.RedirectToAction(nameof(this.Index))); }
public async Task <IActionResult> Edit(int id, ItemEditInputModel itemEdit) { try { if (!this.ModelState.IsValid) { return(this.View(itemEdit)); } var result = await this.itemsService.EditByIdAsync(id, itemEdit); return(this.RedirectToAction(nameof(this.Index))); } catch (ArgumentException e) { // TODO: Error message that item name exist this.ModelState.AddModelError(e.ParamName, e.Message); return(this.View()); } }
public IActionResult Edit(int itemId) { var item = this.itemsService.GetItemById <ItemEditInputModel>(itemId); var viewModel = new ItemEditInputModel { ItemId = item.ItemId, Name = item.Name, Type = item.Type, Shape = item.Shape, Weight = item.Weight, Purity = item.Purity, Fineness = item.Fineness, Quantity = item.Quantity, Dimensions = item.Dimensions, Description = item.Description, ManufacturerId = item.ManufacturerId, Images = item.Images, }; return(this.View(viewModel)); }