public IActionResult Save(DTOBrand model) { if (!IsSet(model.Name)) { ModelState.AddModelError(nameof(model.Name), _localizer.ErrNameIsRequired); } if (!ModelState.IsValid) { model.CategoryBrandsListItems = _unitOfWork.Brands.GetAll().Select(x => new SelectListItem(x.Name, x.Id.ToString())).ToList(); return(PartialView("AddEdit", model)); } if (model.Id == 0) { _unitOfWork.Brands.Add(model); } else { _unitOfWork.Brands.Edit(model); } return(Json(new { success = true })); }
public IActionResult Add() { DTOBrand model = new DTOBrand(); model.CategoryBrandsListItems = _unitOfWork.Categories.GetAll().Select(x => new SelectListItem(x.Name, x.Id.ToString())).ToList(); return(PartialView("AddEdit", model)); }
public DTOBrand ToDTO(Brand brand) { DTOBrand dTOBrand = new DTOBrand(); dTOBrand.Name = brand.Name; dTOBrand.ID = brand.ID; return(dTOBrand); }
public IActionResult Edit(int?id) { if (id == null) { return(RedirectToAction(nameof(Index))); } DTOBrand model = _unitOfWork.Brands.GetByIdDto(id.GetValueOrDefault()); return(PartialView("AddEdit", model)); }
public void Edit(DTOBrand dtoBrand) { Brand brand = _mapper.Map <Brand>(dtoBrand); _context.Update(brand); List <int> itemsToAdd = dtoBrand.SelectedCategoryBrandsIds; List <int> itemsToDelete = new List <int>(); List <CategoryBrand> categoryBrands = _context.CategoriesBrands.Where(x => x.BrandId == brand.Id).ToList(); foreach (var item in categoryBrands) { bool exist = itemsToAdd.Any(x => x == item.CategoryId); if (!exist) { itemsToDelete.Add(item.Id); } else { itemsToAdd.Remove(item.CategoryId); } } if (itemsToDelete != null && itemsToDelete.Any()) { foreach (var item in itemsToDelete) { _unitOfWork.CategoriesBrands.Delete(item); //TODO : Ubaciti provjeru da li je povezan sa nekim postojecim PROIZVODOM } } if (itemsToAdd != null && itemsToAdd.Any()) { CategoryBrand categoryBrand = null; foreach (var item in itemsToAdd) { categoryBrand = new CategoryBrand { BrandId = brand.Id, CategoryId = item }; _context.CategoriesBrands.Add(categoryBrand); } } _context.SaveChanges(); }
public void Add(DTOBrand dtoBrand) { Brand brand = _mapper.Map <Brand>(dtoBrand); _context.Add(brand); _context.SaveChanges(); if (dtoBrand.SelectedCategoryBrandsIds != null && dtoBrand.SelectedCategoryBrandsIds.Any()) { CategoryBrand categoryBrand = null; foreach (var cb in dtoBrand.SelectedCategoryBrandsIds) { categoryBrand = new CategoryBrand { BrandId = brand.Id, CategoryId = cb, }; _context.CategoriesBrands.Add(categoryBrand); } } _context.SaveChanges(); }