Files
vigilcare-clinical/VigilCareClinicalAPI/Services/News2Service.cs
T

50 lines
1.5 KiB
C#

using Microsoft.EntityFrameworkCore;
public class News2Service : INews2Service
{
private readonly AppDbContext _db;
public News2Service(AppDbContext db) => _db = db;
public async Task<News2Score?> GetCurrentAsync(Guid encounterId)
{
return await _db.News2Scores
.AsNoTracking()
.Where(s => s.EncounterId == encounterId)
.OrderByDescending(s => s.CalculatedAt)
.FirstOrDefaultAsync();
}
public async Task<CursorPage<News2Score>> GetHistoryAsync(
Guid encounterId, int limit, string? cursorToken)
{
limit = Math.Clamp(limit, 1, 100);
var cursor = News2ScoreCursor.Decode(cursorToken);
var query = _db.News2Scores
.AsNoTracking()
.Where(s => s.EncounterId == encounterId);
if (cursor is not null)
{
query = query.Where(s =>
s.CalculatedAt < cursor.CalculatedAt ||
(s.CalculatedAt == cursor.CalculatedAt && s.Id.CompareTo(cursor.Id) < 0));
}
var items = await query
.OrderByDescending(s => s.CalculatedAt)
.ThenByDescending(s => s.Id)
.Take(limit + 1)
.ToListAsync();
var hasMore = items.Count > limit;
if (hasMore) items.RemoveAt(limit);
var nextCursor = hasMore
? new News2ScoreCursor(items[^1].CalculatedAt, items[^1].Id).Encode()
: null;
return new CursorPage<News2Score>(items, nextCursor, hasMore);
}
}