Exemplo n.º 1
0
        public async Task <IActionResult> Update(Guid id, [FromBody] UpdateNarudzbinaDTO model)
        {
            var outNarudzbinaDTO = await _orderService.Update(id, model, HttpContext);

            if (outNarudzbinaDTO == null)
            {
                return(BadRequest());
            }

            return(Ok(outNarudzbinaDTO));
        }
Exemplo n.º 2
0
        public async Task <OutProdavacNarudzbinaDTO> Update(Guid id, UpdateNarudzbinaDTO model, HttpContext context)
        {
            var narudzbina = await _db.Narudzbine.Include(n => n.Kupac).Include(n => n.Prodavac)
                             .Include(n => n.ListaElemenata).ThenInclude(k => k.Proizvod).FirstOrDefaultAsync(a => a.Id == id);

            if (narudzbina == null)
            {
                return(null);
            }

            string userName = TokensHelper.GetClaimFromJwt(context, ClaimTypes.Name);

            var user = await _userManager.FindByNameAsync(userName);

            if (user == null)
            {
                throw new ErrorException(ErrorCode.UserNotFound, "Prodavac ne postoji u sistemu.");
            }

            if (narudzbina.Prodavac.Id != user.Id)
            {
                throw new ErrorException(ErrorCode.OrderAccessError, "Nemate pravo da pristupite ovoj narudzbini.");
            }

            narudzbina.StatusNarudzbine     = model.StatusNarudzbine;
            narudzbina.VremeIsporukeUDanima = model.VremeIsporukeUDanima;
            if (narudzbina.StatusNarudzbine == StatusNarudzbine.Odobrena)
            {
                narudzbina.DatumPotvrdeNarudzbine = DateTime.UtcNow;
            }
            else if (narudzbina.StatusNarudzbine == StatusNarudzbine.Odbijena)
            {
                narudzbina.DatumPotvrdeNarudzbine = null;
            }

            try
            {
                await _db.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw new ErrorException(ErrorCode.DbError, "Greška pri čuvanju narudzbine u bazu podataka.");
            }

            var outProdavacNarudzbina = new OutProdavacNarudzbinaDTO
            {
                Id = narudzbina.Id,
                StatusNarudzbine     = narudzbina.StatusNarudzbine,
                VremeIsporukeUDanima = narudzbina.VremeIsporukeUDanima,
                Kupac = new Account
                {
                    Address     = narudzbina.Kupac.Address,
                    Email       = narudzbina.Kupac.Email,
                    FirstName   = narudzbina.Kupac.FirstName,
                    LastName    = narudzbina.Kupac.LastName,
                    PhoneNumber = narudzbina.Kupac.PhoneNumber
                },
                ListaElemenata           = new List <OutElementKorpeDTO>(),
                DatumNarudzbine          = narudzbina.DatumNarudzbine,
                DatumOdobrenjaNarudzbine = narudzbina.DatumPotvrdeNarudzbine
            };

            foreach (var el in narudzbina.ListaElemenata)
            {
                outProdavacNarudzbina.ListaElemenata.Add(new OutElementKorpeDTO
                {
                    Kolicina = el.Kolicina,
                    Proizvod = new OutProizvodDTO
                    {
                        Id              = el.Proizvod.Id,
                        Naziv           = el.Proizvod.Naziv,
                        Cena            = el.Proizvod.Cena,
                        Opis            = el.Proizvod.Opis,
                        NacinKoriscenja = el.Proizvod.NacinKoriscenja,
                        Prodavac        = null
                    }
                });
            }

            return(outProdavacNarudzbina);
        }