public void Dispose()
 {
     if (_db != null)
     {
         _db.Dispose();
         _db = null;
     }
 }
Esempio n. 2
0
        public IQueryable<Car> GetCars([QueryString("id")] int? categoryId, [RouteData] string categoryName)
        {
            var _db = new NewportCars.Models.CarContext();
                IQueryable<Car> query = _db.Cars;
                if (categoryId.HasValue && categoryId > 0)
                {
                    query = query.Where (c => c.CategoryID == categoryId);

                }

                if (!String.IsNullOrEmpty(categoryName))
                {
                    query = query.Where(c => String.Compare(c.Category.CategoryName, categoryName) == 0);
                }
                return query;
        }
 public IQueryable<Car> GetCar([QueryString("CarID")] int? CarID, [RouteData] string carMake)
 {
     var _db = new NewportCars.Models.CarContext();
     IQueryable<Car> query = _db.Cars;
     if (CarID.HasValue && CarID > 0)
     {
         query = query.Where(c => c.CarID == CarID);
     }
     else if (!String.IsNullOrEmpty(carMake))
     {
         query = query.Where(c => String.Compare(c.CarMake, carMake) == 0);
     }
     else
     {
         query = null;
     }
     return query;
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //verify user has completed the checkout porcess
                if ((string) Session["userCheckoutCompleted"] != "true")
                {
                    Session["userCheckoutCompleted"] = string.Empty;
                    Response.Redirect("CheckoutError.aspx?" + "Desc=Unvalidated%20Checkout.");
                }

                NVPAPICaller payPalCaller = new NVPAPICaller();

                string retMsg = "";
                string token = "";
                string finalPaymentAmount= "";
                string PayerID = "";
                NVPCodec decoder = new NVPCodec();

                token = Session["token"].ToString();
                PayerID = Session["payerId"].ToString();
                finalPaymentAmount = Session["payment_amt"].ToString();

                bool ret = payPalCaller.DoCheckoutPayment(finalPaymentAmount, token, PayerID, ref decoder, ref retMsg);
                if (ret)
                {
                    //retrieve PayPal confirmation value.
                    string PaymentConfirmation = decoder["PAYMENTINFO_0_TRANSACTIONID"].ToString();
                    TransactionId.Text = PaymentConfirmation;

                    CarContext _db = new CarContext();
                    //get the current order id
                    int currentOrderId = -1;
                    if (Session["currentOrderId"] != string.Empty)
                    {
                        currentOrderId = Convert.ToInt32(Session["currentOrderID"]);
                    }
                    Order myCurrentOrder;
                    if (currentOrderId >= 0)
                    {
                        //get the order based on order id
                        myCurrentOrder = _db.Orders.Single(o => o.OrderId == currentOrderId);
                        //update the order to reflect payment has been completed.
                        myCurrentOrder.PaymentTransactionId = PaymentConfirmation;
                        //save to db
                        _db.SaveChanges();
                    }

                    // clear shopping cart.
                    using (NewportCars.Logic.ShoppingCartActions usersShoppingCart = new NewportCars.Logic.ShoppingCartActions())
                    {
                        usersShoppingCart.EmptyCart();
                    }

                    //clear order id
                    Session["currentOrderId"] = string.Empty;
                }
                else
                {
                    Response.Redirect("CheckoutError.aspx?" + retMsg);
                }
            }
        }
Esempio n. 5
0
 public IQueryable<Category> GetCategories()
 {
     var _db = new NewportCars.Models.CarContext();
     IQueryable<Category> query = _db.Categories;
     return query;
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                NVPAPICaller payPalCaller = new NVPAPICaller();

                string retMsg = "";
                string token = "";
                string PayerID = "";
                NVPCodec decoder = new NVPCodec();
                token = Session["token"].ToString();

                bool ret = payPalCaller.GetCheckoutDetails(token, ref PayerID, ref decoder, ref retMsg);
                if (ret)
                {
                    Session["payerID"] = PayerID;

                    var myOrder = new Order();
                    myOrder.OrderDate = Convert.ToDateTime(decoder["TIMESTAMP"].ToString());
                    myOrder.Username = User.Identity.Name;
                    myOrder.FirstName = decoder["FIRSTNAME"].ToString();
                    myOrder.LastName = decoder["LASTNAME"].ToString();
                    myOrder.Address = decoder["STREETADDRESS"].ToString();
                    myOrder.City = decoder["CITY"].ToString();
                    myOrder.County = decoder["COUNTY"].ToString();
                    myOrder.Postcode = decoder["POSTCODE"].ToString();
                    myOrder.Country = decoder["COUNTRY"].ToString();
                    myOrder.Email = decoder["EMAIL"].ToString();
                    myOrder.Total = Convert.ToDecimal(decoder["AMT"].ToString());

                    //verify total payment amount as set on checkoutstart.aspx
                    try
                    {
                        decimal paymentAmountOnCheckout = Convert.ToDecimal(Session["payment_amt"].ToString());
                        decimal paymentAmountFromPaypal = Convert.ToDecimal(decoder["AMT"].ToString());
                        if (paymentAmountOnCheckout != paymentAmountFromPaypal)
                        {
                            Response.Redirect("CheckoutError.aspx?" + "Desc=Amount%20total%20mismatch.");
                        }
                    }
                    catch (Exception)
                    {
                        Response.Redirect("CheckoutError.aspx?" + "Desc=Amount%20total%20mismatch.");
                    }

                    //Get DB context
                    CarContext _db = new CarContext();

                    //Add order to DB
                    _db.Orders.Add(myOrder);
                    _db.SaveChanges();

                    //Get the shopping cart items and then process them
                    using (NewportCars.Logic.ShoppingCartActions usersShoppingCart = new NewportCars.Logic.ShoppingCartActions())
                    {
                        List<CartItem> myOrderList = usersShoppingCart.GetCartItems();

                        //add orderdetail information to the db for each car bought
                        for (int i = 0; i < myOrderList.Count; i++)
                        {
                            //create a new orderdetail object
                            var myOrderDetail = new OrderDetail();
                            myOrderDetail.OrderId = myOrder.OrderId;
                            myOrderDetail.Username = User.Identity.Name;
                            myOrderDetail.CarId = myOrderList[i].CarID;
                            myOrderDetail.CarPrice = myOrderList[i].Car.CarPrice;

                            //add order detail to db
                            _db.OrderDetails.Add(myOrderDetail);
                            _db.SaveChanges();
                        }

                        //set OrderId
                        Session["currentOrderId"] = myOrder.OrderId;

                        //display order information
                        List<Order> orderList = new List<Order>();
                        orderList.Add(myOrder);
                        DeliveryInfo.DataSource = orderList;
                        DeliveryInfo.DataBind();

                        //display orderdetails
                        OrderItemList.DataSource = myOrderList;
                        OrderItemList.DataBind();
                    }
                }
                else
                {
                    Response.Redirect("CheckoutError.aspx?" + retMsg);
                }
            }
        }
        protected void RemoveCarButton_Click(object sender, EventArgs e)
        {
            using (var _db = new NewportCars.Models.CarContext())
            {
                int carId = Convert.ToInt16(DropDownRemoveCar.SelectedValue);
                var myItem = (from c in _db.Cars where c.CarID == carId select c).FirstOrDefault();
                if (myItem != null)
                {
                    _db.Cars.Remove(myItem);
                    _db.SaveChanges();

                    //reload page
                    string pageUrl = Request.Url.AbsoluteUri.Substring(0,Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count());
                    Response.Redirect(pageUrl + "?CarAction=remove");
                }
                else
                {
                    LabelRemoveStatus.Text = "Unable to locate car.";
                }
            }
        }
 public IQueryable GetCars()
 {
     var _db = new NewportCars.Models.CarContext();
     IQueryable query = _db.Cars;
     return query;
 }
 public void UpdateShoppingCartDatabase(String cartId, ShoppingCartUpdates[] CartItemUpdates)
 {
     using (var db = new NewportCars.Models.CarContext())
     {
         try
         {
             int CartItemCount = CartItemUpdates.Count();
             List<CartItem> myCart = GetCartItems();
             foreach (var cartItem in myCart)
             {
                 //iterate through all rows within shopping cart list
                 for (int i = 0; i < CartItemCount; i++)
                 {
                     if (cartItem.Car.CarID == CartItemUpdates[i].CarId)
                     {
                         if (CartItemUpdates[i].PurchaseQuantity < 1 || CartItemUpdates[i].RemoveItem == true)
                         {
                             RemoveItem(cartId, cartItem.CarID);
                         }
                         else
                         {
                             UpdateItem(cartId, cartItem.CarID, CartItemUpdates[i].PurchaseQuantity);
                         }
                     }
                 }
             }
         }
         catch (Exception exp)
         {
             throw new Exception("ERROR: Unable to Update Cart Database - " + exp.Message.ToString(), exp);
         }
     }
 }
 public void UpdateItem(string updateCartID, int updateCarID, int quantity)
 {
     using (var _db = new NewportCars.Models.CarContext())
     {
         try
         {
             var myItem = (from c in _db.ShoppingCartItems where c.CartId == updateCartID && c.Car.CarID == updateCarID select c).FirstOrDefault();
             if (myItem != null)
             {
                 myItem.Quantity = quantity;
                 _db.SaveChanges();
             }
         }
         catch (Exception exp)
         {
             throw new Exception("ERROR: Unable to Update Cart Item - " + exp.Message.ToString(), exp);
         }
     }
 }
 public void RemoveItem(string removeCartID, int removeCarID)
 {
     using (var _db = new NewportCars.Models.CarContext())
     {
         try
         {
             var myItem = (from c in _db.ShoppingCartItems where c.CartId == removeCartID && c.Car.CarID == removeCarID select c).FirstOrDefault();
             if (myItem != null)
             {
                 //remove item
                 _db.ShoppingCartItems.Remove(myItem);
                 _db.SaveChanges();
             }
         }
         catch (Exception exp)
         {
             throw new Exception("ERROR: Unable to Remove Cart Item - " + exp.Message.ToString(), exp);
         }
     }
 }