Esempio n. 1
0
        public static void BorrowItem(Item item, Stock stock, User user, int quantity)
        {
            // item operations
            if (stock != null)
            {
                stock.UpdatedAt = DateTime.Now;
            }
            stock.EntityState = EntityState.Modified;

            // borrowing operations
            Borrow borrow = BorrowController.DoesAlreadyEnrolledBySameUser(user.Id, stock.Id);

            if (borrow != null)
            {
                borrow             = item.Borrows.First(x => x.Id.Equals(borrow.Id));
                stock.Quantity    += borrow.Quantity;
                borrow.Quantity   += quantity;
                stock.Quantity    -= borrow.Quantity;
                borrow.BorrowTime  = DateTime.Now;
                borrow.ReturnTime  = null;
                borrow.IsReturn    = false;
                borrow.EntityState = EntityState.Modified;
            }
            else
            {
                borrow = new Borrow()
                {
                    User        = user,
                    User_Id     = user.Id,
                    BorrowTime  = DateTime.Now,
                    IsReturn    = false,
                    Item_Id     = item.Id,
                    Stock_Id    = stock.Id,
                    Quantity    = quantity,
                    EntityState = EntityState.Added
                };
                stock.Quantity -= borrow.Quantity;
                BorrowController.ManageBorrow(borrow, borrow.EntityState);
            }
            StockController.ManageStock(stock, EntityState.Modified);
            ManageItem(item, item.EntityState);
        }