Beispiel #1
0
        public async Task <IActionResult> Edit(int id, [Bind("MeniId,NarudzbaId,Kolicina,Cijena")] Spisakzanarudzbu spisakzanarudzbu)
        {
            if (id != spisakzanarudzbu.MeniId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(spisakzanarudzbu);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SpisakzanarudzbuExists(spisakzanarudzbu.MeniId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["MeniId"]     = new SelectList(_context.Meni, "Id", "Id", spisakzanarudzbu.MeniId);
            ViewData["NarudzbaId"] = new SelectList(_context.Narudzba, "Id", "Id", spisakzanarudzbu.NarudzbaId);
            return(View(spisakzanarudzbu));
        }
Beispiel #2
0
        public async Task <IActionResult> Create(int narudzbaId, [Bind("MeniId,NarudzbaId,Kolicina")] Spisakzanarudzbu spisakzanarudzbu)
        {
            if (ModelState.IsValid)
            {
                var stariSpisakZaNarudzbu = await _context.Spisakzanarudzbu.FindAsync(narudzbaId, spisakzanarudzbu.MeniId);

                var meni = await _context.Meni.FindAsync(spisakzanarudzbu.MeniId);

                var narudzba = await _context.Narudzba.FindAsync(narudzbaId);

                decimal novaCijena = (decimal)(spisakzanarudzbu.Kolicina * meni.Cijena);

                if (stariSpisakZaNarudzbu != null)
                {
                    if (meni.Kolicina > spisakzanarudzbu.Kolicina)
                    {
                        stariSpisakZaNarudzbu.Cijena   += novaCijena;
                        stariSpisakZaNarudzbu.Kolicina += spisakzanarudzbu.Kolicina;
                        _context.Update(stariSpisakZaNarudzbu);

                        narudzba.Cijena += novaCijena;
                        _context.Update(narudzba);

                        meni.Kolicina -= spisakzanarudzbu.Kolicina;
                        _context.Update(meni);

                        await _context.SaveChangesAsync();

                        return(RedirectToAction("Cart", "Spisakzanarudzbu", new { narudzbaId = stariSpisakZaNarudzbu.NarudzbaId }));
                    }
                    else
                    {
                        //greska
                    }
                }
                else
                {
                    spisakzanarudzbu.Cijena = novaCijena;
                    _context.Add(spisakzanarudzbu);

                    narudzba.Cijena += novaCijena;
                    _context.Update(narudzba);

                    meni.Kolicina -= spisakzanarudzbu.Kolicina;
                    _context.Update(meni);

                    await _context.SaveChangesAsync();

                    return(RedirectToAction("Cart", "Spisakzanarudzbu", new { narudzbaId = spisakzanarudzbu.NarudzbaId }));
                }
            }
            ViewData["MeniId"] = new SelectList(_context.Meni, "Id", "Naziv", spisakzanarudzbu.MeniId);
            return(View(spisakzanarudzbu));
        }