public async Task <IEnumerable> GetUserExpenseStatsByProperty( Guid userId, DateTimeOffset startDate, DateTimeOffset endDate, ExpenseGroupByProperty groupByProperty) { var expensesResourceParameters = new ExpensesResourceParameters(); var expensesCollection = await _expenseRepository .GetUserExpenses(userId, expensesResourceParameters); switch (groupByProperty) { case ExpenseGroupByProperty.ExpenseDate: var queryByExpenseDate = expensesCollection .Where(s => s.ExpenseDate >= startDate && s.ExpenseDate <= endDate) .OrderBy(o => o.ExpenseDate) .GroupBy( exp => exp.ExpenseDate, exp => exp.Cost, (date, costs) => new { Day = date.ToString("d"), Costs = costs.Sum() }); return(await queryByExpenseDate.ToListAsync()); case ExpenseGroupByProperty.Category: var queryByCategory = expensesCollection .Where(s => s.ExpenseDate >= startDate && s.ExpenseDate <= endDate) .GroupBy( exp => exp.Category, exp => exp.Cost, (date, costs) => new { Category = date, Costs = costs.Sum() }); return(await queryByCategory.ToListAsync()); case ExpenseGroupByProperty.Title: var queryByTitle = expensesCollection .Where(s => s.ExpenseDate >= startDate && s.ExpenseDate <= endDate) .GroupBy( exp => exp.Title, exp => exp.Cost, (date, costs) => new { Title = date, Costs = costs.Sum() }); return(await queryByTitle.ToListAsync()); default: throw new ArgumentException(); } }
public async Task <ActionResult <IEnumerable> > GetUserExpenseStats( string userName, [FromQuery] DateTimeOffset startDate, DateTimeOffset endDate, ExpenseGroupByProperty statsByProperty) { var userIdFromToken = User.GetUserIdAsGuid(); string currentUserName = User.GetUserName(); if (currentUserName != userName) { return(Unauthorized()); } var dailyStats = await _statisticsService .GetUserExpenseStatsByProperty(userIdFromToken, startDate, endDate, statsByProperty); return(Ok(dailyStats)); }