persistence/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs

52 lines
1.5 KiB
C#

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