Esempio n. 1
0
        public async Task MarkConsumableAsDeletedAsync(string token, int consumableId, string reason)
        {
            NullCheck.ThrowIfNull <string>(reason);

            if (reason.Trim().Length == 0)
            {
                throw new ArgumentException(FailureCodes.InvalidReason);
            }

            // Get consumable from database
            var query =
                from o in _context.offer as IQueryable <OfferEntity>
                join c in _context.consumable on o.id equals c.offer_id
                where token == o.token && c.id == consumableId
                select new { c, o };

            var foundConsumables = await query.ToListAsync();

            if (foundConsumables.Count == 0)
            {
                throw new DataNotFoundException(FailureCodes.NotFoundConsumable);
            }

            ConsumableEntity consumableEntity = foundConsumables[0].c;
            OfferEntity      offerEntity      = foundConsumables[0].o;

            consumableEntity.is_deleted = true;
            await consumableEntity.UpdateAsync(_context);

            await new ChangeEntity()
            {
                change_type      = ChangeEntityChangeType.DeleteResource,
                element_id       = consumableEntity.id,
                element_type     = ChangeEntityElementType.Consumable,
                element_category = consumableEntity.category,
                element_name     = consumableEntity.name,
                diff_amount      = consumableEntity.amount,
                reason           = reason,
                timestamp        = DateTime.Now,
                region           = offerEntity.region
            }.InsertAsync(_context);
        }
Esempio n. 2
0
        public async Task ChangeConsumableAmountAsync(string token, int consumableId, int newAmount, string reason)
        {
            NullCheck.ThrowIfNull <string>(token);
            NullCheck.ThrowIfNull <string>(reason);

            // Get consumable from database
            var query =
                from o in _context.offer as IQueryable <OfferEntity>
                join c in _context.consumable on o.id equals c.offer_id
                where token == o.token && c.id == consumableId
                select new { c, o };
            var foundConsumables = await query.ToListAsync();

            if (foundConsumables.Count == 0)
            {
                throw new DataNotFoundException(FailureCodes.NotFoundConsumable);
            }
            ConsumableEntity consumable = foundConsumables[0].c;
            OfferEntity      offer      = foundConsumables[0].o;

            // If amount has not changed: do nothing
            if (consumable.amount == newAmount)
            {
                return;
            }

            int diffAmount = Math.Abs(newAmount - consumable.amount);

            // If amount has increased: no reason required
            if (consumable.amount < newAmount)
            {
                consumable.amount = newAmount;
                await consumable.UpdateAsync(_context);

                // Add log
                await new ChangeEntity()
                {
                    change_type      = "INCREASE_AMOUNT",
                    element_id       = consumable.id,
                    element_type     = "consumable",
                    element_category = consumable.category,
                    element_name     = consumable.name,
                    diff_amount      = diffAmount,
                    reason           = reason,
                    timestamp        = DateTime.Now,
                    region           = offer.region
                }.InsertAsync(_context);

                return;
            }

            // If amount has decreased: ensure that a reason is provided
            if (reason.Trim().Length == 0)
            {
                throw new ArgumentException(FailureCodes.InvalidReason);
            }
            if (newAmount < 1)
            {
                throw new ArgumentException(FailureCodes.InvalidAmountConsumable);
            }
            consumable.amount = newAmount;
            await consumable.UpdateAsync(_context);

            // Add log
            await new ChangeEntity()
            {
                change_type      = "DECREASE_AMOUNT",
                element_id       = consumable.id,
                element_type     = "consumable",
                element_category = consumable.category,
                element_name     = consumable.name,
                diff_amount      = diffAmount,
                reason           = reason,
                timestamp        = DateTime.Now,
                region           = offer.region
            }.InsertAsync(_context);
        }