public async Task <int> SaveAsync(BL.Product entity) { try { if (entity == null) { return(0); } using (var context = _contextFactory.GetProductContext()) { var entityModel = await context .Products .FirstOrDefaultAsync(item => item.Id.Equals(entity.Id)); if (entityModel == null) { entityModel = new DA.Product(); MapForUpdateEntity(entity, entityModel); await context.Products.AddAsync(entityModel); } else { MapForUpdateEntity(entity, entityModel); } context.SaveChanges(); return(entityModel.Id); } } catch (Exception ex) { throw ex; } }
private void MapForUpdateEntity(BL.Product entity, DA.Product daEntity) { daEntity.Id = entity.Id; daEntity.Name = entity.Name; daEntity.Company = entity.Company; daEntity.Price = entity.Price; }
// GET: Products public ActionResult Add() { DataAccess.Models.Product product = new DataAccess.Models.Product(); using (Context db = new Context()) { ViewData["Brands"] = db.Brands.ToDictionary(i => i.id, i => i.name) .Select(x => new SelectListItem { Text = x.Value, Value = x.Key.ToString() }) .ToList(); } return View(product); }
public ActionResult Edit(int id) { DataAccess.Models.Product product = new DataAccess.Models.Product(); using (Context db = new Context()) { ViewData["Brands"] = db.Brands.ToDictionary(i => i.id, i => i.name) .Select(x => new SelectListItem { Text = x.Value, Value = x.Key.ToString() }) .ToList(); product = db.Products.Find(id); //Set Size long sizevalue = Convert.ToInt64(product.sizes); long[] sizearray = Utility.BitwiseAnd.getFectors(sizevalue); foreach (var s in sizearray) { DataAccess.Models.enumSize enumsize = (DataAccess.Models.enumSize)s; product.SizeList.ForEach(x => { if (x.Size.Equals(enumsize)) { x.IsSelected = true; } }); } //set color long colorvalue = Convert.ToInt64(product.colors); long[] colorsarray = Utility.BitwiseAnd.getFectors(colorvalue); foreach (var c in colorsarray) { DataAccess.Models.enumColor enumcolor = (DataAccess.Models.enumColor)c; product.ColorsList.ForEach(x => { if (x.Color.Equals(enumcolor)) { x.IsSelected = true; } }); } } return View(product); }