public IHttpActionResult Put(int id, UserChoice userChoice) { if (!ModelState.IsValid || id != userChoice.Id) { return(BadRequest(ModelState)); } if (!CheckUserChoise(userChoice)) { return(BadRequest("Not enough money")); } userChoice.Menu = null; db.Entry(userChoice).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!UserChoiceExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Create([Bind(Include = "ID,FoodName,Type,Price")] Food food) { if (ModelState.IsValid) { db.Foods.Add(food); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(food)); }
public ActionResult Create([Bind(Include = "Id,Ime,Edm")] Artikal artikal) { if (ModelState.IsValid) { db.Artikli.Add(artikal); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(artikal)); }
public IHttpActionResult PutMenu(int id, Menu menu) { if (menu.Items != null) { foreach (var item in menu.Items) { if (item.Id == 0) { db.Items.Add(item); db.SaveChanges(); } } ModelState.Clear(); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != menu.Id) { return(BadRequest()); } foreach (var item in menu.Items) { item.Ratings = null; db.Entry(item).State = EntityState.Modified; } db.Entry(menu).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!MenuExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IHttpActionResult ParseMenu(string id, DateTime?start = null, int count = 0, bool update = false, bool next = false) { var result = new ParserResultView(); if (start == null) { start = DateTime.UtcNow.StartOfWeek().AddDays(7); } else { start = start.Value.ToUniversalTime(); } DateTime nextmonday; nextmonday = (DateTime)start; nextmonday = nextmonday.StartOfWeek().AddDays(7); var parser = allParsers.FirstOrDefault(p => p.Id() == id); if (parser == null) { return(NotFound()); } DateTime? old = null; List <Menu> menus = parser.ParseMenu(start); int num = 0; foreach (var menu in menus) { if (old != menu.OnDate) { ++num; old = menu.OnDate; } if (count > 0 && num > count) { --num; break; } foreach (var item in menu.Items.ToList()) { var dbitem = db.Items.Where(i => i.Name == item.Name && i.Weight == item.Weight).FirstOrDefault(); if (dbitem != null) { string dbParts = dbitem.Parts ?? ""; string menuParts = item.Parts ?? ""; if (dbParts.Length < menuParts.Length) { dbitem.Parts = item.Parts; db.Entry(dbitem).State = EntityState.Modified; } menu.Items.Remove(item); menu.Items.Add(dbitem); } } if (update) { var dbmenu = db.Menus .Where ( m => m.Name == menu.Name && DbFunctions.TruncateTime(m.OnDate) == DbFunctions.TruncateTime(menu.OnDate) && m.Price == menu.Price && m.Type == menu.Type ) .Include("Items") .FirstOrDefault(); if (dbmenu != null) { dbmenu.Items = menu.Items; db.Entry(dbmenu).State = EntityState.Modified; } else { db.Menus.Add(menu); } } else { db.Menus.Add(menu); } db.SaveChanges(); } if (next) { var nextmenus = parser.GetDayMenu(); foreach (Menu menu in nextmenus) { menu.OnDate = nextmonday; menu.Items.Clear(); db.Menus.Add(menu); db.SaveChanges(); } ++num; } result.Message = string.Format("Parse menus on {0} days.", num); return(Ok(result)); }