refactor: properly rename connections-related tables; top-level namespaces
This commit is contained in:
@@ -1,76 +1,75 @@
|
||||
using ConnectionsAPI.Database.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ConnectionsAPI.Database.Repository
|
||||
namespace ConnectionsAPI.Database.Repository;
|
||||
|
||||
public class PuzzleRepository(ConnectionsContext _db)
|
||||
{
|
||||
public class PuzzleRepository(ConnectionsContext _db)
|
||||
private readonly ConnectionsContext _db = _db;
|
||||
|
||||
public async Task<ConnectionsPuzzle?> GetPuzzleByDateAsync(string printDate, bool includeSolutions = true)
|
||||
{
|
||||
private readonly ConnectionsContext _db = _db;
|
||||
// query for the puzzle
|
||||
var query = _db.ConnectionsPuzzles.AsNoTracking();
|
||||
|
||||
public async Task<CategoriesPuzzle?> GetPuzzleByDateAsync(string printDate, bool includeSolutions = true)
|
||||
if (includeSolutions)
|
||||
{
|
||||
// query for the puzzle
|
||||
var query = _db.CategoriesPuzzles.AsNoTracking();
|
||||
query = query
|
||||
.Include(x => x.Categories)
|
||||
.ThenInclude(x => x.Cards);
|
||||
}
|
||||
|
||||
if (includeSolutions)
|
||||
{
|
||||
query = query
|
||||
.Include(x => x.Categories)
|
||||
.ThenInclude(x => x.CategoriesPuzzleCards);
|
||||
}
|
||||
var puzzle = await query.FirstOrDefaultAsync(x => x.PrintDate == printDate);
|
||||
|
||||
var puzzle = await query.FirstOrDefaultAsync(x => x.PrintDate == printDate);
|
||||
// if not found, we're done here
|
||||
if (puzzle == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// if not found, we're done here
|
||||
if (puzzle == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
await EnhancePuzzleWithDatesAsync(puzzle);
|
||||
|
||||
return puzzle;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ConnectionsPuzzle>> GetAllPuzzlesAsync(bool includeSolutions = true)
|
||||
{
|
||||
// query all, ordered by print date
|
||||
var query = _db.ConnectionsPuzzles
|
||||
.AsNoTracking();
|
||||
|
||||
if (includeSolutions)
|
||||
{
|
||||
query = query
|
||||
.Include(x => x.Categories)
|
||||
.ThenInclude(x => x.Cards);
|
||||
}
|
||||
|
||||
var result = (await query.OrderBy(x => x.PrintDate).ToListAsync()) ?? [];
|
||||
|
||||
foreach (var puzzle in result)
|
||||
{
|
||||
await EnhancePuzzleWithDatesAsync(puzzle);
|
||||
|
||||
return puzzle;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<CategoriesPuzzle>> GetAllPuzzlesAsync(bool includeSolutions = true)
|
||||
{
|
||||
// query all, ordered by print date
|
||||
var query = _db.CategoriesPuzzles
|
||||
.AsNoTracking();
|
||||
return result;
|
||||
}
|
||||
|
||||
if (includeSolutions)
|
||||
{
|
||||
query = query
|
||||
.Include(x => x.Categories)
|
||||
.ThenInclude(x => x.CategoriesPuzzleCards);
|
||||
}
|
||||
private async Task EnhancePuzzleWithDatesAsync(ConnectionsPuzzle puzzle)
|
||||
{
|
||||
string? previousPuzzleDate = await _db.ConnectionsPuzzles.AsNoTracking()
|
||||
.Where(x => x.PrintDate.CompareTo(puzzle.PrintDate) < 0)
|
||||
.OrderByDescending(x => x.PrintDate)
|
||||
.Select(x => x.PrintDate)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
var result = (await query.OrderBy(x => x.PrintDate).ToListAsync()) ?? [];
|
||||
string? nextPuzzleDate = await _db.ConnectionsPuzzles.AsNoTracking()
|
||||
.Where(x => x.PrintDate.CompareTo(puzzle.PrintDate) > 0)
|
||||
.OrderBy(x => x.PrintDate)
|
||||
.Select(x => x.PrintDate)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
foreach (var puzzle in result)
|
||||
{
|
||||
await EnhancePuzzleWithDatesAsync(puzzle);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task EnhancePuzzleWithDatesAsync(CategoriesPuzzle puzzle)
|
||||
{
|
||||
string? previousPuzzleDate = await _db.CategoriesPuzzles.AsNoTracking()
|
||||
.Where(x => x.PrintDate.CompareTo(puzzle.PrintDate) < 0)
|
||||
.OrderByDescending(x => x.PrintDate)
|
||||
.Select(x => x.PrintDate)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
string? nextPuzzleDate = await _db.CategoriesPuzzles.AsNoTracking()
|
||||
.Where(x => x.PrintDate.CompareTo(puzzle.PrintDate) > 0)
|
||||
.OrderBy(x => x.PrintDate)
|
||||
.Select(x => x.PrintDate)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
puzzle.PrevPrintDate = previousPuzzleDate;
|
||||
puzzle.NextPrintDate = nextPuzzleDate;
|
||||
}
|
||||
puzzle.PrevPrintDate = previousPuzzleDate;
|
||||
puzzle.NextPrintDate = nextPuzzleDate;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user