public bool DeleteHistory(int id) { try { var history = _context.Histories.Find(id); _context.Histories.Remove(history); _context.SaveChanges(); } catch (Exception) { return(false); } return(true); }
public string Register(User entity) { db.Users.Add(entity); UserRole userRole = new UserRole() { userid = entity.id, roleid = 1, createdAt = DateTime.Now }; db.UserRoles.Add(userRole); db.SaveChanges(); return(entity.username); }
public int AddToCart(int catalogid, string username) { var query = from u in _context.Users join b in _context.Baskets on u.id equals b.userid where u.username == username && b.userid == u.id && b.catalogid == catalogid select new { b }; var models = query.OrderBy(x => x.b.createdAt).Select(x => new BasketDTO() { userid = x.b.userid, catalogid = x.b.catalogid, unit = x.b.unit, createdAt = x.b.createdAt }).ToList(); if (models.Count > 0) { var userid = models[0].userid; var basket = _context.Baskets.SingleOrDefault(x => x.userid == userid && x.catalogid == catalogid); basket = BasketMapper.toBasket(models[0], basket); try { _context.SaveChanges(); } catch (Exception) { return(0); } } else { var basket = new Basket(); var userid = _context.Users.SingleOrDefault(x => x.username == username).id; basket = BasketMapper.toBasket(new BasketDTO() { catalogid = catalogid, userid = userid }, basket); _context.Baskets.Add(basket); try { _context.SaveChanges(); } catch (Exception) { return(0); } } return(1); }
public Dictionary <int, List <string> > AddBill(List <CartItem> carts, BillDTO model, int userid) { Dictionary <int, List <string> > hash = new Dictionary <int, List <string> >(); // list sản phẩm k đủ số lượng List <string> lst = new List <string>(); using (var dbContextTransaction = db.Database.BeginTransaction()) { try { foreach (var item in carts) { var bill = new Bill { unit = item.quantity, name = item.catalog.name, price = item.catalog.price * item.quantity, pictureuri = item.catalog.pictureuri, catalogid = item.catalog.id, status = 0, userid = userid, shiptoaddress = model.shiptoaddress, notebuy = model.notebuy, createdAt = DateTime.Now, updatedAt = DateTime.Now, phone = model.phone, username = model.username }; db.Bills.Add(bill); var catalog = db.Catalogs.Find(item.catalog.id); if (catalog.quantity >= item.quantity) { var msgBill = db.MessageBills.Find(1); msgBill.updateChoDuyetAt = DateTime.Now; catalog.quantity -= item.quantity; } else { lst.Add(catalog.name); } } if (lst.Count > 0) { hash.Add(0, lst); return(hash); } db.SaveChanges(); hash.Add(1, null); dbContextTransaction.Commit(); return(hash); } catch (Exception) { dbContextTransaction.Rollback(); hash.Add(-1, null); return(hash); } } }
public bool Insert(UserDTO model) { var result = _context.Users.Count(x => x.username == model.username); if (result > 0) { return(false); } else { try { var user = new User(); user = UserMapper.toUser(model, user); _context.Users.Add(user); _context.SaveChanges(); AddRole(model.username); return(true); } catch (Exception) { return(false); } } }
public int[] GetMessage() { int countChoDuyet = db.Bills.Count(x => x.status == 0); // cho duyet int countDaHuy = db.Bills.Count(x => x.status == -1); // da huy int[] arr = new int[2]; if (db.MessageBills.Count() == 0) { var model = new MessageBill() { countChoDuyet = countChoDuyet, countDaHuy = countDaHuy }; db.MessageBills.Add(model); db.SaveChanges(); arr[0] = -2; arr[1] = -2; return(arr); } int countCD = db.MessageBills.Find(1).countChoDuyet; int countDH = db.MessageBills.Find(1).countDaHuy; if (countChoDuyet > countCD) { arr[0] = 0; } else { arr[0] = -2; } if (countDaHuy > countDH) { arr[1] = -1; } else { arr[1] = -2; } return(arr); }
public int SaveBrand(BrandDTO model) { if (model.id == 0) { var data = _context.CatalogBrands.Count(x => x.brand == model.brand); if (data > 0) { return(-1); } var brand = new CatalogBrand(); brand.brand = model.brand; brand.pictureurl = model.pictureurl; _context.CatalogBrands.Add(brand); try { _context.SaveChanges(); } catch (Exception) { return(0); } } else { var brand = _context.CatalogBrands.Find(model.id); brand.brand = model.brand; brand.pictureurl = model.pictureurl; try { _context.SaveChanges(); } catch (Exception) { return(0); } } return(1); }
public bool SaveMobile(int id, MobileDTO catalogDTO) { DbContextTransaction transaction = _context.Database.BeginTransaction(); var check = SaveCatalog(id, transaction, catalogDTO); if (!check) { return(false); } if (id == 0) { var mobile = new SpecificationsMobile(); mobile = CatalogMapper.toSpecificationsMobile(catalogDTO, mobile); try { _context.SpecificationsMobiles.Add(mobile); _context.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); return(false); } } else { var mobile = _context.SpecificationsMobiles.SingleOrDefault(x => x.catalogid == catalogDTO.id); if (mobile == null) { transaction.Rollback(); return(false); } mobile = CatalogMapper.toSpecificationsMobile(catalogDTO, mobile); try { _context.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); return(false); } } return(true); }
public void Update(Catalog entity) { try { var catalog = db.Catalogs.Find(entity.id); CatalogMapper.Mapper(catalog, entity); db.SaveChanges(); } catch (Exception) { throw new Exception("Lỗi cập nhật sản phẩm"); } }