Ejemplo n.º 1
0
        public OverPricedCategories GetMostOverPricedCategories(TransactionType transactionType)
        {
            var query = (from category in context.Set <Category>()
                         join transaction in context.Set <Transaction>()
                         on category.Id equals transaction.CategoryId
                         where transaction.TransactionType == transactionType
                         group category by category.Id into g
                         select new OverPricedCategory
            {
                Category = categoryMapper.ToModel(context.Set <Category>().FirstOrDefault(c => c.Id.Equals(g.Key))),
                Price = context.Set <Transaction>().Where(c => c.CategoryId.Equals(g.Select(s => s.Id).First()) && c.TransactionType == transactionType && c.Date.Month.Equals(DateTime.Now.Month))
                        .Sum(o => o.Price)
            }).ToList();

            query = query.OrderByDescending(x => x.Price).Take(5).ToList();

            if (query.Count.Equals(default(int)))
            {
                return(null);
            }

            var categories = new OverPricedCategories();

            foreach (var item in query.OrderBy(x => x.Category.Name))
            {
                item.Price = item.Price / 1000;
                categories.Categories.Add(item);
            }

            categories.HighestValue = query.Max(c => c.Price) + (query.Max(c => c.Price) / 4);
            categories.LowestValue  = query.Min(c => c.Price);

            return(categories);
        }
Ejemplo n.º 2
0
        public IEnumerable <MonthlyCategory> TopTransactionsByAccountAndMonth(int accountId, int month)
        {
            using (var transactionRepository = repositoryFactory.CreateTransactionRepository())
                using (var categoryRepository = repositoryFactory.CreateCategoryRepository())
                {
                    var auhtenticatedUser = userSessionService.GetAuthenticatedUser();

                    if (month.Equals(default(int)))
                    {
                        month = DateTime.Now.Month;
                    }

                    var userCategories = categoryRepository.GetActiveUserCategories(auhtenticatedUser.Email);

                    var transactions = transactionRepository
                                       .Where(x => x.AccountId.Equals(accountId) &&
                                              x.Account.Measurable &&
                                              x.Date.Month.Equals(month) &&
                                              x.Date.Year.Equals(DateTime.Now.Year) &&
                                              x.TransactionType == TransactionType.Expense &&
                                              x.Category.Measurable);

                    var accountTransactions = transactions
                                              .GroupBy(x => x.CategoryId)
                                              .Select(x => new MonthlyCategory
                    {
                        Category = categoryMapper.ToModel(userCategories.First(uc => uc.Id.Equals(x.First().CategoryId))),
                        Price    = x.Sum(d => d.Price)
                    });

                    return(accountTransactions);
                }
        }
Ejemplo n.º 3
0
        public IEnumerable <CategoryModel> GetActive()
        {
            var categoryRepository = repositoryFactory.CreateCategoryRepository();

            var authenticatedUser = userService.GetAuthenticatedUser();

            var categories = categoryRepository
                             .GetActiveUserCategories(authenticatedUser.Email)
                             .Select(x => categoryMapper.ToModel(x))
                             .ToList();

            return(categories);
        }