public async Task <IActionResult> Register(Customer customer)
        {
            if (ModelState.IsValid)
            {
                var check = _context.Customer.FirstOrDefault(s => s.Email == customer.Email);
                if (check == null)
                {
                    customer.PassWord = GetMD5(customer.PassWord);
                    customer.JoinDate = DateTime.Now;
                    customer.isNew    = true;
                    customer.status   = "Active";
                    _context.Customer.Add(customer);
                    await _context.SaveChangesAsync();

                    //add session
                    HttpContext.Session.SetString("FullName", customer.FirstName + " " + customer.LastName);
                    HttpContext.Session.SetString("Email", customer.Email);
                    HttpContext.Session.SetInt32("idUser", customer.Id);
                    return(Redirect("/Home"));
                }
                else
                {
                    HttpContext.Session.SetString("Error", "Email already exists");
                }
            }
            return(Redirect("/Login"));
        }
Beispiel #2
0
        public async Task <IActionResult> Save()
        {
            if (Request.HasFormContentType && Request.Form != null && Request.Form.Count() > 0)
            {
                if (HttpContext.Session.GetString("success") == null)
                {
                    string url = "";
                    //paypal
                    var    payPalAPI = new PayPalAPI(configuration);
                    var    totala    = Request.Form["total"][0];
                    double total2    = Int32.Parse(totala) / 23139 + Int32.Parse(totala) % 23139;
                    url = await payPalAPI.getRedirectUrlToPayPal(total2, "USD");

                    HttpContext.Session.SetString("qrcode", url);
                    return(Redirect("/Checkout"));

                    //adddatabase
                    var Address = Request.Form["Address"][0];
                    HttpContext.Session.SetString("Address", Address);
                    var City = Request.Form["City"][0];
                    HttpContext.Session.SetString("City", City);
                    var County = Request.Form["County"][0];
                    HttpContext.Session.SetString("County", County);
                    var Postcode = Request.Form["Postcode"][0];
                    HttpContext.Session.SetString("Postcode", Postcode);
                    var Odernote = Request.Form["Odernote"][0];
                    HttpContext.Session.SetString("Odernote", Odernote);
                    var total = Request.Form["total"][0];
                    HttpContext.Session.SetString("total", total);
                    var Id = Request.Form["Id"][0];
                    HttpContext.Session.SetString("Id", Id);
                    var count = Request.Form["count"][0];
                    HttpContext.Session.SetString("count", count);
                    return(Redirect(url));
                }
            }
            if (HttpContext.Session.GetString("success") == "success")
            {
                //adddatabase
                var Address  = HttpContext.Session.GetString("Address");
                var City     = HttpContext.Session.GetString("City");
                var County   = HttpContext.Session.GetString("County");
                var Postcode = HttpContext.Session.GetString("Postcode");
                var Odernote = HttpContext.Session.GetString("Odernote");
                var total    = HttpContext.Session.GetString("total");
                var Id       = HttpContext.Session.GetString("Id");
                var count    = HttpContext.Session.GetString("count");

                Invoice invoice = new Invoice();
                invoice.Customer_Id     = Int32.Parse(Id);
                invoice.customer        = _context.Customer.FirstOrDefault(s => s.Id == Int32.Parse(Id));
                invoice.TotalMoney      = total;
                invoice.Postcode        = Postcode;
                invoice.amount          = count;
                invoice.CustomerAddress = Address + "," + City + "," + County;
                invoice.CreateDay       = DateTime.Now;
                invoice.Ordernote       = Odernote;
                invoice.status          = "Đang giao hàng";
                _context.Invoice.Add(invoice);
                await _context.SaveChangesAsync();

                //detail
                InvoiceDetail invoiceDetail = new InvoiceDetail();
                var           jsoncart      = "";
                var           jsoncartcombo = "";
                List <Item>   cart          = SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart");
                if (cart != null)
                {
                    jsoncart = JsonConvert.SerializeObject(cart);
                    invoiceDetail.Products = jsoncart;
                }
                List <ItemCombo> cartcombo = SessionCombo.GetObjectFromJsonCombo <List <ItemCombo> >(HttpContext.Session, "cartcombo");
                if (cartcombo != null)
                {
                    jsoncartcombo        = JsonConvert.SerializeObject(cartcombo);
                    invoiceDetail.Combos = jsoncartcombo;
                }
                var invoices = from s in _context.Invoice
                               select s;
                invoices = invoices.OrderByDescending(s => s.Id);
                Invoice a = invoices.First();
                invoiceDetail.Invoice_Id = a.Id;
                _context.InvoiceDetail.Add(invoiceDetail);
                await _context.SaveChangesAsync();

                //amount
                if (cart != null)
                {
                    foreach (var item in cart)
                    {
                        var products = from s in _context.Product
                                       select s;
                        products = products.Where(s => s.Id == item.Product.Id);
                        Product pro = products.FirstOrDefault();
                        pro.Amount = pro.Amount - item.Quantity;
                        await _context.SaveChangesAsync();

                        Removecart(item.Product.Id);
                    }
                }
                if (cartcombo != null)
                {
                    foreach (var item in cartcombo)
                    {
                        var cartcombos = from s in _context.Combo
                                         select s;
                        cartcombos = cartcombos.Where(s => s.Id == item.Combo.Id);
                        Combo cobo = cartcombos.First();
                        var   list = await _context.ComboProduct.Include("Products")
                                     .Where(s => s.Combo_Id == cobo.Id).ToListAsync();

                        foreach (var item1 in list)
                        {
                            item1.Products.Amount = item1.Products.Amount - item.Quantity;
                        }
                        await _context.SaveChangesAsync();

                        Removecombo(item.Combo.Id);
                    }
                }
            }
            if (HttpContext.Session.GetString("success") == "cancel")
            {
                return(Redirect("/Checkout"));
            }
            return(Redirect("/Checkout"));
        }