public Box GetById(int id) { using (var context = new Models.BoxesDBContext()) { return(context.Boxes.FirstOrDefault(b => b.Id.Equals(id))); } }
public IEnumerable <Box> Get() { using (var context = new Models.BoxesDBContext()) { return(context.Boxes.OrderBy(b => b.TimeLastPurchase).ToList()); } }
public void Delete(int id) { using (var context = new Models.BoxesDBContext()) { var boxToDelete = context.Boxes.FirstOrDefault(b => b.Id.Equals(id)); context.Boxes.Remove(boxToDelete); context.SaveChanges(); } }
public Box Create(Box entityToCreate) { using (var context = new Models.BoxesDBContext()) { context.Boxes.Add(entityToCreate); context.SaveChanges(); } return(entityToCreate); }
public Box Update(Box entityToUpdate) { using (var context = new Models.BoxesDBContext()) { var box = context.Boxes.FirstOrDefault(b => b.Id.Equals(entityToUpdate.Id)); if (box is null) { throw new ArgumentException("No such box in db"); } box.X = entityToUpdate.X; box.Y = entityToUpdate.Y; box.Count = entityToUpdate.Count; box.TimeLastPurchase = entityToUpdate.TimeLastPurchase; context.SaveChanges(); return(box); } }
public void InitDB() { //make sure the db is created and if it needs to create it it will load initial data on the db using (var context = new Models.BoxesDBContext()) { //Creat the entire db if not exist bool isNew = context.Database.CreateIfNotExists(); if (isNew == false) { return; } //if db was created //inti for safty context.Database.Initialize(true); } //Load initial data _initialData.ForEach(b => Create(b)); }