Esempio n. 1
0
        public JsonResult <Object> AddUserAddress(string UserID, string AddressTitle, string Address)
        {
            bool result = false;

            try
            {
                UsersAddress userAddress = new UsersAddress();
                userAddress.AspNetUserID = UserID;
                userAddress.AddressTitle = AddressTitle;
                userAddress.Address      = Address;
                userAddress.DeliveringTo = true;

                using (var db = new BookPoolEntities())
                {
                    db.UsersAddresses.Where(x => x.AspNetUserID == UserID).Select(x => x).ForEach(x => x.DeliveringTo = false);
                    db.UsersAddresses.Add(userAddress);
                    db.SaveChanges();
                }

                result = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return(Json((object)new { result }));
        }
Esempio n. 2
0
        public JsonResult <Object> RemoveBookFromBasket(string UserID, int BookID)
        {
            bool results = false;

            try
            {
                using (var db = new BookPoolEntities())
                {
                    UserCart userCart = db.UserCarts.FirstOrDefault(x => x.UserID == UserID);
                    if (userCart != null)
                    {
                        List <int> booksIDsInCart = userCart.BooksIDsCSV.Split(',').Select(int.Parse).ToList();
                        booksIDsInCart.Remove(BookID);

                        userCart.BooksIDsCSV = string.Join(",", booksIDsInCart);
                        db.SaveChanges();
                    }
                }

                results = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return(Json((object)new { results }));
        }
Esempio n. 3
0
        public ActionResult UpdateUserInfo(string FirstName, string LastName, string Email, string PhoneNumber)
        {
            bool result = false;

            try
            {
                using (var db = new BookPoolEntities())
                {
                    string     thisUserID = User.Identity.GetUserId();
                    AspNetUser aspNetUser = db.AspNetUsers.First(x => x.Id == thisUserID);
                    aspNetUser.FirstName   = FirstName;
                    aspNetUser.LastName    = LastName;
                    aspNetUser.Email       = Email;
                    aspNetUser.PhoneNumber = PhoneNumber;

                    db.SaveChanges();
                }
                result = true;
            }
            catch (Exception ex)
            {
            }

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
Esempio n. 4
0
        public async Task <ActionResult> PlacedOrder(string books, string FirstName, string LastName, string Email, string PhoneNumber)
        {
            ViewBag.UserAddresses = await Global.Globals.GetUserAddresses(User.Identity.GetUserId());

            using (var db = new BookPoolEntities())
            {
                string     thisUserID = User.Identity.GetUserId();
                AspNetUser aspNetUser = db.AspNetUsers.First(x => x.Id == thisUserID);
                aspNetUser.FirstName   = FirstName;
                aspNetUser.LastName    = LastName;
                aspNetUser.Email       = Email;
                aspNetUser.PhoneNumber = PhoneNumber;

                db.SaveChanges();
            }

            Dictionary <string, string> apiResults = new Dictionary <string, string>();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(Global.Globals.baseURL);
                StringBuilder httpRoute = new StringBuilder();
                httpRoute.Append("api/Users/PlaceOrder");
                httpRoute.Append("?");
                httpRoute.AppendFormat("BuyerID={0}", User.Identity.GetUserId());
                httpRoute.Append("&");
                httpRoute.AppendFormat("BooksIDs={0}", books);

                var response = await client.GetAsync(httpRoute.ToString());

                if (response.IsSuccessStatusCode)
                {
                    apiResults = await response.Content.ReadAsAsync <Dictionary <string, string> >();

                    ViewBag.OrderNumber = apiResults["results"];
                }
            }
            return(View());
        }
Esempio n. 5
0
        public JsonResult <Object> DeliveringToUserAddress(string UserID, int AddressID)
        {
            bool result = false;

            try
            {
                using (var db = new BookPoolEntities())
                {
                    db.UsersAddresses.Where(x => x.AspNetUserID == UserID).Select(x => x).ForEach(x => x.DeliveringTo = false);
                    UsersAddress usersAddress = db.UsersAddresses.First(x => x.ID == AddressID);
                    usersAddress.DeliveringTo = true;
                    db.SaveChanges();
                }

                result = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return(Json((object)new { result }));
        }
Esempio n. 6
0
        public async Task <JsonResult <Object> > PlaceOrder(string BuyerID, string BooksIDs)
        {
            string results = string.Empty;

            decimal bookpoolCharges = Global.Globals.GetBookpoolCharges();

            try
            {
                using (var db = new BookPoolEntities())
                {
                    decimal    totalPrice     = 0;
                    List <int> booksIDsInCart = BooksIDs.Split(',').Select(int.Parse).ToList();
                    foreach (var bookID in booksIDsInCart)
                    {
                        AvailableBook availableBook = db.AvailableBooks.FirstOrDefault(x => x.ID == bookID);
                        totalPrice += (availableBook.Price + bookpoolCharges);
                    }

                    OrderHeader orderHeader = new OrderHeader();
                    orderHeader.ClientUserID = BuyerID;
                    orderHeader.OrderedOn    = DateTime.Now;
                    orderHeader.Status       = Globals.OrderStatusPending;
                    orderHeader.TotalPrice   = totalPrice;


                    db.OrderHeaders.Add(orderHeader);

                    db.SaveChanges();

                    results = orderHeader.ID.ToString() + orderHeader.OrderedOn.ToString("ddMMyy");

                    UserCart userCart = db.UserCarts.FirstOrDefault(x => x.UserID == BuyerID);

                    foreach (var bookID in booksIDsInCart)
                    {
                        AvailableBook availableBook = db.AvailableBooks.FirstOrDefault(x => x.ID == bookID);
                        if (availableBook != null)
                        {
                            availableBook.SellingStatus = Global.Globals.BookSellingStatus_NotAvailable;

                            OrderDetail orderDetail = new OrderDetail();
                            orderDetail.BookName      = availableBook.BookName;
                            orderDetail.BookPrice     = availableBook.Price + bookpoolCharges;
                            orderDetail.OrderHeaderID = orderHeader.ID;
                            orderDetail.Status        = Globals.OrderStatusPending;
                            orderDetail.SellerUserID  = availableBook.OwnerUserID;
                            orderDetail.BookID        = availableBook.ID;

                            GoogleBook googleResult = new GoogleBook();
                            using (var client = new HttpClient())
                            {
                                client.BaseAddress = new Uri(DataObjects.Global.Globals.googleBaseAPI_SearchByID);
                                StringBuilder httpRoute = new StringBuilder();
                                httpRoute.Append(availableBook.GoogleID);

                                var response = await client.GetAsync(httpRoute.ToString());

                                if (response.IsSuccessStatusCode)
                                {
                                    googleResult = await response.Content.ReadAsAsync <GoogleBook>();
                                }
                            }

                            orderDetail.BookImage = googleResult.volumeInfo.imageLinks.thumbnail.Replace("http:", "https:");
                            orderDetail.Authors   = string.Join(",", googleResult.volumeInfo.authors);
                            orderDetail.GoogleID  = googleResult.id;

                            db.OrderDetails.Add(orderDetail);

                            db.SaveChanges();
                        }


                        if (userCart != null)
                        {
                            List <int> userCartBooks = userCart.BooksIDsCSV.Split(',').Select(int.Parse).ToList();
                            userCartBooks.Remove(bookID);
                            userCart.BooksIDsCSV = string.Join(",", userCartBooks);
                            db.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return(Json((object)new { results }));
        }