using Mapster; using Microsoft.EntityFrameworkCore; using Persistence.Database.Model; using Persistence.Models; using Persistence.Repositories; namespace Persistence.Repository.Repositories; public class TimeSeriesDataRepository : ITimeSeriesDataRepository where TEntity : class, ITimestampedData, new() where TDto : class, ITimeSeriesAbstractDto, new() { private DbContext db; public TimeSeriesDataRepository(DbContext db) { this.db = db; } protected virtual IQueryable GetQueryReadOnly() => this.db.Set(); public async Task> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token) { var query = GetQueryReadOnly(); var entities = await query.ToArrayAsync(token); var dtos = entities.Select(e => e.Adapt()); return dtos; } public async Task GetDatesRangeAsync(CancellationToken token) { var query = GetQueryReadOnly(); var minDate = await query.MinAsync(o => o.Date, token); var maxDate = await query.MaxAsync(o => o.Date, token); return new DatesRangeDto { From = minDate, To = maxDate }; } public async Task> GetGtDate(DateTimeOffset date, CancellationToken token) { var query = this.db.Set().Where(e => e.Date > date); var entities = await query.ToArrayAsync(token); var dtos = entities.Select(e => e.Adapt()); return dtos; } public async Task InsertRange(IEnumerable dtos, CancellationToken token) { var entities = dtos.Select(d => d.Adapt()); await db.Set().AddRangeAsync(entities, token); var result = await db.SaveChangesAsync(token); return result; } }