public ActionResult Register(string username, string password) { try { using (BookStoreDataContext data = new BookStoreDataContext()) { var user = data.Users.SingleOrDefault(_ => _.Username == username); if (user == null) { user = new User { Username = username, Password = password }; data.Users.InsertOnSubmit(user); data.SubmitChanges(); return Json(new { results = new { userid = user.ID }, success = true }, JsonRequestBehavior.AllowGet); } else { return Json(new { success = false, reason = "userexists" }, JsonRequestBehavior.AllowGet); } } } catch (Exception ex) { return Json(new { success = false, reason = "servererror" }, JsonRequestBehavior.AllowGet); } }
public ActionResult SubmitViewLogs(int userid, string bookids, string times) { Thread.Sleep(1000); var items = new List<ViewLogParm>(); var bookidArray = bookids.Split(',').Select(a => int.Parse(a)).ToList(); var timeArray = times.Split(',').Select(a => DateTime.Parse(a)).ToList(); for (int i = 0; i < bookidArray.Count; i++) { items.Add(new ViewLogParm { bookid = bookidArray[i], time = timeArray[i] }); } using (BookStoreDataContext data = new BookStoreDataContext()) { data.ViewLogs.InsertAllOnSubmit(items.Select(_ => new ViewLog { BookId = _.bookid, Time = _.time, UserId = userid }).ToList()); data.SubmitChanges(); return Json(new { success = true }, JsonRequestBehavior.AllowGet); } }
public ActionResult ChangePassword(int id, string password) { try { using (BookStoreDataContext data = new BookStoreDataContext()) { var user = data.Users.SingleOrDefault(_ => _.ID == id); if (user != null) { user.Password = password; data.SubmitChanges(); return Json(new { success = true }, JsonRequestBehavior.AllowGet); } else { return Json(new { success = false, reason = "usernotexists" }, JsonRequestBehavior.AllowGet); } } } catch (Exception ex) { return Json(new { success = false, reason = "servererror" }, JsonRequestBehavior.AllowGet); } }
public ActionResult SubmitOrder(int userid, string bookids, string qtys) { try { var items = new List<OrderItemParm>(); var bookidArray = bookids.Split(',').Select(a=> int.Parse(a)).ToList(); var qtyArray = qtys.Split(',').Select(a => int.Parse(a)).ToList(); for (int i = 0; i < bookidArray.Count; i++) { items.Add(new OrderItemParm { bookid = bookidArray[i], qty = qtyArray[i] }); } using (BookStoreDataContext data = new BookStoreDataContext()) { var user = data.Users.SingleOrDefault(_ => _.ID == userid); if (user != null) { var order = new Order { Time = DateTime.Now, UserId = userid, }; foreach (var bookid in bookidArray) { var book = data.Books.SingleOrDefault(b => b.ID == bookid); if (book != null) { var oi = new OrderItem { BookId = bookid, Price = book.Price, Qty = items.Single(a => a.bookid == bookid).qty, }; oi.TotalPrice = oi.Price * oi.Qty; order.OrderItems.Add(oi); } } order.TotalPrice = order.OrderItems.Sum(_ => _.TotalPrice); data.Orders.InsertOnSubmit(order); data.SubmitChanges(); return Json(new { results = new { orderid = order.ID }, success = true }, JsonRequestBehavior.AllowGet); } else return Json(new { success = false, reason = "usernotexists" }, JsonRequestBehavior.AllowGet); } } catch (Exception ex) { return Json(new { success = false, reason = "servererror" }, JsonRequestBehavior.AllowGet); } }