Skip to content

Commit

Permalink
Builder uses Summary Mapper with template method
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanchiz1 committed May 12, 2024
1 parent db71144 commit 8ae81b8
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Domain.AggregatesModel.ReportAggregate.ReportBuilder;
using Domain.AggregatesModel.TransactionAggregate;

namespace Domain.AggregatesModel.ReportAggregate.CreateReportHandler;
namespace Domain.AggregatesModel.ReportAggregate.CreateReportHandlers;
public class CreateDailyReportHandler : CreateReportHandler
{
public IExpectsCurrency _reportBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Domain.AggregatesModel.TransactionAggregate;
using Domain.Extensions;

namespace Domain.AggregatesModel.ReportAggregate.CreateReportHandler;
namespace Domain.AggregatesModel.ReportAggregate.CreateReportHandlers;
public class CreateMonthlyReportHandler : CreateReportHandler
{
public IExpectsCurrency _reportBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Domain.AggregatesModel.TransactionAggregate;
using Domain.ValueObjects;

namespace Domain.AggregatesModel.ReportAggregate.CreateReportHandler;
namespace Domain.AggregatesModel.ReportAggregate.CreateReportHandlers;
public abstract class CreateReportHandler : ICreateReportHandler
{
private ICreateReportHandler _nextHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Domain.AggregatesModel.TransactionAggregate;
using Domain.Extensions;

namespace Domain.AggregatesModel.ReportAggregate.CreateReportHandler;
namespace Domain.AggregatesModel.ReportAggregate.CreateReportHandlers;
public class CreateWeeklyReportHandler : CreateReportHandler
{
public IExpectsCurrency _reportBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Domain.AggregatesModel.TransactionAggregate;

namespace Domain.AggregatesModel.ReportAggregate.CreateReportHandler;
namespace Domain.AggregatesModel.ReportAggregate.CreateReportHandlers;
public interface ICreateReportHandler
{
Report CreateReport(IEnumerable<Transaction> transactions);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Domain.AggregatesModel.TransactionAggregate;
using Domain.AggregatesModel.ReportAggregate.ReportBuilder.SummaryMappers;
using Domain.AggregatesModel.TransactionAggregate;
using Domain.Extensions;
using Domain.ValueObjects;

Expand Down Expand Up @@ -30,82 +31,27 @@ public IReportBuilder WithSummary(IEnumerable<Transaction> transactions)

public IReportBuilder WithDailySummary(IEnumerable<Transaction> transactions)
{
var dateRange = transactions.GetDateRange();

var currentDate = dateRange.Start;

while (currentDate <= dateRange.End)
{
decimal totalAmount = transactions.Where(t =>
t.OperationDate.IsDate(currentDate))
.Sum(o => o.RealAmount);

var summary = new Summary(totalAmount,
DateRange.Create(currentDate, currentDate)
);

Summaries.Add(summary);
SummaryMapper summaryMapper = new DailySummaryMapper();

currentDate = currentDate.AddDays(1);
}
Summaries = summaryMapper.MapToSummaries(transactions).ToList();

return this;
}

public IReportBuilder WithWeeklySummary(IEnumerable<Transaction> transactions)
{
var dateRange = transactions.GetDateRange();

var currentDate = dateRange.Start.GetStartOfWeek();

while (currentDate <= dateRange.End)
{
var startOfWeekDateTime = currentDate.GetStartOfWeek().ToDateTime(TimeOnly.MinValue);
var endOfWeekDateTime = currentDate.GetEndOfWeek().ToDateTime(TimeOnly.MaxValue);

decimal totalAmount = transactions.Where(t =>
t.OperationDate.Date >= startOfWeekDateTime &&
t.OperationDate.Date <= endOfWeekDateTime)
.Sum(o => o.RealAmount);

var summary = new Summary(totalAmount,
DateRange.Create(currentDate.GetStartOfWeek(), currentDate.GetEndOfWeek())
);
SummaryMapper summaryMapper = new WeeklySummaryMapper();

Summaries.Add(summary);

currentDate = currentDate.AddDays(7);
}
Summaries = summaryMapper.MapToSummaries(transactions).ToList();

return this;
}

public IReportBuilder WithMonthlySummary(IEnumerable<Transaction> transactions)
{
Summaries = [];

var dateRange = transactions.GetDateRange();

var currentDate = dateRange.Start.GetStartOfMonth();

while (currentDate <= dateRange.End)
{
var startOfMonthDateTime = currentDate.GetStartOfMonth().ToDateTime(TimeOnly.MinValue);
var endOfMonthDateTime = currentDate.GetEndOfMonth().ToDateTime(TimeOnly.MaxValue);

decimal totalAmount = transactions.Where(t =>
t.OperationDate.Date >= startOfMonthDateTime &&
t.OperationDate.Date <= endOfMonthDateTime)
.Sum(o => o.RealAmount);

var summary = new Summary(totalAmount,
DateRange.Create(currentDate.GetStartOfMonth(), currentDate.GetEndOfMonth())
);

Summaries.Add(summary);
SummaryMapper summaryMapper = new MonthlySummaryMapper();

currentDate = currentDate.AddMonths(1);
}
Summaries = summaryMapper.MapToSummaries(transactions).ToList();

return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Domain.AggregatesModel.TransactionAggregate;
using Domain.Extensions;
using Domain.ValueObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.AggregatesModel.ReportAggregate.ReportBuilder.SummaryMappers;
public class DailySummaryMapper : SummaryMapper
{
protected override DateOnly GetStartDate(DateOnly date)
{
return date;
}
protected override DateOnly NextDate(DateOnly currentDate)
{
return currentDate.AddDays(1);
}

protected override decimal GetTotalAmount(DateOnly currentDate, IEnumerable<Transaction> transactions)
{
return transactions.Where(t =>
t.OperationDate.IsDate(currentDate))
.Sum(o => o.RealAmount);
}

protected override Summary CreateSummary(DateOnly currentDate, decimal amount)
{
return new Summary(amount,
DateRange.Create(currentDate, currentDate)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Domain.AggregatesModel.TransactionAggregate;
using Domain.Extensions;
using Domain.ValueObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.AggregatesModel.ReportAggregate.ReportBuilder.SummaryMappers;
public class MonthlySummaryMapper : SummaryMapper
{
protected override DateOnly GetStartDate(DateOnly date)
{
return date.GetStartOfMonth();
}
protected override DateOnly NextDate(DateOnly currentDate)
{
return currentDate.AddMonths(1);
}

protected override decimal GetTotalAmount(DateOnly currentDate, IEnumerable<Transaction> transactions)
{
var startOfMonthDateTime = currentDate.GetStartOfMonth().ToDateTime(TimeOnly.MinValue);
var endOfMonthDateTime = currentDate.GetEndOfMonth().ToDateTime(TimeOnly.MaxValue);

return transactions.Where(t =>
t.OperationDate.Date >= startOfMonthDateTime &&
t.OperationDate.Date <= endOfMonthDateTime)
.Sum(o => o.RealAmount);
}

protected override Summary CreateSummary(DateOnly currentDate, decimal amount)
{
return new Summary(amount,
DateRange.Create(currentDate.GetStartOfMonth(),
currentDate.GetEndOfMonth())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Domain.AggregatesModel.TransactionAggregate;
using Domain.Extensions;
using Domain.ValueObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.AggregatesModel.ReportAggregate.ReportBuilder.SummaryMappers;
public abstract class SummaryMapper
{
public IEnumerable<Summary> MapToSummaries(IEnumerable<Transaction> transactions)
{
var summaries = new List<Summary>();

var dateRange = transactions.GetDateRange();

var currentDate = GetStartDate(dateRange.Start);

while (currentDate <= dateRange.End)
{
decimal totalAmount = GetTotalAmount(currentDate, transactions);

var summary = CreateSummary(currentDate, totalAmount);

summaries.Add(summary);

currentDate = NextDate(currentDate);
}

return summaries;
}

protected abstract DateOnly GetStartDate(DateOnly date);
protected abstract DateOnly NextDate(DateOnly currentDate);
protected abstract decimal GetTotalAmount(DateOnly currentDate, IEnumerable<Transaction> transactions);
protected abstract Summary CreateSummary(DateOnly currentDate, decimal amount);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Domain.AggregatesModel.TransactionAggregate;
using Domain.Extensions;
using Domain.ValueObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.AggregatesModel.ReportAggregate.ReportBuilder.SummaryMappers;
public class WeeklySummaryMapper : SummaryMapper
{
protected override DateOnly GetStartDate(DateOnly date)
{
return date.GetStartOfWeek();
}
protected override DateOnly NextDate(DateOnly currentDate)
{
return currentDate.AddDays(7);
}

protected override decimal GetTotalAmount(DateOnly currentDate, IEnumerable<Transaction> transactions)
{
var startOfWeekDateTime = currentDate.GetStartOfWeek().ToDateTime(TimeOnly.MinValue);
var endOfWeekDateTime = currentDate.GetEndOfWeek().ToDateTime(TimeOnly.MaxValue);

return transactions.Where(t =>
t.OperationDate.Date >= startOfWeekDateTime &&
t.OperationDate.Date <= endOfWeekDateTime)
.Sum(o => o.RealAmount);
}

protected override Summary CreateSummary(DateOnly currentDate, decimal amount)
{
return new Summary(amount,
DateRange.Create(currentDate.GetStartOfWeek(),
currentDate.GetEndOfWeek())
);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Domain.AggregatesModel.ReportAggregate.CreateReportHandler;
using Domain.AggregatesModel.ReportAggregate.CreateReportHandlers;
using Domain.AggregatesModel.ReportAggregate.CurrencyConversion;
using Domain.AggregatesModel.TransactionAggregate;
using Domain.ValueObjects;
Expand Down

0 comments on commit 8ae81b8

Please sign in to comment.