Olga Nemt
828864c112
- Persistence.API - Persistence.Database - Persistence.Repository - Persistence.IntegrationTests
51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
using Mapster;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Persistence.Models;
|
|
using Persistence.Repositories;
|
|
using Persistence.Database.Model;
|
|
using Persistence.Repository.Data;
|
|
|
|
namespace Persistence.Repository.Repositories;
|
|
public abstract 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() => 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;
|
|
}
|
|
}
|