Skip to content

Commit

Permalink
get transactions by filtered date range
Browse files Browse the repository at this point in the history
  • Loading branch information
gwalus committed Jun 2, 2024
1 parent aa8911a commit 31add5c
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Dollet.Core.Abstractions.Repositories
{
public interface IExpensesRepository
{
Task<IEnumerable<Expense>> GetAllAsync(DateOnly? from = null, DateOnly? to = null, int? categoryId = null);
Task<IEnumerable<Expense>> GetAllAsync(DateTime? from = null, DateTime? to = null, int? categoryId = null);
Task<Expense?> GetAsync(int id);
void Add(Expense expense);
void Update(Expense expense);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Dollet.Core.Abstractions.Repositories
{
public interface IIncomesRepository
{
Task<IEnumerable<Income>> GetAllAsync(DateOnly? from = null, DateOnly? to = null, int? categoryId = null);
Task<IEnumerable<Income>> GetAllAsync(DateTime? from = null, DateTime? to = null, int? categoryId = null);
Task<Income?> GetAsync(int id);
void Add(Income income);
void Update(Income income);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Dollet.Core.Abstractions.Services
{
public interface IDateTimeRangeService
{
DateTime GetToday();
(DateTime, DateTime) GetWeekly();
(DateTime, DateTime) GetMonthly();
}
}
8 changes: 4 additions & 4 deletions src/Dollet.Core/Helpers/PeriodsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
using Dollet.Core.Enums;
namespace Dollet.Core.Helpers
{
public class PeriodsHelper(IDateOnlyRangeService dateOnlyRangeService)
public class PeriodsHelper(IDateTimeRangeService dateTimeRangeService)
{
private readonly IDateOnlyRangeService _dateOnlyRangeService = dateOnlyRangeService;
private readonly IDateTimeRangeService _dateOnlyRangeService = dateTimeRangeService;

public (DateOnly, DateOnly) GetDateRangeBasedOnSelectedPeriod(string selectedPeriod)
public (DateTime, DateTime) GetDateRangeBasedOnSelectedPeriod(string selectedPeriod)
{
switch (selectedPeriod)
{
Expand All @@ -25,7 +25,7 @@ public class PeriodsHelper(IDateOnlyRangeService dateOnlyRangeService)
}
}

public string GetStringPeriod((DateOnly, DateOnly) dateRange)
public static string GetStringPeriod((DateTime, DateTime) dateRange)
{
if (dateRange.Item1 == dateRange.Item2)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task DeleteAllAsync(int accountId)
_dbContext.Expenses.RemoveRange(toDelete);
}

public async Task<IEnumerable<Expense>> GetAllAsync(DateOnly? from = null, DateOnly? to = null, int? categoryId = null)
public async Task<IEnumerable<Expense>> GetAllAsync(DateTime? from = null, DateTime? to = null, int? categoryId = null)
{
var query = _dbContext.Expenses
.Include(x => x.Category)
Expand All @@ -36,8 +36,9 @@ public async Task<IEnumerable<Expense>> GetAllAsync(DateOnly? from = null, DateO

if (from.HasValue && to.HasValue)
{
query = query.Where(x => DateOnly.FromDateTime(x.Date) >= from.Value &&
DateOnly.FromDateTime(x.Date) <= to.Value);
query = query.Where(x =>
x.Date >= from.Value &&
x.Date <= to.Value);
}

if (categoryId.HasValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task DeleteAllAsync(int accountId)
_dbContext.Incomes.RemoveRange(toDelete);
}

public async Task<IEnumerable<Income>> GetAllAsync(DateOnly? from = null, DateOnly? to = null, int? categoryId = null)
public async Task<IEnumerable<Income>> GetAllAsync(DateTime? from = null, DateTime? to = null, int? categoryId = null)
{
var query = _dbContext.Incomes
.Include(x => x.Category)
Expand All @@ -36,8 +36,9 @@ public async Task<IEnumerable<Income>> GetAllAsync(DateOnly? from = null, DateOn

if (from.HasValue && to.HasValue)
{
query = query.Where(x => DateOnly.FromDateTime(x.Date) >= from.Value &&
DateOnly.FromDateTime(x.Date) <= to.Value);
query = query.Where(x =>
x.Date >= from.Value &&
x.Date <= to.Value);
}

if (categoryId.HasValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Dollet.Infrastructure.Services
{
internal class DateOnlyRangeService : IDateOnlyRangeService
internal class DateTimeRangeService : IDateTimeRangeService
{
public DateOnly GetToday()
public DateTime GetToday()
{
return DateOnly.FromDateTime(DateTime.Today);
return DateTime.Today;
}

public (DateOnly, DateOnly) GetWeekly()
public (DateTime, DateTime) GetWeekly()
{
var today = DateTime.Today;

Expand All @@ -22,14 +22,14 @@ public DateOnly GetToday()
}

var sunday = monday.AddDays(6);
return (DateOnly.FromDateTime(monday), DateOnly.FromDateTime(sunday));
return (monday, sunday);
}

public (DateOnly, DateOnly) GetMonthly()
public (DateTime, DateTime) GetMonthly()
{
var today = DateTime.Today;
var firstDayOfMonth = new DateOnly(today.Year, today.Month, 1);
var lastDayOfMonth = new DateOnly(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
var firstDayOfMonth = new DateTime(today.Year, today.Month, 1);
var lastDayOfMonth = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));

return (firstDayOfMonth, lastDayOfMonth);
}
Expand Down
6 changes: 1 addition & 5 deletions src/Dollet.Infrastructure/Services/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ internal static class Extensions
{
public static IServiceCollection AddServices(this IServiceCollection services, IConfiguration configuration)
{
//var config = configuration
// .GetRequiredSection("FreeCurrency")
// .Get<FreeCurrencyOptions>();

services.AddTransient(sp => new Freecurrencyapi("fca_live_g1Fta4EqDbFeAPwR3yETQE7SacgvlyfM9qZFGD1p"));

services.AddSingleton<IDateOnlyRangeService, DateOnlyRangeService>();
services.AddSingleton<IDateTimeRangeService, DateTimeRangeService>();
services.AddTransient<IDataSeedService, DataSeedService>();
services.AddTransient<IFreeCurrencyService, FreeCurrencyService>();

Expand Down
4 changes: 2 additions & 2 deletions src/Dollet.Presentation/Maui/Helpers/QueryParameterHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
internal static class QueryParameterHelper
{
public static (DateOnly, DateOnly, int) ExpensesDetails;
public static (DateOnly, DateOnly, int) IncomesDetails;
public static (DateTime, DateTime, int) ExpensesDetails;
public static (DateTime, DateTime, int) IncomesDetails;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<HorizontalStackLayout>
<Label
<Label
Text="{Binding}"
FontSize="18"
Padding="5, 15"
Padding="5, 15, 5, 10"
FontAttributes="Bold"
BackgroundColor="Transparent"/>

Expand Down Expand Up @@ -79,9 +79,19 @@
<RoundRectangle CornerRadius="10" BackgroundColor="Black"/>
</Border.StrokeShape>

<micro:ChartView
Chart="{Binding DonutChart}"
BackgroundColor="{AppThemeBinding Light={DynamicResource White}, Dark={DynamicResource Gray900}}"/>
<VerticalStackLayout>
<Label
Text="{Binding Period}"
HorizontalOptions="Center"
Padding="5"
FontSize="20"
FontAttributes="Italic"/>

<micro:ChartView
MinimumHeightRequest="150"
Chart="{Binding DonutChart}"
BackgroundColor="{AppThemeBinding Light={DynamicResource White}, Dark={DynamicResource Gray900}}"/>
</VerticalStackLayout>
</Border>

<CollectionView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,15 @@ public partial class ExpensesDetailsPageViewModel(IUnitOfWork unitOfWork) : Obse

public void ApplyQueryAttributes(IDictionary<string, object> query)
{
DateFrom = (DateOnly)query["DateFrom"];
DateTo = (DateOnly)query["DateTo"];
DateFrom = (DateTime)query["DateFrom"];
DateTo = (DateTime)query["DateTo"];
_categoryId = (int)query["CategoryId"];
}

private int _categoryId;

private DateOnly dateFrom, dateTo;
public DateOnly DateFrom
{
get => dateFrom;
private set { SetProperty(ref dateFrom, value); }
}
public DateOnly DateTo
{
get => dateTo;
private set { SetProperty(ref dateTo, value); }
}
[ObservableProperty]
private DateTime _dateFrom, _dateTo;

public ObservableRangeCollection<ExpensesDetailsGroupDto> Expenses { get; private set; } = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public partial class ExpensesPageViewModel(IUnitOfWork unitOfWork, IPopupService

public string SelectedPeriod { get; set; } = TransactionsPeriod.Daily.ToString();

private (DateOnly, DateOnly) dateRange;
private (DateTime, DateTime) dateRange;

[ObservableProperty]
private string _period;
Expand Down Expand Up @@ -81,14 +81,13 @@ private async Task CalculateExpensesAsync()
{
dateRange = _periodsHelper.GetDateRangeBasedOnSelectedPeriod(SelectedPeriod);

DateFrom = dateRange.Item1.ToDateTime(TimeOnly.Parse("00:00 AM"));
DateTo = dateRange.Item2.ToDateTime(TimeOnly.Parse("00:00 AM"));
DateFrom = dateRange.Item1;
DateTo = dateRange.Item2;
}

Period = _periodsHelper.GetStringPeriod(dateRange);
Period = PeriodsHelper.GetStringPeriod(dateRange);

// to fix
var expenses = await _expensesRepository.GetAllAsync(dateRange.Item1, dateRange.Item2);
var expenses = await _expensesRepository.GetAllAsync(DateFrom, DateTo);

if (expenses.Any())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ public partial class IncomesDetailsPageViewModel(IUnitOfWork unitOfWork) : Obser

private int _categoryId;

private DateOnly dateFrom, dateTo;
public DateOnly DateFrom { get => dateFrom; private set { SetProperty(ref dateFrom, value); } }
public DateOnly DateTo { get => dateTo; private set { SetProperty(ref dateTo, value); } }

[ObservableProperty]
private DateTime _dateFrom, _dateTo;

public ObservableRangeCollection<IncomesDetailsGroupDto> Incomes { get; private set; } = [];

public void ApplyQueryAttributes(IDictionary<string, object> query)
{
DateFrom = (DateOnly)query["DateFrom"];
DateTo = (DateOnly)query["DateTo"];
DateFrom = (DateTime)query["DateFrom"];
DateTo = (DateTime)query["DateTo"];
_categoryId = (int)query["CategoryId"];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public partial class IncomesPageViewModel(IUnitOfWork unitOfWork, PeriodsHelper

public string SelectedPeriod { get; set; } = TransactionsPeriod.Daily.ToString();

private (DateOnly, DateOnly) dateRange;
private (DateTime, DateTime) dateRange;

[ObservableProperty]
private DateTime _dateFrom = DateTime.Today, _dateTo = DateTime.Today;

[ObservableProperty]
string _period;
Expand All @@ -54,11 +57,17 @@ private async Task CalculateIncomesAsync()
var groupedIncomes = new List<IncomesGroupDto>();
var entries = new List<ChartEntry>();

dateRange = _periodsHelper.GetDateRangeBasedOnSelectedPeriod(SelectedPeriod);
if (SelectedPeriod != TransactionsPeriod.Custom.ToString())
{
dateRange = _periodsHelper.GetDateRangeBasedOnSelectedPeriod(SelectedPeriod);

DateFrom = dateRange.Item1;
DateTo = dateRange.Item2;
}

Period = _periodsHelper.GetStringPeriod(dateRange);
Period = PeriodsHelper.GetStringPeriod(dateRange);

var incomes = await _incomesRepository.GetAllAsync(dateRange.Item1, dateRange.Item2);
var incomes = await _incomesRepository.GetAllAsync(DateFrom, DateTo);

if (incomes.Any())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
x:Class="Dollet.Views.TransactionGroupItemView">

<Border
Padding="5, 3, 15, 3"
Padding="5, 5, 10, 5"
Margin="10, 5, 10, 0">

<Border.StrokeShape>
Expand Down

0 comments on commit 31add5c

Please sign in to comment.