public ActionResult AddGame() { GameViewModel gvm = new GameViewModel(); Game objGame = new Game(); if (Request.Form["game.name"] == null) { return(View("AddGame", gvm)); } objGame.name = Request.Form["game.name"].ToString(); objGame.console = Request.Form["game.console"].ToString(); objGame.genre = Request.Form["game.genre"].ToString(); objGame.price = Request.Form["game.price"].ToString(); objGame.coverLink = Request.Form["game.coverLink"].ToString(); objGame.stock = Request.Form["game.stock"].ToString(); GameDal dal = new GameDal(); //data validation before updating db if (ModelState.IsValid) { dal.Games.Add(objGame); dal.SaveChanges(); gvm.game = new Game(); } else { gvm.game = objGame; } gvm.games = dal.Games.ToList <Game>(); return(View("AddGame", gvm)); }
public ActionResult checkOut() { //updates stock GameDal dal = new GameDal(); GameViewModel gvm = new GameViewModel(); gvm.games = new List <Game>(); //create game view model foreach (Game g in dal.Games) { gvm.games.Add(g); } //updates the stock for each game purchesed for (int j = 0; j < Global.cart.Count(); j++) { for (int i = 0; i < gvm.games.Count(); i++) { if (gvm.games[i].name == Global.cart[j].name) { if (Int32.Parse(gvm.games[i].stock) == 0) { gvm.games[i].stock = "0"; } else { gvm.games[i].stock = (Int32.Parse(gvm.games[i].stock) - 1).ToString(); } break; } } } //saves the updates dal.SaveChanges(); //addes a new order OrderDal odal = new OrderDal(); string uname = Session["userName"].ToString(); Order or = new Order(); or.userName = uname; int sum = Int32.Parse(Session["sum"].ToString()); or.sum = sum; odal.orders.Add(or); odal.SaveChanges(); Order o = odal.getLastOrder(); //connect all cart items to the above order ItemsDal idal = new ItemsDal(); foreach (Game g in Global.cart) { idal.items.Add(new Item(o.id, g.name)); } idal.SaveChanges(); //resets cart and global variables Global.cart = new List <Game>(); Global.sum = 0; Session["sum"] = "0"; //retuns thank you for your purchese return(View()); }