public ActionResult Create(view.User formUser) { lib.User libUser = view.Mapper.Map(formUser); userRepo.Create(db.Mapper.Map(libUser)); userRepo.SaveChanges(); return(RedirectToAction(nameof(Index))); }
/***** Library -> View *****/ public static view.User Map(lib.User libUser) { view.User viewUser = new view.User { Id = libUser.Id, FirstName = libUser.FirstName, LastName = libUser.LastName }; try { if (libUser.DefaultLocationId != null) { viewUser.DefaultLocationName = lib.Location.GetById((int)libUser.DefaultLocationId).Name; } else { viewUser.DefaultLocationName = null; } } catch (e.InvalidIdException) { viewUser.DefaultLocationName = null; } return(viewUser); }
/***** Library -> Database *****/ public static db.User Map(lib.User libUser) { db.User dbUser = new db.User { Id = libUser.Id, FirstName = libUser.FirstName, LastName = libUser.LastName, DefaultLocationId = libUser.DefaultLocationId }; return(dbUser); }
public static void SyncFromDatabase() { lib.User.Users.Clear(); List <db.User> users = userRepo.GetAllUsers(); foreach (var dbUser in users) { lib.User libUser = new lib.User { Id = dbUser.Id, FirstName = dbUser.FirstName, LastName = dbUser.LastName, DefaultLocationId = dbUser.DefaultLocationId }; } }
public ActionResult Delete(int id, view.User formUser) { try { lib.User libUser = lib.User.GetById(id); lib.User.Users.Remove(libUser); db.User dbUser = db.Mapper.Map(libUser); userRepo.Delete(dbUser); userRepo.SaveChanges(); return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
/***** Library -> View *****/ public static view.Order Map(lib.Order libOrder) { view.Order viewOrder = new view.Order { Id = libOrder.Id, TimePlaced = libOrder.TimePlaced, TotalPrice = libOrder.TotalPrice }; try { viewOrder.Location = lib.Location.GetById(libOrder.LocationId).Name; lib.User u = lib.User.GetById(libOrder.UserId); viewOrder.UserName = u.FirstName + " " + u.LastName; } catch (e.InvalidIdException e) { throw (e); } return(viewOrder); }
/***** View -> Library *****/ public static lib.User Map(view.User viewUser) { lib.User libUser = new lib.User { FirstName = viewUser.FirstName, LastName = viewUser.LastName }; if (viewUser.Id != 0) { libUser.Id = viewUser.Id; } try { libUser.DefaultLocationId = lib.Location.GetByName(viewUser.DefaultLocationName).Id; } catch (e.InvalidNameException) { viewUser.DefaultLocationName = null; } return(libUser); }
public ActionResult CreatePizza(view.Order formOrder, string AddPizza, string PlaceOrder, string Default, string Suggest) { lib.Order libOrder; if (formOrder.Id != 0) { libOrder = lib.Order.GetById(formOrder.Id); } else { libOrder = view.Mapper.Map(formOrder); } if (Suggest != null) { lib.Order temp = libOrder; libOrder = lib.Order.Orders.Where(o => o.UserId == libOrder.UserId).OrderByDescending(o => o.TimePlaced).FirstOrDefault(); if (libOrder != null) { lib.Order.Orders.Remove(libOrder); lib.Order o = new lib.Order { LocationId = libOrder.LocationId, UserId = libOrder.UserId }; foreach (var pizza in libOrder.Pizzas) { o.AddPizzaToOrder(pizza.Ingredients.Select(i => i.Name).ToList().ToArray()); } return(RedirectToAction(nameof(Create), new { o.Id })); } else { libOrder = temp; } } if (AddPizza != null) { if (libOrder.Pizzas.Count < 12 && libOrder.TotalPrice + formOrder.pizza.Price <= 500) { // 2 hour check lib.Order mostRecentOrder = lib.Order.Orders .Where(o => o.UserId == libOrder.UserId) .OrderByDescending(o => o.TimePlaced) .FirstOrDefault(); //if(mostRecentOrder != null) // if((DateTime.Now - mostRecentOrder.TimePlaced).Hours > 2) libOrder.AddPizzaToOrder(formOrder.pizza); } } else if (PlaceOrder != null || Default != null) { if (Default != null) { lib.User libUser = lib.User.GetById(libOrder.UserId); if (libUser.DefaultLocationId != null) { libOrder.LocationId = (int)libUser.DefaultLocationId; } } // Place the order try { // Attempt to place the order // Get all the ingredients required to place the order List <string> ingredients = new List <string>(); foreach (var pizza in libOrder.Pizzas) { foreach (var ingredient in pizza.Ingredients) { ingredients.Add(ingredient.Name); } } var inventory = ingredients.Select(s => new KeyValuePair <string, int>(s, ingredients.Where(s2 => s2 == s).Count())).ToList(); lib.Location.GetById(libOrder.LocationId).UpdateInventory(inventory); } catch (e.InsufficientIngredientException) { } lib.Order.OrderHasBeenPlace = true; libOrder.TimePlaced = DateTime.Now; db.Order dbOrder; List <db.OrderJunction> orderJunctionList; List <db.PizzaJunction> pizzaJunctionList; dbOrder = db.Mapper.Map(libOrder, out orderJunctionList, out pizzaJunctionList); foreach (var pizzaJunction in pizzaJunctionList) { orderRepo.pizzaJunctionRepo.Create(pizzaJunction); } foreach (var orderJunction in orderJunctionList) { orderRepo.orderJunctionRepo.Create(orderJunction); } orderRepo.Create(dbOrder); orderRepo.SaveChanges(); bool resync = false; return(RedirectToAction(nameof(Index), new { resync })); } return(RedirectToAction(nameof(Create), new { libOrder.Id })); }