Exemplo n.º 1
0
        public async Task <IActionResult> Pay()
        {
            var idd = HttpContext.Session.Get <Dictionary <int, int> >("ls")[int.MaxValue];

            if (CreditCard.AppointmentId != idd)
            {
                return(NotFound());
            }
            if (!ModelState.IsValid)
            {
                return(View(CreditCard));
            }
            var id = CreditCard.AppointmentId;

            CreditCard.Appointments = null;

            List <ProductsSelectedForAppointment> psd = null;

            if (orm == 1)
            {
                qdb.incredit(CreditCard);
                psd = qdb.retpsa_with_ai(id);

                foreach (var item in psd)
                {
                    var product = qdb.retProduct(item.ProductId);
                    product.Count -= item.Count;
                    if (product.Count == 0)
                    {
                        product.Available = false;
                    }

                    qdb.upProduct(product);
                }

                var appointment = qdb.retAppointment(id);
                appointment.IsConfirmed = true;
                qdb.upAppointment(appointment);
            }
            else
            {
                _db.CreditCards.Add(CreditCard);
                await _db.SaveChangesAsync();

                psd = await _db.ProductsSelectedForAppointments.Where(e => e.AppointmentId == id)
                      .ToListAsync();

                foreach (var item in psd)
                {
                    var product = _db.Products.First(e => e.Id == item.ProductId);
                    product.Count -= item.Count;
                    if (product.Count == 0)
                    {
                        product.Available = false;
                    }
                }
                _db.Appointments.First(e => e.Id == id).IsConfirmed = true;
                await _db.SaveChangesAsync();
            }

            Dictionary <int, int> dic = new Dictionary <int, int>();

            HttpContext.Session.Set("ls", dic);
            TempData["congrats"] = 1;
            return(RedirectToAction("Index", "Home"));
        }
        public async Task <IActionResult> CreatePost()

        {
            if (!ModelState.IsValid)
            {
                List <Special_Tags> specialtags  = null;
                List <ProductTypes> producttypes = null;
                if (orm == 1)
                {
                    specialtags  = qdb.retSpecialTag();
                    producttypes = qdb.retProductType();
                }
                else
                {
                    specialtags  = _db.SpecialTags.ToList();
                    producttypes = _db.ProductTypes.ToList();
                }
                ProductsVm.SpecialTags  = specialtags;
                ProductsVm.ProductTypes = producttypes;
                return(View("Create", ProductsVm));
            }

            Products obj = null;

            if (orm == 1)
            {
                obj    = ProductsVm.Products;
                obj.Id = qdb.inProduct(ProductsVm.Products);
                //obj = qdb.retProduct(ProductsVm.Products);
            }
            else
            {
                _db.Products.Add(ProductsVm.Products);
                await _db.SaveChangesAsync();

                obj = await _db.Products.FindAsync(ProductsVm.Products.Id);
            }



            string path = _hostingEnvironment.WebRootPath;

            var files = HttpContext.Request.Form.Files;


            //if the user uploads image :
            if (files.Count != 0)
            {
                string extension = Path.GetExtension(files[0].FileName);
                string newName   = path + @"\" + SD.ImageFolder + @"\" + ProductsVm.Products.Id + extension;
                using (FileStream fs = new FileStream(newName, FileMode.Create))
                {
                    files[0].CopyTo(fs);
                }

                obj.Image = @"\" + SD.ImageFolder + @"\" + ProductsVm.Products.Id + extension;
            }
            //if user doesn't upload any image :
            else
            {
                string newName = path + @"\" + SD.ImageFolder + @"\" + SD.DefaultProductImage;
                System.IO.File.Copy(newName, path + @"\" + SD.ImageFolder + @"\" + ProductsVm.Products.Id + ".jpg");
                obj.Image = @"\" + SD.ImageFolder + @"\" + ProductsVm.Products.Id + ".jpg";
            }

            if (orm == 1)
            {
                qdb.upProduct(obj);
            }
            else
            {
                await _db.SaveChangesAsync();
            }

            TempData["create"] = 1;
            return(RedirectToAction(nameof(Index)));
        }