Exemplo n.º 1
0
        public WalletModel SelectByUserId(string currentUserId)
        {
            var wallet      = _context.Wallets.ToList().Where(s => s.UserId == currentUserId).FirstOrDefault();
            var walletModel = new WalletModel
            {
                Id      = wallet.Id,
                UserId  = wallet.UserId,
                Balance = wallet.Balance,
            };

            var walletHistories = _context.WalletsHistory.ToList().Where(h => h.BuyerWalletId == wallet.Id || h.SellerWalletId == wallet.Id || h.WalletId == wallet.Id).ToList();

            foreach (var walletHistory in walletHistories)
            {
                walletModel.WalletHistory.Add(new WalletHistoryModel
                {
                    Id                 = walletHistory.Id,
                    BuyerWalletId      = walletHistory.BuyerWalletId,
                    SellerWalletId     = walletHistory.SellerWalletId,
                    TransactionDate    = walletHistory.TransactionDate,
                    TransactionValue   = walletHistory.TransactionValue,
                    Description        = walletHistory.Description,
                    WalletIdUserLogged = walletModel.Id
                });
            }

            walletModel.Balance = CalculateBalance(walletModel);

            return(walletModel);
        }
Exemplo n.º 2
0
        public void CreateBalanceHistory(WalletModel walletModel)
        {
            _context.WalletsHistory.Add(new WalletHistory
            {
                TransactionValue = walletModel.HandlingValue,
                TransactionDate  = DateTime.Now,
                Description      = walletModel.HandlingValue > 0 ? "Entrada de saldo." : "Salida de saldo.",
                WalletId         = walletModel.Id
            });

            _context.SaveChanges();
        }
Exemplo n.º 3
0
        public decimal CalculateBalance(WalletModel walletModel)
        {
            var     transactions  = _context.Transactions.ToList().Where(t => t.BuyerId == walletModel.UserId && t.TransactionContemplated == false && t.TransactionEndDate == null).OrderByDescending(t => t.Bid).ToList();
            decimal bids          = 0;
            int     lastAuctionId = 0;

            transactions.ForEach(t =>
            {
                if (lastAuctionId != t.AuctionId)
                {
                    bids         += t.Bid;
                    lastAuctionId = t.AuctionId;
                }
            });

            return(walletModel.Balance - bids);
        }