Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/Api/Services/BackgroundWorkers.cs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
using Application.Common.Interfaces;
using Domain.Statuses;
using Microsoft.EntityFrameworkCore;
using NodaTime;

Expand All@@ -20,7 +21,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
var workers = new List<Task>
{
DisposeExpiredEntries(TimeSpan.FromSeconds(10), stoppingToken),
DisposeExpiredPermissions(TimeSpan.FromSeconds(10), stoppingToken)
DisposeExpiredPermissions(TimeSpan.FromSeconds(10), stoppingToken),
HandleOverdueRequest(TimeSpan.FromMinutes(2),stoppingToken),
};

await Task.WhenAll(workers.ToArray());
Expand DownExpand Up@@ -51,4 +53,25 @@ private async Task DisposeExpiredPermissions(TimeSpan delay, CancellationToken s
await context.SaveChangesAsync(stoppingToken);
await Task.Delay(delay, stoppingToken);
}

private async Task HandleOverdueRequest(TimeSpan delay, CancellationToken stoppingToken)
{
var localDateTimeNow = LocalDateTime.FromDateTime(DateTime.Now);
using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<IApplicationDbContext>();
var overdueRequests = context.Borrows
.Where(x => x.Status != BorrowRequestStatus.Overdue
&& x.Status == BorrowRequestStatus.CheckedOut
&& x.DueTime < localDateTimeNow)
.ToList();

foreach (var request in overdueRequests)
{
request.Status = BorrowRequestStatus.Overdue;
}
context.Borrows.UpdateRange(overdueRequests);

await context.SaveChangesAsync(stoppingToken);
await Task.Delay(delay, stoppingToken);
}
}
2 changes: 1 addition & 1 deletion src/Application/Loggings/Queries/GetAllLogsPaginated.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ public async Task<PaginatedList<LogDto>> Handle(Query request, CancellationToken

if (!(request.SearchTerm is null || request.SearchTerm.Trim().Equals(string.Empty)))
{
logs = logs.Where(x => x.Message!.Contains(request.SearchTerm.Trim().ToLower()));
logs = logs.Where(x => x.Message!.ToLower().Contains(request.SearchTerm.Trim().ToLower()));
}

var pageNumber = request.Page is null or <= 0 ? 1 : request.Page;
Expand Down