示例#1
0
        public IActionResult Create(CoffeeItem item)
        {
            _context.CoffeeItems.Add(item);
            _context.SaveChanges();

            return(CreatedAtRoute("GetCoffee", new { id = item.Id }, item));
        }
 public IActionResult AddEditBook(long?id, BookViewModel model)
 {
     try
     {
         if (ModelState.IsValid)
         {
             bool isNew = !id.HasValue;
             Book book  = isNew ? new Book
             {
                 AddedDate = DateTime.UtcNow
             } : context.Set <Book>().SingleOrDefault(s => s.Id == id.Value);
             book.Name      = model.Name;
             book.ISBN      = model.ISBN;
             book.Author    = model.Author;
             book.Publisher = model.Publisher;
             if (isNew)
             {
                 context.Add(book);
             }
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(RedirectToAction("Index"));
 }
        public IHttpActionResult PutProduct(long id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != product.Id)
            {
                return(BadRequest());
            }

            db.Entry(product).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
示例#4
0
        public ActionResult Create([Bind(Include = "ID,FirstName,LastName,EmploymentDate")] MasterRoaster masterRoaster)
        {
            if (ModelState.IsValid)
            {
                db.MasterRoasters.Add(masterRoaster);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(masterRoaster));
        }
        public OrderDetail Create(OrderDetail model)
        {
            var newOd = new OrderDetail()
            {
                OrderDetailId = model.OrderDetailId,
                OrderId       = model.OrderId
            };

            context.OrderDetails.Add(newOd);
            context.SaveChanges();
            return(model);
        }
示例#6
0
        public ProductOrderDetail Create(ProductOrderDetail model)
        {
            var newPod = new ProductOrderDetail()
            {
                ProductId            = model.ProductId,
                OrderDetailId        = model.OrderDetailId,
                Count                = model.Count,
                ProductOrderDetailId = model.ProductOrderDetailId
            };

            context.ProductOrderDetails.Add(newPod);
            context.SaveChanges();
            return(model);
        }
示例#7
0
        public Product Create(Product model)
        {
            var newPt = new Product()
            {
                ProductId     = model.ProductId,
                Name          = model.Name,
                AvatarPath    = model.AvatarPath,
                Price         = model.Price,
                ProductTypeId = model.ProductTypeId
            };

            context.Products.Add(newPt);
            context.SaveChanges();
            return(model);
        }
示例#8
0
        public override void Up()
        {
            //Add in no sugar option to list
            using (var db = new CoffeeContext())
            {
                // Get all the users
                foreach (var user in db.Users)
                {
                    // Transform JSON
                    List <KeyValuePair <string, KeyValuePair <string, string> > > options = JsonConvert.DeserializeObject <List <KeyValuePair <string, KeyValuePair <string, string> > > >(user.CoffeeOptionsJson);

                    // Add in No Sugar option
                    if (!options.Any(o => o.Key == "No Sugar"))
                    {
                        options.Add(new KeyValuePair <string, KeyValuePair <string, string> >(
                                        key: "No Sugar",
                                        value: new KeyValuePair <string, string>(
                                            key: "bool",
                                            value: "false"
                                            )
                                        ));
                    }

                    user.CoffeeOptionsJson = JsonConvert.SerializeObject(options);
                }

                db.SaveChanges();
            }
        }
示例#9
0
        public ActionResult Get(int id)
        {
            UserItem          user        = _context.UserItems.Find(id);
            List <CoffeeItem> coffeeItems = new List <CoffeeItem>();

            foreach (CoffeeItem item in _context.CoffeeItems)
            {
                if (item.UserId == user.UserId)
                {
                    coffeeItems.Add(item);
                }
            }
            user.CoffeeItems = coffeeItems.ToList();
            _context.SaveChanges();
            return(new JsonResult(user));
        }
示例#10
0
 public void addProduct(Product product)
 {
     using (var db = new CoffeeContext()){
         db.Products.Add(product);
         db.SaveChanges();
     }
 }
        public override void Down()
        {
            // Change JSON for coffee options from KeyValuePair<string, KeyValuePair<string, string>> to KeyValuePair<string, int
            // THIS RESULTS IN DATA LOSS (of non-integer values)
            using (var db = new CoffeeContext())
            {
                // Get all the users
                foreach (var user in db.Users)
                {
                    // Transform JSON
                    List <KeyValuePair <string, KeyValuePair <string, string> > > old = JsonConvert.DeserializeObject <List <KeyValuePair <string, KeyValuePair <string, string> > > >(user.CoffeeOptionsJson);
                    List <KeyValuePair <string, int> > newOptions = new List <KeyValuePair <string, int> >();

                    foreach (KeyValuePair <string, KeyValuePair <string, string> > kvp in old)
                    {
                        if (kvp.Value.Key == "int")
                        {
                            newOptions.Add(
                                new KeyValuePair <string, int>(
                                    kvp.Key,
                                    Int32.Parse(kvp.Value.Value)
                                    )
                                );
                        }
                    }

                    user.CoffeeOptionsJson = JsonConvert.SerializeObject(newOptions);
                }

                db.SaveChanges();
            }
        }
        public override void Up()
        {
            // Change JSON for coffee options from KeyValuePair<string, int> to KeyValuePair<string, KeyValuePair<string, string>>
            using (var db = new CoffeeContext())
            {
                // Get all the users
                foreach (var user in db.Users)
                {
                    // Transform JSON
                    List <KeyValuePair <string, int> > old = JsonConvert.DeserializeObject <List <KeyValuePair <string, int> > >(user.CoffeeOptionsJson);
                    List <KeyValuePair <string, KeyValuePair <string, string> > > newOptions = new List <KeyValuePair <string, KeyValuePair <string, string> > >();

                    foreach (KeyValuePair <string, int> kvp in old)
                    {
                        newOptions.Add(
                            new KeyValuePair <string, KeyValuePair <string, string> >(
                                kvp.Key,
                                new KeyValuePair <string, string>(
                                    "int",
                                    kvp.Value.ToString())
                                )
                            );
                    }

                    user.CoffeeOptionsJson = JsonConvert.SerializeObject(newOptions);
                }

                db.SaveChanges();
            }
        }
示例#13
0
        protected void SetUpDatabase()
        {
            var options = new DbContextOptionsBuilder <CoffeeContext>()
                          .UseInMemoryDatabase(databaseName: "CoffeeDb")
                          .Options;

            Guid CoffeeEspressorId    = Guid.NewGuid();
            Guid EspressorEspressorId = Guid.NewGuid();

            Context = new CoffeeContext(options);

            Context.EspressoMachines.Add(new EspressoMachineEntity {
                Id = CoffeeEspressorId, IsEspressor = false
            });
            Context.EspressoMachines.Add(new EspressoMachineEntity {
                Id = EspressorEspressorId, IsEspressor = true
            });

            Context.Coffees.Add(new CoffeeEntity {
                Name = "First Coffee", Price = 20, Sweetness = Sweetness.Bitter, EspressoMachineId = CoffeeEspressorId
            });
            Context.Coffees.Add(new CoffeeEntity {
                Name = "Second Coffee", Price = 30, Sweetness = Sweetness.Sweet, EspressoMachineId = EspressorEspressorId
            });
            Context.Coffees.Add(new CoffeeEntity {
                Name = "Third Coffee", Price = 45, Sweetness = Sweetness.LessSweet, EspressoMachineId = EspressorEspressorId
            });

            Context.SaveChanges();
        }
示例#14
0
 public void UpdateUser(User user)
 {
     using (var coffeeContext = new CoffeeContext())
     {
         coffeeContext.Users.AddOrUpdate(user);
         coffeeContext.SaveChanges();
     }
 }
示例#15
0
 public void ClearOldReminders(DateTime oldestAcceptableDate)
 {
     using (var coffeeContext = new CoffeeContext())
     {
         var removethese = coffeeContext.Reminders.Where(r => r.CreatedOn < oldestAcceptableDate);
         coffeeContext.Reminders.RemoveRange(removethese);
         coffeeContext.SaveChanges();
     }
 }
示例#16
0
 public void SetWillBeThere(User forUser, bool value = true)
 {
     using (var coffeeContext = new CoffeeContext())
     {
         var user = coffeeContext.Users.SingleOrDefault(u => u.Id == forUser.Id);
         user.WillBeThere = value;
         coffeeContext.SaveChanges();
     }
 }
示例#17
0
        public void SaveReport(CoffeeReport coffeereport)
        {
            using (var coffeeContext = new CoffeeContext())
            {
                coffeeContext.CoffeeReports.Add(coffeereport);

                coffeeContext.SaveChanges();
            }
        }
示例#18
0
        public Order Edit(Order employee)
        {
            var editOd = context.Orders.Attach(employee);

            editOd.State = EntityState.Modified;
            context.SaveChanges();

            return(employee);
        }
示例#19
0
        public IActionResult Post(CoffeeItem item)
        {
            CoffeeItem exist = _context.CoffeeItems.Find(item.Id);

            if (exist == null)
            {
                _context.CoffeeItems.Add(item);
                _context.SaveChanges();
                return(CreatedAtRoute("coffee", new { id = item.Id }, item));
            }
            else
            {
                Debug.Print("SERVER INFO item name:" + item.Name);
                exist.Name = item.Name;
                _context.SaveChanges();
                return(CreatedAtRoute("coffee", new { id = item.Id }, item));
            }
        }
示例#20
0
        public JsonResult updateOrderStatus(long orderId, int currentStatus)
        {
            var         currentOrder = _context.Orders.FirstOrDefault(x => x.Id == orderId);
            OrderStatus currStatus   = (OrderStatus)Enum.Parse(typeof(OrderStatus), currentStatus.ToString());

            currentOrder.Status = currStatus;
            _context.SaveChanges();
            return(Json(currentOrder, JsonRequestBehavior.AllowGet));
        }
示例#21
0
 public void SetWillBeThere(User forUser, Reminder reminder)
 {
     SetWillBeThere(forUser);
     using (var coffeeContext = new CoffeeContext())
     {
         var dbReminder = coffeeContext.Reminders.Single(r => r.Id == reminder.Id);
         coffeeContext.Reminders.Remove(dbReminder);
         coffeeContext.SaveChanges();
     }
 }
示例#22
0
        public void RemoveUserFromCompany(long userId)
        {
            var user = _context.Set <User>().Include(uc => uc.UserCompanies).FirstOrDefault(item => item.Id == userId);

            var userCompany = user.UserCompanies.FirstOrDefault(item => item.UserId == userId);

            user.UserCompanies.Remove(userCompany);

            _context.SaveChanges();
        }
        public void AddEspressoMachine(EspressoMachineEntity espressoMachine)
        {
            if (string.IsNullOrEmpty(espressoMachine.Name))
            {
                throw new Exception("Name cannot be empty");
            }

            coffeeContext.EspressoMachines.Add(espressoMachine);
            coffeeContext.SaveChanges();
        }
示例#24
0
        public CoffeeDataController(CoffeeContext context)
        {
            _context = context;

            if (_context.coffeeShops.Count() == 0)
            {
                //_context.coffeeShops.Add(new CoffeeShop {Name = "Lupo"} );
                _context.SaveChanges();
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CoffeeContext db = new CoffeeContext();

            db.Roles.Find("Admin");
            if (!db.Roles.Any())
            {
                try
                {
                    db.Roles.AddRange(new List <UserRole>()
                    {
                        new UserRole()
                        {
                            Role = "User"
                        }, new UserRole()
                        {
                            Role = "Employee"
                        },
                        new UserRole()
                        {
                            Role = "Admin"
                        }
                    });
                    db.SaveChanges();
                    db.Users.Add(new User()
                    {
                        Login   = "******", Password = "******".GetSha256Hash(), RoleFK = "Admin", Name = "John",
                        Surname = "Doe"
                    });
                    db.SaveChanges();
                }
                catch (DbUpdateException ex)
                {
                    MessageBox.Show(ex.Message);
                    throw;
                }

                db.Dispose();
            }

            e.Handled = true;
        }
 public ActionResult Create(Product product)
 {
     product.ProductName = product.ProductName.Trim();
     if (_context.Products.Any(x => x.ProductName.ToLower() == product.ProductName.ToLower()))
     {
         ModelState.AddModelError("ProductName", "Product Name already exists!");
         ViewBag.Categories = LoadCategories(null);
         return(View());
     }
     else
     {
         product.CreatedBy   = "";
         product.CreatedDate = System.DateTime.Now;
         product.UpdatedBy   = "";
         product.UpdatedDate = System.DateTime.Now;
         _context.Products.Add(product);
         _context.SaveChanges();
         return(RedirectToAction("Index"));
     }
 }
示例#27
0
 public void SetMeeting(DateTime time)
 {
     using (var coffeeContext = new CoffeeContext())
     {
         var settings = coffeeContext.GlobalInformation.FirstOrDefault();
         settings             = settings ?? new GlobalInformation();
         settings.MeetingDate = time;
         coffeeContext.GlobalInformation.AddOrUpdate(settings);
         coffeeContext.SaveChanges();
     }
 }
示例#28
0
        public void ClearWillBeThere()
        {
            using (var coffeeContext = new CoffeeContext())
            {
                foreach (var user in coffeeContext.Users)
                {
                    user.WillBeThere = false;
                }

                coffeeContext.SaveChanges();
            }
        }
示例#29
0
        public IActionResult Create([FromBody] CoffeeShop item)
        {
            if (item == null)
            {
                return(BadRequest());
            }

            _context.coffeeShops.Add(item);
            _context.SaveChanges();

            //return CreatedAtRoute("GetCoffeeShop", new CoffeeShop{Id = item.Id});

            return(NoContent());
        }
示例#30
0
        public Reminder CreateReminder(User user)
        {
            using (var coffeeContext = new CoffeeContext())
            {
                var dbUser   = coffeeContext.Users.Include(u => u.Drink).SingleOrDefault(u => u.Id == user.Id);
                var reminder = new Reminder()
                {
                    User      = dbUser,
                    CreatedOn = DateTime.UtcNow,
                    Id        = Guid.NewGuid()
                };

                coffeeContext.Reminders.Add(reminder);
                coffeeContext.SaveChanges();
                return(reminder);
            }
        }