Esempio n. 1
0
        public async Task <bool> Handle(CommitTransactionCommand command, CancellationToken cancellationToken)
        {
            var user = await _userManager.FindByEmailAsync(command.UserEmail);

            if (user.Balance < command.Amount)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { exception = "Not enough PW to commit transaction" });
            }

            if (user.Id == command.ReceiverUserId)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { exception = "You can't transfer PW to your account" });
            }

            var transaction = new Domain.Entities.Transaction()
            {
                ReceiverUserId = command.ReceiverUserId,
                Amount         = command.Amount,
                UserId         = user.Id,
                OperationDate  = DateTime.Now
            };

            _context.Transactions.Add(transaction);

            user.Balance -= transaction.Amount;
            await _userManager.UpdateAsync(user);

            await _context.UserTransactionInfos.AddAsync(new UserTransactionInfo()
            {
                UserId        = user.Id,
                TransactionId = transaction.Id,
                Amount        = transaction.Amount,
                FinalBalance  = user.Balance
            }, cancellationToken);

            var receiverUser = await _context.Users.FirstOrDefaultAsync(u => u.Id == transaction.ReceiverUserId, cancellationToken : cancellationToken);

            receiverUser.Balance += transaction.Amount;
            await _context.UserTransactionInfos.AddAsync(new UserTransactionInfo()
            {
                UserId        = receiverUser.Id,
                TransactionId = transaction.Id,
                Amount        = transaction.Amount,
                FinalBalance  = receiverUser.Balance
            }, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(true);
        }
Esempio n. 2
0
 public bool Deposit(int toAccount, decimal amount)
 {
     if (accountHandler.Deposit(toAccount, amount, _context))
     {
         Domain.Entities.Transaction toTransaction = new Domain.Entities.Transaction();
         toTransaction.AccountId = toAccount;
         toTransaction.Type      = "Credit";
         toTransaction.Operation = "Deposit";
         toTransaction.Amount    = amount;
         toTransaction.Date      = DateTime.Now;
         toTransaction.Balance   = _context.Accounts.Where(a => a.AccountId == toAccount).SingleOrDefault().Balance;
         _context.Transactions.Add(toTransaction);
         _context.Save();
         return(true);
     }
     ErrorMessage = accountHandler.ErrorMessage;
     return(false);
 }
Esempio n. 3
0
        public bool Withdrawal(int fromAccount, decimal amount)
        {
            if (accountHandler.IsWidthdrawalAllowed(fromAccount, amount, _context))
            {
                accountHandler.Withdrawal(fromAccount, amount, _context);

                Domain.Entities.Transaction fromTransaction = new Domain.Entities.Transaction();
                fromTransaction.AccountId = fromAccount;
                fromTransaction.Type      = "Debit";
                fromTransaction.Operation = "Withdrawal";
                fromTransaction.Amount    = amount;
                fromTransaction.Date      = DateTime.Now;
                fromTransaction.Balance   = _context.Accounts.Where(a => a.AccountId == fromAccount).SingleOrDefault().Balance;
                _context.Transactions.Add(fromTransaction);
                _context.Save();
                return(true);
            }
            ErrorMessage = accountHandler.ErrorMessage;
            return(false);
        }
Esempio n. 4
0
        public bool Transfer(int fromAccount, int toAccount, decimal amount)
        {
            if (accountHandler.AreDifferentAccounts(fromAccount, toAccount, _context))
            {
                if (accountHandler.IsWidthdrawalAllowed(fromAccount, amount, _context))
                {
                    accountHandler.Withdrawal(fromAccount, amount, _context);
                    accountHandler.Deposit(toAccount, amount, _context);

                    DateTime now = DateTime.Now;

                    Domain.Entities.Transaction fromTransaction = new Domain.Entities.Transaction();
                    fromTransaction.AccountId = fromAccount;
                    fromTransaction.Type      = "Debit";
                    fromTransaction.Operation = "Transfer to account " + toAccount;
                    fromTransaction.Amount    = amount;
                    fromTransaction.Date      = now;
                    fromTransaction.Balance   = _context.Accounts.Where(a => a.AccountId == fromAccount).SingleOrDefault().Balance;
                    fromTransaction.Account   = toAccount.ToString();
                    _context.Transactions.Add(fromTransaction);

                    Domain.Entities.Transaction toTransaction = new Domain.Entities.Transaction();
                    toTransaction.AccountId = toAccount;
                    toTransaction.Type      = "Credit";
                    toTransaction.Operation = "Transfer from account " + fromAccount;
                    toTransaction.Amount    = amount;
                    toTransaction.Date      = now;
                    toTransaction.Balance   = _context.Accounts.Where(a => a.AccountId == toAccount).SingleOrDefault().Balance;
                    toTransaction.Account   = toAccount.ToString();
                    _context.Transactions.Add(toTransaction);

                    _context.Save();
                    return(true);
                }
                ErrorMessage = accountHandler.ErrorMessage;
            }
            return(false);
        }
Esempio n. 5
0
        private static void Run()
        {
            Log("Scaning...");

            List<Site> sites;

            using (var db = new Context())
            {
                sites = db.Sites.ToList();
            }

            var now = DateTime.Now;

            foreach (var site in sites)
            {
                var site1 = site;
                switch (site1.Type)
                {
                    case Type.Ping:
                        new Thread(() =>
                        {
                            using (var db = new Context())
                            {
                                var s = db.Sites.Include(p => p.PatternTimes).Include(d => d.Downtimes).Single(x => x.Id == site1.Id);

                                var office = s.PatternTimes.Any( x => x.Days.Contains(now.DayOfWeek.ToString()) && x.TimeOpen <= now.Hour && now.Hour < x.TimeClose) ? Office.Open : Office.Closed;

                                var tr = new Transaction { Office = office, ResponseTime = DateTime.Now, Type = Type.Ping };

                                try
                                {
                                    var reply = new Ping().Send(IPAddress.Parse(site1.Ip), 10 * 1000); // 1 minute time out (in ms)

                                    tr.Status = (reply != null && reply.Status == IPStatus.Success) ? Status.Up : Status.Dn;
                                }
                                catch (Exception)
                                {
                                    tr.Status = Status.Dn;
                                }

                                UpdateStatus(s, tr);
                                db.SaveChanges();
                            }
                        }).Start();
                        break;

                    case Type.Snmp:
                        new Thread(() =>
                        {
                            using (var db = new Context())
                            {
                                var s = db.Sites.Include(p => p.PatternTimes).Include(d => d.Downtimes).Single(x => x.Id == site1.Id);

                                var office = s.PatternTimes.Any( x => x.Days.Contains(now.DayOfWeek.ToString()) && x.TimeOpen <= now.Hour && now.Hour < x.TimeClose) ? Office.Open : Office.Closed;

                                var tr = new Transaction { Office = office, ResponseTime = DateTime.Now, Type = Type.Snmp };

                                try
                                {
                                    Messenger.Get(VersionCode.V1, new IPEndPoint(IPAddress.Parse(site1.Ip), 161), new OctetString(site1.Community), new List<Variable> { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) }, 10 * 1000); // 1 minute time out (in ms)

                                    tr.Status = Status.Up;
                                }
                                catch (Exception)
                                {
                                    tr.Status = Status.Dn;
                                }

                                UpdateStatus(s, tr);
                                db.SaveChanges();
                            }
                        }).Start();
                        break;
                }
            }
        }
Esempio n. 6
0
        private static void UpdateStatus(Site site, Transaction transaction)
        {
            site.Transactions.Add(transaction);
            site.Status = transaction.Status;
            site.Office = transaction.Office;
            site.LastUpdate = transaction.ResponseTime;

            var dt = site.Downtimes.SingleOrDefault(d => d.UpTime == null);

            if (dt == null)
            {
                if (transaction.Status == Status.Dn)
                {
                    site.Downtimes.Add(new Downtime
                    {
                        DownTime = transaction.ResponseTime,
                        Office = transaction.Office
                    });
                }
            }
            else if (transaction.Status == Status.Up)
            {
                dt.UpTime = transaction.ResponseTime;
                dt.DurationTime = (transaction.ResponseTime - dt.DownTime).Minutes;
            }

            Log(site.Ip + " [" + transaction.Type + ":" + transaction.Status + "]");
        }
Esempio n. 7
0
        public void TestAddTransaction()
        {
            using (var db = new Context())
            {
                var site = new Site
                {
                    Code = "KKC03",
                    Name = "คังเซนฯ สุพรรณบุรี",
                    Ip = "192.168.13.254",
                    Type = Type.Snmp,
                    Isp = Isp.Tot,
                    Phone = "035-451629"
                };

                var trans = new Transaction
                {
                    Status = Status.Up,
                    //ResponseTime = DateTime.Now,
                    Type = Type.Snmp
                };

                site.Transactions.Add(trans);

                db.Sites.Add(site);
                db.SaveChanges();
            }
        }
Esempio n. 8
0
        public void TestDowntime()
        {
            using (var db = new Context())
            {
                var tr = new Transaction { Office = Office.Open, ResponseTime = DateTime.Now, Type = Type.Ping, Status = Status.Dn };

                var s = db.Sites.Include(d => d.Downtimes).Single(x => x.Id == 164);

                var dt = s.Downtimes.SingleOrDefault(d => d.UpTime == null);

                if (dt == null)
                {
                    if (tr.Status == Status.Dn)
                    {
                        s.Downtimes.Add(new Downtime
                        {
                            DownTime = tr.ResponseTime
                        });
                    }
                }
                else if (tr.Status == Status.Up)
                {
                    dt.UpTime = tr.ResponseTime;
                    dt.DurationTime = (tr.ResponseTime - dt.DownTime).Minutes;
                }

                db.SaveChanges();
            }
        }