Ejemplo n.º 1
0
 public async Task <ActionResult <bool> > Put(ItemAmountChangeRequest request)
 {
     try
     {
         return(Ok(await service.ChangeCount(await users.GetByUsername(User.Identity.Name), request)));
     }
     catch (Exception)
     {
         return(NotFound($"User \'{User.Identity.Name}\' or item with ID {request.Id} could not be found!"));
     }
 }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public async Task <bool> ChangeCount(ShopUser user, ItemAmountChangeRequest request)
        {
            if (user == null)
            {
                throw new Exception();
            }

            var item = await items.SingleAsync(s => s.Id == request.Id);

            if ((request.Amount < 0 && Math.Abs(request.Amount) > item.Count))//Avoid negative numbers
            {
                throw new ArgumentException("You can't change an Item's count below zero!");
            }

            item.Count += request.Amount;

            items.Update(item);

            var returnamount = await context.SaveChangesAsync() > 0;

            await LogItem(user, item, request.Amount);

            return(returnamount);
        }