public async Task <RemoveExpenditureCommand.Result> Execute(RemoveExpenditureCommand command)
        {
            if (command?.ExpenditureIds == null)
            {
                throw new ArgumentNullException("There is no selected expenditure");
            }

            var deletedExpenditures = await _db.Expenditures.Where(x => command.ExpenditureIds.Contains(x.KeyId)).ToListAsync();

            deletedExpenditures.ForEach(x =>
            {
                if (x.BudgetId != null)
                {
                    x.Account.Balance += x.Amount;
                    x.Budget.Balance  += x.Amount;
                    x.Budget.Expensed -= x.Amount;
                }
                else
                {
                    if (x.Account.Balance - x.Amount < 0)
                    {
                        throw new AggregateException("Account balance cannot be less than 0");
                    }
                    x.Account.Balance -= x.Amount;
                }

                _db.Expenditures.Remove(x);
            });
            var effectiveRows = await _db.SaveChangesAsync();

            return(new RemoveExpenditureCommand.Result
            {
                EffectiveRows = effectiveRows
            });
        }
示例#2
0
        public async Task <DepositToExistingAccountCommand.Result> Execute(DepositToExistingAccountCommand command)
        {
            var expenditure = new ExpenditureIncome
            {
                Amount          = command.Amount,
                AccountId       = command.AccountId,
                CreatedOn       = DateTime.Now,
                ExpenditureDate = command.ExpenditureDate,
                Description     = command.Description,
                LastTime        = DateTime.Now,
                CreatedById     = command.UserId,
                LastUserId      = command.UserId,
                UserId          = command.UserId
            };

            var accounts = await _db.Accounts.Where(x => x.UserId == command.UserId).ToListAsync();

            var account = accounts.FirstOrDefault(x => x.KeyId == command.AccountId);

            if (account != null)
            {
                account.Balance += command.Amount;
            }

            _db.Expenditures.Add(expenditure);

            var effectiveRows = await _db.SaveChangesAsync();

            return(new DepositToExistingAccountCommand.Result
            {
                EffectiveRows = effectiveRows
            });
        }
示例#3
0
        public async Task <SaveAccountCommand.Result> Execute(SaveAccountCommand command)
        {
            var account = new Account
            {
                Balance     = command.Amount,
                CreatedById = command.UserId,
                CreatedOn   = DateTime.Now,
                LastTime    = DateTime.Now,
                LastUserId  = command.UserId,
                UserId      = command.UserId,
                ShortName   = command.ShortName
            };

            var addedAccount = _db.Accounts.Add(account);
            var translation  = new AccountTranslation
            {
                CultureId       = command.CultureId,
                CreatedOn       = DateTime.Now,
                CreatedById     = command.UserId,
                LastTime        = DateTime.Now,
                LastUserId      = command.UserId,
                Name            = command.Name,
                TranslationOfId = addedAccount.KeyId
            };

            _db.AccountTranslations.Add(translation);

            var effectiveRows = await _db.SaveChangesAsync();

            return(new SaveAccountCommand.Result
            {
                EffectiveRows = effectiveRows
            });
        }
示例#4
0
        public async Task <SaveExpenditureCommand.Result> Execute(SaveExpenditureCommand command)
        {
            var expenditure = new Expenditure
            {
                BudgetId        = command.BudgetId,
                Amount          = command.Amount,
                AccountId       = command.AccountId,
                CreatedOn       = DateTime.Now,
                ExpenditureDate = command.ExpenditureDate,
                Description     = command.Description,
                LastTime        = DateTime.Now,
                CreatedById     = command.UserId,
                LastUserId      = command.UserId,
                UserId          = command.UserId
            };

            var accounts = await _db.Accounts.Where(x => x.UserId == command.UserId).ToListAsync();

            var account = accounts.FirstOrDefault(x => x.KeyId == command.AccountId);

            if (account?.Balance < command.Amount)
            {
                throw new Exception("Account doesn't have enough mooney for this expenditure");
            }

            if (account != null)
            {
                account.Balance -= command.Amount;
            }

            var budgets = await _db.Budgets.Where(x => x.UserId == command.UserId).ToListAsync();

            var budget = budgets.FirstOrDefault(x => x.KeyId == command.BudgetId);

            if (budget?.Balance < command.Amount)
            {
                throw new Exception("Budget doesn't have enough mooney for this expenditure");
            }

            if (budget != null)
            {
                budget.Balance  -= command.Amount;
                budget.Expensed += command.Amount;
            }

            _db.Expenditures.Add(expenditure);
            var effectiveRows = await _db.SaveChangesAsync();

            return(new SaveExpenditureCommand.Result
            {
                EffectiveRows = effectiveRows
            });
        }
示例#5
0
        public async Task <UpdateExpenditureCommand.Result> Execute(UpdateExpenditureCommand command)
        {
            var expenditureModel = await _db.Expenditures.FirstOrDefaultAsync(x => x.KeyId == command.KeyId);

            if (expenditureModel != null)
            {
                expenditureModel.AccountId = command.AccountId;
                var oldAmount = expenditureModel.Amount;
                expenditureModel.Amount          = command.Amount;
                expenditureModel.BudgetId        = command.BudgetId;
                expenditureModel.Description     = command.Description;
                expenditureModel.ExpenditureDate = command.ExpenditureDate;
                expenditureModel.LastTime        = DateTime.Now;
                expenditureModel.LastUserId      = command.UserId;

                var accounts = await _db.Accounts.Where(x => x.UserId == command.UserId).ToListAsync();

                var account = accounts.FirstOrDefault(x => x.KeyId == command.AccountId);

                if (account?.Balance + oldAmount < command.Amount)
                {
                    throw new Exception("Account doesn't have enough mooney for this expenditure");
                }

                if (account != null)
                {
                    account.Balance = account.Balance + oldAmount - command.Amount;
                }

                var budgets = await _db.Budgets.Where(x => x.UserId == command.UserId).ToListAsync();

                var budget = budgets.FirstOrDefault(x => x.KeyId == command.BudgetId);

                if (budget?.Balance + oldAmount < command.Amount)
                {
                    throw new Exception("Budget doesn't have enough mooney for this expenditure");
                }

                if (budget != null)
                {
                    budget.Balance  = budget.Balance + oldAmount - command.Amount;
                    budget.Expensed = budget.Expensed - oldAmount + command.Amount;
                }
            }

            var effectiveRows = await _db.SaveChangesAsync();

            return(new UpdateExpenditureCommand.Result
            {
                EffectiveRows = effectiveRows
            });
        }
示例#6
0
        public async Task <RemoveBudgetCommand.Result> Execute(RemoveBudgetCommand command)
        {
            if (command?.BudgetIds == null)
            {
                throw new ArgumentNullException("There is no selected budget");
            }

            var deletedBudget = await _db.Budgets.Where(x => command.BudgetIds.Contains(x.KeyId)).ToListAsync();

            deletedBudget.ForEach(x =>
            {
                _db.Budgets.Remove(x);
            });
            var effectiveRows = await _db.SaveChangesAsync();

            return(new RemoveBudgetCommand.Result
            {
                EffectiveRows = effectiveRows
            });
        }
示例#7
0
        public async Task <SaveBudgetCommand.Result> Execute(SaveBudgetCommand command)
        {
            var budget = new Budget
            {
                Balance       = command.Balance,
                CreatedById   = command.UserId,
                CreatedOn     = DateTime.Now,
                LastTime      = DateTime.Now,
                LastUserId    = command.UserId,
                UserId        = command.UserId,
                ShortName     = command.ShortName,
                EffectiveFrom = command.StartDate,
                EffectiveTo   = command.EndDate,
                Expensed      = command.Expensed,
                Total         = command.Total
            };

            var addedBudget = _db.Budgets.Add(budget);
            var translation = new BudgetTranslation
            {
                CultureId       = command.CultureId,
                CreatedOn       = DateTime.Now,
                CreatedById     = command.UserId,
                LastTime        = DateTime.Now,
                LastUserId      = command.UserId,
                Name            = command.Name,
                TranslationOfId = addedBudget.KeyId
            };

            _db.BudgetTranslations.Add(translation);

            var effectiveRows = await _db.SaveChangesAsync();

            return(new SaveBudgetCommand.Result
            {
                EffectiveRows = effectiveRows
            });
        }