Esempio n. 1
0
        public IActionResult Index(NaruzbaViewModel model)
        {
            NaruzbaViewModel returnModel = new NaruzbaViewModel();

            PopulateModel(ref returnModel);

            if (!ModelState.IsValid)
            {
                return(View(returnModel));
            }


            Narudzba narudzba = new Narudzba
            {
                Datum          = DateTime.UtcNow,
                Kanton         = model.Kanton,
                Ulica          = model.Ulica,
                KontaktTelefon = model.KontaktTelefon,
                PostanskiBroj  = model.PostanskiBroj,
                UkupnaCijena   = returnModel.Detalji.TotalPrice,
                Stanje         = 1,
                KupacId        = kupacService.GetKupacByAspUserId(_userManager.GetUserId(HttpContext.User)),
                Opcina         = model.Opcina
            };

            List <StavkaNarudzbe> listaStavki = new List <StavkaNarudzbe>();

            foreach (var item in returnModel.Detalji.Items)
            {
                var stavka = new StavkaNarudzbe
                {
                    MobitelId = item.Product.Id,
                    Kolicina  = item.Quantity,
                    Cijena    = item.Product.mobitel.Cijena
                };
                listaStavki.Add(stavka);
            }


            narudzbaService.InsertNarudzba(narudzba, listaStavki);

            // posalji sms zahvale kupcu
            string poruka = "Hvala vam na naruzbi sa webshopmobitela, " + model.Ime + ".Vasa naruzba ce ubrzo biti dostavljena u ulicu " + model.Ulica + ".";

            smsService.SendSms(new SmsModel {
                To = model.FullPhone, Text = poruka
            });


            // nakon uspjesne narudzbe ukloni sve iz kosarice.
            List <Item> cart = SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, CartName);

            cart.RemoveAll(x => x.Quantity > 0);
            SessionHelper.SetObjectAsJson(HttpContext.Session, CartName, cart);


            return(RedirectToAction("Narudzbe"));
        }
        public IActionResult Index(NarudzbaViewModel model)
        {
            NarudzbaViewModel returnModel = new NarudzbaViewModel();

            PopulateModel(ref returnModel);

            if (!ModelState.IsValid)
            {
                return(View(returnModel));
            }


            Narudzba narudzba = new Narudzba
            {
                DatumNarudzbe = DateTime.UtcNow,
                Kontakt       = model.KontaktTelefon,
                UkupnaCijena  = returnModel.Detalji.TotalPrice,
                Status        = 1,
                KupacId       = KupacService.GetKupacByAspUserId(UserManager.GetUserId(HttpContext.User))
            };

            List <StavkaNarudzbe> listaStavki = new List <StavkaNarudzbe>();

            foreach (var item in returnModel.Detalji.Items)
            {
                var stavka = new StavkaNarudzbe
                {
                    ArtikalId = item.Product.Id,
                    Kolicina  = item.Quantity,
                    Cijena    = item.Product.artikal.Cijena
                };
                listaStavki.Add(stavka);
            }


            NarudzbaService.InsertNarudzba(narudzba, listaStavki);
            var VONAGE_API_KEY    = Configuration["VONAGE_API_KEY"];
            var VONAGE_API_SECRET = Configuration["VONAGE_API_SECRET"];
            var credentials       = Credentials.FromApiKeyAndSecret(VONAGE_API_KEY, VONAGE_API_SECRET);
            var client            = new SmsClient(credentials);
            var request           = new SendSmsRequest {
                To = model.KontaktTelefon, From = model.KontaktTelefon, Text = "Uspjesno ste narucili artikal!"
            };
            var response = client.SendAnSms(request);

            ViewBag.MessageId = response.Messages[0].MessageId;

            // Delete sve iz kosarice nakon uspjesne narudzbe
            List <Item> cart = SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, CartName);

            cart.RemoveAll(x => x.Quantity > 0);
            SessionHelper.SetObjectAsJson(HttpContext.Session, CartName, cart);



            return(View("NarudzbaConfirmation"));
        }
        public IActionResult Komentiraj(string komentar, int mobitelId)
        {
            if (komentar.IsNullOrEmpty() || komentar.Length > 1000)
            {
                ModelState.AddModelError("predugkomentar", "Vas komentar je predug, isti mora biti manji od 1000 karaktera.");
                MobitelIndexViewModel model = new MobitelIndexViewModel();
                model.Mobitel   = MobitelViewModel.ConvertToMobitelViewModel(mobitelService.GetMobitel(mobitelId));
                model.Komentari = komentarService.GetAllKomentariByPhoneId(mobitelId);
                return(View(model));
            }
            var userId  = _userManager.GetUserId(HttpContext.User);
            int kupacId = kupacService.GetKupacByAspUserId(userId);

            komentarService.InsertKomentar(new Komentar {
                Datum = DateTime.UtcNow, KupacId = kupacId, Sadrzaj = komentar, MobitelId = mobitelId
            });
            return(RedirectToAction("Index", new { id = mobitelId }));
        }