public ActionResult AddBrand(BrandEditViewModel bevm, HttpPostedFileBase file1) { unitOfWork = new EFUnitOfWork(db); var entity = Mapper.Map <BrandModel, Brand>(bevm.BrandModel); if (file1 != null) { entity.BrandImgUrl = "~/Images/BrandImages/" + bevm.BrandModel.Name + ".png";; string path = Path.Combine(Server.MapPath(entity.BrandImgUrl)); file1.SaveAs(path); } entity.TypeBrands = bevm.typeIds.Select(t => new TypeBrand() { TypeId = t, BrandId = entity.Id }).ToList(); unitOfWork.GetRepository <Brand>().Add(entity); unitOfWork.SaveChanges(); unitOfWork.Dispose(); return(RedirectToAction("GetBrandList")); }
public PartialViewResult EditBrand(int id) { unitOfWork = new EFUnitOfWork(db); BrandEditViewModel bevm = new BrandEditViewModel(); var model = unitOfWork.GetRepository <Brand>().GetById(id); BrandModel bm = Mapper.Map <Brand, BrandModel>(model); var types = unitOfWork.GetRepository <Type>().GetAll().Select(t => new SelectListItem() { Value = t.Id.ToString(), Text = t.Name }).ToList(); bevm.typeIds = model.TypeBrands.Select(tb => tb.TypeId).ToList(); bevm.BrandModel = bm; bevm.Types = new MultiSelectList(types, "Value", "Text", bevm.typeIds); unitOfWork.Dispose(); return(PartialView("_BrandEditPartialView", bevm)); }
public async Task <IActionResult> Edit(BrandEditViewModel brandEditViewModel) { if (!this.ModelState.IsValid) { return(this.View(brandEditViewModel)); } await this.brandsService.EditAsync(brandEditViewModel); return(this.RedirectToAction("GetAll", "Brands", new { area = "Administration" })); }
public async Task CheckIfEditingBrandReturnsNullReferenceException() { this.SeedDatabase(); var brandEditViewModel = new BrandEditViewModel { Id = 3, Name = "Sony", }; var exception = await Assert .ThrowsAsync <NullReferenceException>(async() => await this.brandsService.EditAsync(brandEditViewModel)); Assert.Equal(string.Format(ExceptionMessages.BrandNotFound, brandEditViewModel.Id), exception.Message); }
public async Task CheckIfEditingBrandWorksCorrectly() { this.SeedDatabase(); var brandEditViewModel = new BrandEditViewModel { Id = this.firstBrand.Id, Name = "Sony", }; await this.brandsService.EditAsync(brandEditViewModel); Assert.Equal(brandEditViewModel.Id, this.firstBrand.Id); Assert.Equal(brandEditViewModel.Name, this.firstBrand.Name); }
public ActionResult EditBrandConfirm(BrandEditViewModel bevm, HttpPostedFileBase file1) { unitOfWork = new EFUnitOfWork(db); var entity = unitOfWork.GetRepository <Brand>().GetById(bevm.BrandModel.Id); entity.Name = bevm.BrandModel.Name; if (file1 != null) { string fullPath = Request.MapPath(entity.BrandImgUrl); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); } entity.BrandImgUrl = "~/Images/BrandImages/" + bevm.BrandModel.Name + ".png";; string path = Path.Combine(Server.MapPath(entity.BrandImgUrl)); file1.SaveAs(path); } var typeBrands = unitOfWork.GetRepository <TypeBrand>().GetAll() .Where(tb => tb.BrandId == bevm.BrandModel.Id).ToList(); foreach (var typeBrand in typeBrands) { unitOfWork.GetRepository <TypeBrand>().Delete(typeBrand); } entity.TypeBrands = bevm.typeIds.Select(t => new TypeBrand() { TypeId = t, BrandId = entity.Id }).ToList(); unitOfWork.GetRepository <Brand>().Update(entity); unitOfWork.SaveChanges(); unitOfWork.Dispose(); return(RedirectToAction("GetBrandList")); }
public async Task EditAsync(BrandEditViewModel brandEditViewModel) { var brand = await this.brandsRepository .All() .FirstOrDefaultAsync(b => b.Id == brandEditViewModel.Id); if (brand == null) { throw new NullReferenceException( string.Format(ExceptionMessages.BrandNotFound, brandEditViewModel.Id)); } brand.Name = brandEditViewModel.Name; brand.ModifiedOn = DateTime.UtcNow; this.brandsRepository.Update(brand); await this.brandsRepository.SaveChangesAsync(); }
public PartialViewResult GetAddBrand() { unitOfWork = new EFUnitOfWork(db); BrandEditViewModel bevm = new BrandEditViewModel(); bevm.BrandModel = new BrandModel(); var types = unitOfWork.GetRepository <Type>().GetAll().Select(t => new SelectListItem() { Value = t.Id.ToString(), Text = t.Name }).ToList(); bevm.typeIds = new List <int>(); bevm.Types = new MultiSelectList(types, "Value", "Text", bevm.typeIds); unitOfWork.Dispose(); return(PartialView("_BrandAddPartialView", bevm)); }