Remove caching; add previous/next puzzle dates to puzzles
This commit is contained in:
@@ -11,6 +11,11 @@ namespace ConnectionsAPI.Database
|
|||||||
{
|
{
|
||||||
modelBuilder.Entity<Puzzle>()
|
modelBuilder.Entity<Puzzle>()
|
||||||
.HasIndex(x => x.PrintDate).IsUnique();
|
.HasIndex(x => x.PrintDate).IsUnique();
|
||||||
|
modelBuilder.Entity<Puzzle>()
|
||||||
|
.Ignore(x => x.NextPrintDate);
|
||||||
|
modelBuilder.Entity<Puzzle>()
|
||||||
|
.Ignore(x => x.PrevPrintDate);
|
||||||
|
|
||||||
|
|
||||||
base.OnModelCreating(modelBuilder);
|
base.OnModelCreating(modelBuilder);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
namespace ConnectionsAPI.Database.Entities
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
|
namespace ConnectionsAPI.Database.Entities
|
||||||
{
|
{
|
||||||
public class Puzzle
|
public class Puzzle
|
||||||
{
|
{
|
||||||
@@ -36,5 +38,10 @@
|
|||||||
/// The categories associated with this puzzle
|
/// The categories associated with this puzzle
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual ICollection<PuzzleCategory> Categories { get; set; } = [];
|
public virtual ICollection<PuzzleCategory> Categories { get; set; } = [];
|
||||||
|
|
||||||
|
[NotMapped]
|
||||||
|
public string? PrevPrintDate { get; set; }
|
||||||
|
[NotMapped]
|
||||||
|
public string? NextPrintDate { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
76
Database/Repository/PuzzleRepository.cs
Normal file
76
Database/Repository/PuzzleRepository.cs
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
using ConnectionsAPI.Database.Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace ConnectionsAPI.Database.Repository
|
||||||
|
{
|
||||||
|
public class PuzzleRepository(ConnectionsContext _db)
|
||||||
|
{
|
||||||
|
private readonly ConnectionsContext _db = _db;
|
||||||
|
|
||||||
|
public async Task<Puzzle?> GetPuzzleByDateAsync(string printDate, bool includeSolutions = true)
|
||||||
|
{
|
||||||
|
// query for the puzzle
|
||||||
|
var query = _db.Puzzles.AsNoTracking();
|
||||||
|
|
||||||
|
if (includeSolutions)
|
||||||
|
{
|
||||||
|
query = query
|
||||||
|
.Include(x => x.Categories)
|
||||||
|
.ThenInclude(x => x.PuzzleCards);
|
||||||
|
}
|
||||||
|
|
||||||
|
var puzzle = await query.FirstOrDefaultAsync(x => x.PrintDate == printDate);
|
||||||
|
|
||||||
|
// if not found, we're done here
|
||||||
|
if (puzzle == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
await EnhancePuzzleWithDatesAsync(puzzle);
|
||||||
|
|
||||||
|
return puzzle;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<Puzzle>> GetAllPuzzlesAsync(bool includeSolutions = true)
|
||||||
|
{
|
||||||
|
// query all, ordered by print date
|
||||||
|
var query = _db.Puzzles
|
||||||
|
.AsNoTracking();
|
||||||
|
|
||||||
|
if (includeSolutions)
|
||||||
|
{
|
||||||
|
query = query
|
||||||
|
.Include(x => x.Categories)
|
||||||
|
.ThenInclude(x => x.PuzzleCards);
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = (await query.OrderBy(x => x.PrintDate).ToListAsync()) ?? [];
|
||||||
|
|
||||||
|
foreach (var puzzle in result)
|
||||||
|
{
|
||||||
|
await EnhancePuzzleWithDatesAsync(puzzle);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task EnhancePuzzleWithDatesAsync(Puzzle puzzle)
|
||||||
|
{
|
||||||
|
string? previousPuzzleDate = await _db.Puzzles.AsNoTracking()
|
||||||
|
.Where(x => x.PrintDate.CompareTo(puzzle.PrintDate) < 0)
|
||||||
|
.OrderByDescending(x => x.PrintDate)
|
||||||
|
.Select(x => x.PrintDate)
|
||||||
|
.FirstOrDefaultAsync();
|
||||||
|
|
||||||
|
string? nextPuzzleDate = await _db.Puzzles.AsNoTracking()
|
||||||
|
.Where(x => x.PrintDate.CompareTo(puzzle.PrintDate) > 0)
|
||||||
|
.OrderBy(x => x.PrintDate)
|
||||||
|
.Select(x => x.PrintDate)
|
||||||
|
.FirstOrDefaultAsync();
|
||||||
|
|
||||||
|
puzzle.PrevPrintDate = previousPuzzleDate;
|
||||||
|
puzzle.NextPrintDate = nextPuzzleDate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
using ConnectionsAPI.Database;
|
using ConnectionsAPI.Database.Repository;
|
||||||
using ConnectionsAPI.Models;
|
using ConnectionsAPI.Models;
|
||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
using LazyCache;
|
using LazyCache;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace ConnectionsAPI.Features.Puzzle.Get
|
namespace ConnectionsAPI.Features.Puzzle.Get
|
||||||
@@ -22,15 +21,16 @@ namespace ConnectionsAPI.Features.Puzzle.Get
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class GetPuzzleEndpoint(ConnectionsContext db, ILogger<GetPuzzleEndpoint> logger, IAppCache cache) : Endpoint<GetPuzzleEndpointRequest, PuzzleDTO>
|
public class GetPuzzleEndpoint(PuzzleRepository puzzleRepo, ILogger<GetPuzzleEndpoint> logger, IAppCache cache) : Endpoint<GetPuzzleEndpointRequest, PuzzleDTO>
|
||||||
{
|
{
|
||||||
private readonly ConnectionsContext _db = db;
|
private readonly PuzzleRepository _puzzleRepo = puzzleRepo;
|
||||||
private readonly ILogger<GetPuzzleEndpoint> _logger = logger;
|
private readonly ILogger<GetPuzzleEndpoint> _logger = logger;
|
||||||
private readonly IAppCache _cache = cache;
|
private readonly IAppCache _cache = cache;
|
||||||
|
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
Get("/{PuzzleDate}.json");
|
Get("/{PuzzleDate}.json",
|
||||||
|
"/puzzle/{PuzzleDate}");
|
||||||
AllowAnonymous();
|
AllowAnonymous();
|
||||||
DontThrowIfValidationFails();
|
DontThrowIfValidationFails();
|
||||||
}
|
}
|
||||||
@@ -45,39 +45,23 @@ namespace ConnectionsAPI.Features.Puzzle.Get
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get response from cache
|
bool hideSolutions = Query<bool>("hideSolutions", isRequired: false);
|
||||||
var response = await _cache.GetOrAddAsync($"Puzzle:{req.PuzzleDate}",
|
|
||||||
() => { return GetResponseForCache(req.PuzzleDate); },
|
// query for the puzzle
|
||||||
DateTimeOffset.UtcNow.AddMinutes(5));
|
var puzzle = await _puzzleRepo.GetPuzzleByDateAsync(req.PuzzleDate, includeSolutions: !hideSolutions);
|
||||||
|
|
||||||
// if not found, done here
|
// if not found, done here
|
||||||
if (response == null)
|
if (puzzle == null)
|
||||||
{
|
{
|
||||||
await SendNotFoundAsync(ct);
|
await SendNotFoundAsync(ct);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get response from cache
|
||||||
|
var response = PuzzleDTO.FromEntity(puzzle);
|
||||||
|
|
||||||
// done
|
// done
|
||||||
await SendAsync(response, cancellation: ct);
|
await SendAsync(response, cancellation: ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<PuzzleDTO?> GetResponseForCache(string printDate)
|
|
||||||
{
|
|
||||||
// query for the puzzle
|
|
||||||
var puzzle = await _db.Puzzles
|
|
||||||
.Include(x => x.Categories)
|
|
||||||
.ThenInclude(x => x.PuzzleCards)
|
|
||||||
.AsNoTracking()
|
|
||||||
.FirstOrDefaultAsync(x => x.PrintDate == printDate);
|
|
||||||
|
|
||||||
// if not found, we're done here
|
|
||||||
if (puzzle == null)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if found, map
|
|
||||||
return PuzzleDTO.FromEntity(puzzle);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,43 +1,34 @@
|
|||||||
using ConnectionsAPI.Database;
|
using ConnectionsAPI.Database.Repository;
|
||||||
using ConnectionsAPI.Models;
|
using ConnectionsAPI.Models;
|
||||||
using LazyCache;
|
using LazyCache;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace ConnectionsAPI.Features.Puzzle.List
|
namespace ConnectionsAPI.Features.Puzzle.List
|
||||||
{
|
{
|
||||||
public class ListPuzzlesEndpoint(ConnectionsContext db, IAppCache cache) : EndpointWithoutRequest<ICollection<PuzzleDTO>>
|
public class ListPuzzlesEndpoint(PuzzleRepository puzzleRepo, IAppCache cache) : EndpointWithoutRequest<ICollection<PuzzleDTO>>
|
||||||
{
|
{
|
||||||
private readonly ConnectionsContext _db = db;
|
private readonly PuzzleRepository _puzzleRepo = puzzleRepo;
|
||||||
private readonly IAppCache _cache = cache;
|
private readonly IAppCache _cache = cache;
|
||||||
|
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
Get("/all.json");
|
Get("/all.json",
|
||||||
|
"/puzzle/all");
|
||||||
AllowAnonymous();
|
AllowAnonymous();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task HandleAsync(CancellationToken ct)
|
public override async Task HandleAsync(CancellationToken ct)
|
||||||
{
|
{
|
||||||
// get response from cache
|
bool hideSolutions = Query<bool>("hideSolutions", isRequired: false);
|
||||||
var response = await _cache.GetOrAddAsync("Puzzle:All",
|
|
||||||
GetResponseForCache,
|
// query all, ordered by print date
|
||||||
DateTimeOffset.UtcNow.AddMinutes(5));
|
var puzzles = await _puzzleRepo.GetAllPuzzlesAsync(includeSolutions: !hideSolutions);
|
||||||
|
|
||||||
|
// map to response object
|
||||||
|
var response = puzzles.Select(PuzzleDTO.FromEntity).ToList();
|
||||||
|
|
||||||
// done
|
// done
|
||||||
await SendAsync(response, cancellation: ct);
|
await SendAsync(response, cancellation: ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<ICollection<PuzzleDTO>> GetResponseForCache()
|
|
||||||
{
|
|
||||||
// query all, ordered by print date
|
|
||||||
var puzzles = await _db.Puzzles
|
|
||||||
.Include(x => x.Categories)
|
|
||||||
.ThenInclude(x => x.PuzzleCards)
|
|
||||||
.AsNoTracking()
|
|
||||||
.OrderBy(x => x.PrintDate)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
// map to dto
|
|
||||||
return puzzles.Select(PuzzleDTO.FromEntity).ToList();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,11 +8,17 @@
|
|||||||
PuzzleNumber = dbPuzzle.Index,
|
PuzzleNumber = dbPuzzle.Index,
|
||||||
PrintDate = dbPuzzle.PrintDate,
|
PrintDate = dbPuzzle.PrintDate,
|
||||||
Editor = dbPuzzle.EditorName,
|
Editor = dbPuzzle.EditorName,
|
||||||
Categories = dbPuzzle.Categories.OrderBy(x => (int)x.Color).Select(PuzzleCategoryDTO.FromEntity).ToList()
|
|
||||||
|
NextPuzzle = dbPuzzle.NextPrintDate ?? string.Empty,
|
||||||
|
PreviousPuzzle = dbPuzzle.PrevPrintDate ?? string.Empty,
|
||||||
|
|
||||||
|
Categories = dbPuzzle.Categories.OrderBy(x => (int)x.Color).Select(PuzzleCategoryDTO.FromEntity).ToList(),
|
||||||
};
|
};
|
||||||
|
|
||||||
public int PuzzleNumber { get; set; }
|
public int PuzzleNumber { get; set; }
|
||||||
public string PrintDate { get; set; } = string.Empty;
|
public string PrintDate { get; set; } = string.Empty;
|
||||||
|
public string PreviousPuzzle { get; set; } = string.Empty;
|
||||||
|
public string NextPuzzle { get; set; } = string.Empty;
|
||||||
public string Editor { get; set; } = string.Empty;
|
public string Editor { get; set; } = string.Empty;
|
||||||
public ICollection<PuzzleCategoryDTO> Categories { get; set; } = [];
|
public ICollection<PuzzleCategoryDTO> Categories { get; set; } = [];
|
||||||
}
|
}
|
||||||
|
|||||||
19
Program.cs
19
Program.cs
@@ -1,5 +1,6 @@
|
|||||||
using ConnectionsAPI.Config;
|
using ConnectionsAPI.Config;
|
||||||
using ConnectionsAPI.Database;
|
using ConnectionsAPI.Database;
|
||||||
|
using ConnectionsAPI.Database.Repository;
|
||||||
using ConnectionsAPI.Utility;
|
using ConnectionsAPI.Utility;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
@@ -8,6 +9,8 @@ namespace ConnectionsAPI
|
|||||||
{
|
{
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
|
const string CorsPolicyName = "DefaultCorsPolicy";
|
||||||
|
|
||||||
public static async Task Main(string[] args)
|
public static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
@@ -36,8 +39,21 @@ namespace ConnectionsAPI
|
|||||||
|
|
||||||
builder.Services.AddLazyCache();
|
builder.Services.AddLazyCache();
|
||||||
|
|
||||||
|
builder.Services.AddScoped<PuzzleRepository>();
|
||||||
|
|
||||||
builder.Services.AddHostedService<SyncScheduler>();
|
builder.Services.AddHostedService<SyncScheduler>();
|
||||||
|
|
||||||
|
// configure clors
|
||||||
|
builder.Services.AddCors(config =>
|
||||||
|
{
|
||||||
|
config.AddPolicy(CorsPolicyName, policy =>
|
||||||
|
{
|
||||||
|
policy.AllowAnyOrigin();
|
||||||
|
policy.AllowAnyHeader();
|
||||||
|
policy.WithMethods("GET");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
var logger = app.Services.GetRequiredService<ILogger<Program>>();
|
var logger = app.Services.GetRequiredService<ILogger<Program>>();
|
||||||
@@ -88,6 +104,9 @@ namespace ConnectionsAPI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// enable cors
|
||||||
|
app.UseCors(CorsPolicyName);
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,6 @@
|
|||||||
"ConnectionsContext": "Data Source=c:\\tmp\\connections-api\\dev.db;"
|
"ConnectionsContext": "Data Source=c:\\tmp\\connections-api\\dev.db;"
|
||||||
},
|
},
|
||||||
"Sync": {
|
"Sync": {
|
||||||
"RunImmediately": false
|
"RunImmediately": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user