Ejemplo n.º 1
0
        public async Task <Packaging> AddPackagingMovementAsync(AddPackagingMovementRequest packagingMovement)
        {
            try
            {
                if (packagingMovement == null)
                {
                    throw new ArgumentNullException();
                }

                PackagingEntity packaging = await _context.Packagings
                                            .FindAsync(packagingMovement.PackagingId)
                                            .ConfigureAwait(false);

                if (packaging != null && IsValidAmount(packaging.Total, packagingMovement))
                {
                    packaging.Total = packagingMovement.Operation.Equals(Operation.Add) ?
                                      packaging.Total + packagingMovement.Amount :
                                      packaging.Total - packagingMovement.Amount;

                    PackagingMovementEntity entity = _mapper.Map <PackagingMovementEntity>(packagingMovement);
                    entity.Total = packaging.Total;

                    var added = await _context.PackagingMovements.AddAsync(entity);

                    if (added.State.Equals(EntityState.Added))
                    {
                        await _context.SaveChangesAsync().ConfigureAwait(false);

                        return(_mapper.Map <Packaging>(packaging));
                    }
                }

                _context.DetachAll();
            }
            catch (Exception e)
            {
                _context.DetachAll();
                _logger.LogError(e, "Exception: {e} // Internal Error while adding new Packaging movement: {packagingMovement}",
                                 e.Message, JsonSerializer.Serialize(packagingMovement));
            }

            return(null);
        }
Ejemplo n.º 2
0
 private bool IsValidAmount(int currentTotal, AddPackagingMovementRequest packagingMovement)
 {
     return(packagingMovement.Operation.Equals(Operation.Substract) ?
            (currentTotal - packagingMovement.Amount) >= 0 :
            packagingMovement.Amount >= 0);
 }