Ejemplo n.º 1
0
        public async Task <IActionResult> Index()
        {
            Korpa korpa = kServis.CitajKorpu();

            if (korpa.Stavke.Count() == 0)
            {
                return(RedirectToAction("Index", "Home"));
            }

            ApplicationUser user = await um.GetUserAsync(User);

            string     id = user.Id;
            Porudzbina p1 = new Porudzbina
            {
                KupacId       = id,
                DatumKupovine = DateTime.Now
            };

            try
            {
                db.Porudzbine.Add(p1);
                db.SaveChanges();
                int pId = p1.PorudzbinaId;

                foreach (StavkaKorpe st in korpa.Stavke)
                {
                    Stavka st1 = new Stavka
                    {
                        PorudzbinaId = pId,
                        ProizvodId   = st.Proizvod.ProizvodId,
                        Kolicina     = st.Kolicina
                    };

                    db.Stavke.Add(st1);
                    db.SaveChanges();
                }


                kServis.ObrisiKorpu();

                return(View());
            }
            catch (Exception)
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
        public async Task <ActionResult> Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email,
                    Ime      = model.Ime, Prezime = model.Prezime, Adresa = model.Adresa
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    Kupac k = new Kupac
                    {
                        KupacId = user.Id,
                        Ime     = user.Ime,
                        Prezime = user.Prezime,
                        Drzava  = "Srbija",
                        Grad    = "Beograd",
                        Adresa  = user.Adresa
                    };
                    db.Kupci.Add(k);
                    db.SaveChanges();

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(RedirectToAction("Index", "Home"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.TryAddModelError(error.Code, error.Description);
                }

                return(View(model));
            }


            return(View(model));
        }