Пример #1
0
 public Box GetById(int id)
 {
     using (var context = new Models.BoxesDBContext())
     {
         return(context.Boxes.FirstOrDefault(b => b.Id.Equals(id)));
     }
 }
Пример #2
0
 public IEnumerable <Box> Get()
 {
     using (var context = new Models.BoxesDBContext())
     {
         return(context.Boxes.OrderBy(b => b.TimeLastPurchase).ToList());
     }
 }
Пример #3
0
 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();
     }
 }
Пример #4
0
 public Box Create(Box entityToCreate)
 {
     using (var context = new Models.BoxesDBContext())
     {
         context.Boxes.Add(entityToCreate);
         context.SaveChanges();
     }
     return(entityToCreate);
 }
Пример #5
0
 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);
     }
 }
Пример #6
0
 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));
 }