Exemplo n.º 1
0
        public async Task <IActionResult> PutUser([FromRoute] string id, [FromBody] User user)
        {
            //string userId = User.Claims.First(c => c.Type == "UserID").Value;
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != user.Id)
            {
                return(BadRequest());
            }

            _context.Entry(user).State = EntityState.Modified;
            //var result = await _userManager.UpdateAsync(user);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    return(BadRequest("Bandykite dar kartą, arba trūksta duomenų"));
                }
            }

            return(NoContent());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PutProduct([FromRoute] int id, [FromBody] Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != product.Id)
            {
                return(BadRequest());
            }

            _context.Entry(product).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    return(BadRequest("Bandykite dar kartą, arba trūksta duomenų"));
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutOrderProduct([FromRoute] int id, [FromBody] OrderProduct orderProduct)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != orderProduct.Id)
            {
                return(BadRequest());
            }

            string       userId           = User.Claims.First(c => c.Type == "UserId").Value;
            OrderProduct tempOrderProduct = await _context.OrderProducts.AsNoTracking().FirstOrDefaultAsync(i => i.Id == id);

            orderProduct.OrderId   = tempOrderProduct.OrderId;
            orderProduct.ProductId = tempOrderProduct.ProductId;
            var tempOrder = await _context.Orders.FirstOrDefaultAsync(i => i.Id == tempOrderProduct.OrderId);

            if (User.IsInRole("Admin") || tempOrder.UserId.Equals(userId))//jei admin, gali visus
            {
                //order.Date = DateTime.Now.ToLocalTime();
                //order.UserId = tempOrder.UserId;
                _context.Entry(orderProduct).State = EntityState.Modified;

                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!OrderProductExists(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                        //return BadRequest("Bandykite dar kartą, arba trūksta duomenų");
                    }
                }

                return(NoContent());
                //return Ok(order);
            }

            return(Unauthorized());
        }