Ejemplo n.º 1
0
 public ActionResult GetBooksByIds(string bookids)
 {
     try
     {
         using (BookStoreDataContext data = new BookStoreDataContext())
         {
             var bookidArray = bookids.Split(',').Select(_=> int.Parse(_)).ToArray();
             var query = data.Books.Where(book => bookidArray.Contains(book.ID)).Select(_ => new
             {
                 categoryId = _.CategoryId,
                 bookid = _.ID,
                 bookname = _.Name,
                 categoryname = _.Category.Name,
                 imagename = _.ImageName,
                 bookprice = _.Price,
             });
             var result = query.ToList();
             return Json(new { success = true, results = result }, JsonRequestBehavior.AllowGet);
         }
     }
     catch (Exception ex)
     {
         return Json(new { success = false, reason = "servererror" }, JsonRequestBehavior.AllowGet);
     }
 }
Ejemplo n.º 2
0
 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);
     }
 }
Ejemplo n.º 3
0
        public ActionResult GetBooks(int? categoryid, int? pageindex, int? pagesize)
        {
            try
            {
                using (BookStoreDataContext data = new BookStoreDataContext())
                {
                    var query = data.Books.Select(_ => new
                    {
                        categoryId = _.CategoryId,
                        bookid = _.ID,
                        bookname = _.Name,
                        categoryname = _.Category.Name,
                        imagename = _.ImageName,
                        bookprice = _.Price,
                        bookdesc = _.Description
                    });
                    if (categoryid != null)
                        query = query.Where(_ => _.categoryId == categoryid.Value);
                    if (pageindex != null)
                        query = query.Skip(pagesize.Value * pageindex.Value);
                    if (pagesize != null)
                        query = query.Take(pagesize.Value);

                    var result = query.ToList();
                    return Json(new { success = true, results = result }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, reason = "servererror" }, JsonRequestBehavior.AllowGet);
            }
        }
Ejemplo n.º 4
0
        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);
            }
        }
Ejemplo n.º 5
0
        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);
            }
        }
Ejemplo n.º 6
0
 public ActionResult Login(string username, string password)
 {
     try
     {
         using (BookStoreDataContext data = new BookStoreDataContext())
         {
             var user = data.Users.SingleOrDefault(_ => _.Username == username && _.Password == password);
             if (user != null)
             {
                 return Json(new { results = new { userid = user.ID }, success = true }, JsonRequestBehavior.AllowGet);
             }
             else
             {
                 return Json(new { success = false, reason = "authfailed" }, JsonRequestBehavior.AllowGet);
             }
         }
     }
     catch(Exception ex)
     {
         return Json(new { success = false, reason = "servererror" }, JsonRequestBehavior.AllowGet);
     }
 }
Ejemplo n.º 7
0
 public ActionResult GetBookCategories()
 {
     try
     {
         using (BookStoreDataContext data = new BookStoreDataContext())
         {
             return Json(new
             {
                 success = true,
                 results = data.Categories.Select(_ => new
                 {
                     id = _.ID,
                     name = _.Name,
                 }).ToList()
             }, JsonRequestBehavior.AllowGet);
         }
     }
     catch (Exception ex)
     {
         return Json(new { success = false, reason = "servererror" }, JsonRequestBehavior.AllowGet);
     }
 }
Ejemplo n.º 8
0
 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);
     }
 }
Ejemplo n.º 9
0
 public ActionResult GetOrders(int userid)
 {
     try
     {
         using (BookStoreDataContext data = new BookStoreDataContext())
         {
             var user = data.Users.SingleOrDefault(_ => _.ID == userid);
             if (user != null)
             {
                 return Json(new
                 {
                     results = data.Orders.Where(o=>o.UserId == userid).Select(_ => new
                     {
                         orderid = _.ID,
                         totalprice = _.TotalPrice,
                         time = _.Time,
                         orderitems = _.OrderItems.Select(__ => new
                         {
                             bookid = __.BookId,
                             bookname = data.Books.SingleOrDefault(b => b.ID == __.BookId).Name,
                             price = __.Price,
                             totalprice = __.TotalPrice,
                             qty = __.Qty
                         }).ToList(),
                     }).OrderByDescending(_ => _.time).ToList()
                 , 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);
     }
 }