Skip to content

Commit

Permalink
feat(seeding,datetimes): added context seeders; added datetimes to ex…
Browse files Browse the repository at this point in the history
…penses and domain level utc normalizers as well as existing expenses specification extension
  • Loading branch information
mezdelex committed Nov 16, 2024
1 parent 110c4c4 commit f0e309b
Show file tree
Hide file tree
Showing 20 changed files with 361 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public sealed record PatchedExpenseEvent(
string Name,
string Description,
double Value,
DateTime Date,
Guid CategoryId
)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public record PostedExpenseEvent(
string Name,
string Description,
double Value,
DateTime Date,
Guid CategoryId
)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Application/Features/Queries/GetAllCategoriesQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public async Task<PagedList<CategoryDTO>> Handle(
CancellationToken cancellationToken
)
{
var redisKey = $"{nameof(GetAllCategoriesQuery)}#{request.Page}#{request.PageSize}";
var redisKey =
$"{nameof(GetAllCategoriesQuery)}#{request.Name}#{request.ContainedWord}#{request.Page}#{request.PageSize}";
var cachedPagedCategories = await _redisCache.GetCachedData<PagedList<CategoryDTO>>(
redisKey
);
Expand Down
7 changes: 6 additions & 1 deletion src/Application/Features/Queries/GetAllExpensesQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public sealed record GetAllExpensesQuery : BaseRequest, IRequest<PagedList<Expen
{
public string? Name { get; set; }
public string? ContainedWord { get; set; }
public DateTime? MinDate { get; set; }
public DateTime? MaxDate { get; set; }
public Guid? CategoryId { get; set; }

public sealed class GetAllExpensesQueryHandler
Expand All @@ -29,7 +31,8 @@ public async Task<PagedList<ExpenseDTO>> Handle(
CancellationToken cancellationToken
)
{
var redisKey = $"{nameof(GetAllExpensesQuery)}#{request.Page}#{request.PageSize}";
var redisKey =
$"{nameof(GetAllExpensesQuery)}#{request.Name}#{request.ContainedWord}#{request.MinDate}#{request.MaxDate}#{request.CategoryId}#{request.Page}#{request.PageSize}";
var cachedPagedExpenses = await _redisCache.GetCachedData<PagedList<ExpenseDTO>>(
redisKey
);
Expand All @@ -41,6 +44,8 @@ CancellationToken cancellationToken
new ExpensesSpecification(
name: request.Name,
containedWord: request.ContainedWord,
minDate: request.MinDate,
maxDate: request.MaxDate,
categoryId: request.CategoryId
)
)
Expand Down
9 changes: 8 additions & 1 deletion src/Application/Features/Shared/ExpenseDTO.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
namespace Application.Features.Shared;

public record ExpenseDTO(Guid Id, string Name, string Description, double Value, Guid CategoryId);
public record ExpenseDTO(
Guid Id,
string Name,
string Description,
double Value,
DateTime Date,
Guid CategoryId
);
7 changes: 7 additions & 0 deletions src/Domain/Conversors/DateTimeConversors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Domain.Conversors;

public static class DateTimeConversors
{
public static DateTime NormalizeToUtc(DateTime date) =>
date.Kind == DateTimeKind.Utc ? date : date.ToUniversalTime();
}
1 change: 1 addition & 0 deletions src/Domain/Entities/Expense.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class Expense : BaseEntity
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public double Value { get; set; }
public DateTime Date { get; set; }
public Guid CategoryId { get; set; }

public virtual Category Category { get; set; } = default!;
Expand Down
1 change: 1 addition & 0 deletions src/Domain/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
global using Ardalis.Specification;
global using Domain.Conversors;
global using Domain.Entities;
global using Microsoft.AspNetCore.Identity;
global using Microsoft.EntityFrameworkCore;
18 changes: 15 additions & 3 deletions src/Domain/Specifications/ExpensesSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public ExpensesSpecification(
Guid? id = null,
string? name = null,
string? containedWord = null,
DateTime? minDate = null,
DateTime? maxDate = null,
Guid? categoryId = null
)
{
Expand All @@ -17,14 +19,24 @@ public ExpensesSpecification(
if (!string.IsNullOrWhiteSpace(name))
Query.Where(x => x.Name.Equals(name));

if (categoryId != null)
Query.Where(x => x.CategoryId.Equals(categoryId));

if (!string.IsNullOrWhiteSpace(containedWord))
Query.Where(x =>
x.Name.Contains(containedWord) || x.Description.Contains(containedWord)
);

if (minDate.HasValue)
Query.Where(x =>
x.Date.CompareTo(DateTimeConversors.NormalizeToUtc(minDate.Value)) >= 0
);

if (maxDate.HasValue)
Query.Where(x =>
x.Date.CompareTo(DateTimeConversors.NormalizeToUtc(maxDate.Value)) <= 0
);

if (categoryId != null)
Query.Where(x => x.CategoryId.Equals(categoryId));

Query.OrderBy(x => x.CategoryId).ThenBy(x => x.Name);
}
}
1 change: 1 addition & 0 deletions src/Infrastructure/Contexts/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
protected override void OnModelCreating(ModelBuilder builder)
{
builder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
builder.SeedData();

base.OnModelCreating(builder);
}
Expand Down
96 changes: 96 additions & 0 deletions src/Infrastructure/Extensions/SeedingExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
namespace Infrastructure.Extensions;

public static class SeedingExtension
{
public static void SeedData(this ModelBuilder builder)
{
var ids = new List<Guid> { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };
builder
.Entity<Category>()
.HasData(
new()
{
Id = ids[0],
Name = "Groceries",
Description = "Groceries category.",
},
new()
{
Id = ids[1],
Name = "Transportation",
Description = "Transportation category.",
},
new()
{
Id = ids[2],
Name = "Leisure",
Description = "Leisure category.",
},
new()
{
Id = ids[3],
Name = "Utilities",
Description = "Utilities category.",
}
);

builder
.Entity<Expense>()
.HasData(
new()
{
Id = Guid.NewGuid(),
Name = "200g of chicken",
Description = "Some chicken bought in the supermarket",
Value = 2.05,
Date = DateTime.UtcNow,
CategoryId = ids[0],
},
new()
{
Id = Guid.NewGuid(),
Name = "1 can of beans",
Description = "Some chicken bought in the supermarket",
Value = 2,
Date = DateTime.UtcNow,
CategoryId = ids[0],
},
new()
{
Id = Guid.NewGuid(),
Name = "Full car tank",
Description = "Full car tank of 95",
Value = 50,
Date = DateTime.UtcNow,
CategoryId = ids[1],
},
new()
{
Id = Guid.NewGuid(),
Name = "Random film",
Description = "A film I watched",
Value = 5.90,
Date = DateTime.UtcNow,
CategoryId = ids[2],
},
new()
{
Id = Guid.NewGuid(),
Name = "Electricity",
Description = "Electricity bill",
Value = 40,
Date = DateTime.UtcNow,
CategoryId = ids[3],
},
new()
{
Id = Guid.NewGuid(),
Name = "Water",
Description = "Water bill",
Value = 15,
Date = DateTime.UtcNow,
CategoryId = ids[3],
}
);
}
}
1 change: 1 addition & 0 deletions src/Infrastructure/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
global using Domain.Persistence;
global using Infrastructure.Cache;
global using Infrastructure.Contexts;
global using Infrastructure.Extensions;
global using Infrastructure.MessageBrokers.RabbitMQ;
global using Infrastructure.Persistence;
global using Infrastructure.Repositories;
Expand Down
Loading

0 comments on commit f0e309b

Please sign in to comment.