Ejemplo n.º 1
0
        public OperationResult CreateUser(string email, string language)
        {
            var userEntity = new User
            {
                Email = email,
            };

            using (var userRepository = repositoryFactory.CreateUserRepository())
                using (var accountRepository = repositoryFactory.CreateAccountRepository())
                    using (var preferencesRepository = repositoryFactory.CreatePreferencesRepository())
                        using (var categoryRepository = repositoryFactory.CreateCategoryRepository())
                        {
                            userRepository.Add(userEntity);

                            accountRepository.Add(new Account
                            {
                                UserId      = userEntity.Id,
                                Name        = language.Contains("en") ? "Cash" : "Efectivo",
                                AccountType = Domain.AccountType.Cash,
                                CurrencyId  = 1
                            });

                            accountRepository.Commit();

                            var categories = CategoryRepository.GetDefaultCategories().Select(x => new Category
                            {
                                Id         = x.Id,
                                Name       = x.Name,
                                Icon       = x.Icon,
                                IsActive   = true,
                                Measurable = true,
                                UserId     = userEntity.Id
                            }).ToList();

                            categoryRepository.Add(categories);
                            categoryRepository.Commit();

                            preferencesRepository.Add(new Preferences
                            {
                                UserId = userEntity.Id,
                                Key    = "PreferredCurrency",
                                Value  = "USD",
                            });
                            preferencesRepository.Add(new Preferences
                            {
                                UserId = userEntity.Id,
                                Key    = "Language",
                                Value  = language,
                            });
                            preferencesRepository.Commit();
                        }
            return(OperationResult.Succeed());
        }
Ejemplo n.º 2
0
        public void Add(TransactionModel transaction)
        {
            Category category;

            using (var transactionRepository = repositoryFactory.CreateTransactionRepository())
                using (var accountRepository = repositoryFactory.CreateAccountRepository())
                    using (var categoryRepository = repositoryFactory.CreateCategoryRepository())
                    {
                        if (transaction.CategoryId.Equals(default) || categoryRepository.FirstOrDefault(x => x.Id.Equals(transaction.CategoryId)) == null)
Ejemplo n.º 3
0
        public OperationResult Add(AccountModel accountModel)
        {
            using (var accountRepository = repositoryFactory.CreateAccountRepository())
            {
                var user = userService.GetAuthenticatedUser();

                var entity = accountMapper.ToEntity(accountModel);
                entity.UserId = user.Id;
                accountRepository.Add(entity);
                accountRepository.Commit();
            }
            return(OperationResult.Succeed());
        }
Ejemplo n.º 4
0
        public async Task <StorageItemModel> Upload(string filename, int transactionId, Stream stream)
        {
            using (var transactionRepository = repositoryFactory.CreateTransactionRepository())
                using (var accountRepository = repositoryFactory.CreateAccountRepository())
                    using (var storageItemRepository = repositoryFactory.CreateStorageItemRepository())
                    {
                        var transaction = transactionRepository.GetById(transactionId);
                        var account     = accountRepository.GetById(transaction.AccountId);

                        var storageItem = await storageClient.Upload(account.UserId, filename, account.Name, transaction.Name, stream);

                        storageItem.TransactionId = transactionId;

                        storageItemRepository.Add(storageItem);
                        storageItemRepository.Commit();

                        return(storageItemMapper.ToModel(storageItem));
                    }
        }
Ejemplo n.º 5
0
        public AccountsEvolutionModel GetAccountsBalancesEvolution()
        {
            using (var transactionRepository = repositoryFactory.CreateTransactionRepository())
                using (var accountRepository = repositoryFactory.CreateAccountRepository())
                {
                    var model = new AccountsEvolutionModel();

                    var auhtenticatedUser = userSessionService.GetAuthenticatedUser();

                    var accounts = accountRepository.GetAllByUser(auhtenticatedUser.Email);

                    int low  = 0;
                    int high = 0;

                    foreach (var account in accounts)
                    {
                        if (!account.Measurable)
                        {
                            continue;
                        }

                        var accountEvoModel = new AccountBalanceModel();

                        for (int i = 1; i <= DateTime.Now.Month; i++)
                        {
                            var incomeTransactions = transactionRepository
                                                     .Where(x => x.AccountId.Equals(account.Id) &&
                                                            x.Account.Measurable &&
                                                            x.TransactionType == TransactionType.Income &&
                                                            x.Date.Month.Equals(i) &&
                                                            x.Date.Year.Equals(DateTime.Now.Year))
                                                     .Sum(x => x.Price.ParseNoDecimals());

                            var outcomeTransactions = transactionRepository
                                                      .Where(x => x.AccountId.Equals(account.Id) &&
                                                             x.Account.Measurable &&
                                                             x.TransactionType == TransactionType.Expense &&
                                                             x.Date.Month.Equals(i) &&
                                                             x.Date.Year.Equals(DateTime.Now.Year))
                                                      .Sum(x => x.Price.ParseNoDecimals());

                            accountEvoModel.AccountId   = account.Id;
                            accountEvoModel.AccountName = account.Name;

                            var monthBalance = new MonthBalance
                            {
                                Balance = decimal.ToInt32(incomeTransactions) - decimal.ToInt32(outcomeTransactions),
                                Month   = new DateTime(DateTime.Now.Year, i, 1).ToString("MMM")
                            };

                            accountEvoModel.BalanceEvolution.Add(monthBalance);

                            if (monthBalance.Balance < low)
                            {
                                low = monthBalance.Balance;
                            }

                            if (monthBalance.Balance > high)
                            {
                                high = monthBalance.Balance;
                            }
                        }

                        model.Accounts.Add(accountEvoModel);
                    }

                    model.HighestValue = high + int.Parse((high * 0.25).ToString("F0"));
                    model.LowestValue  = low;

                    return(model);
                }
        }