73 lines
2.0 KiB
C#
73 lines
2.0 KiB
C#
using Mapster;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Persistence.Database.Model;
|
|
using Persistence.Models;
|
|
using Persistence.Repositories;
|
|
|
|
namespace Persistence.Repository.Repositories
|
|
{
|
|
public class SetpointRepository : ISetpointRepository
|
|
{
|
|
private DbContext db;
|
|
public SetpointRepository(DbContext db)
|
|
{
|
|
this.db = db;
|
|
}
|
|
|
|
protected virtual IQueryable<Setpoint> GetQueryReadOnly() => db.Set<Setpoint>();
|
|
|
|
public async Task<IEnumerable<SetpointValueDto>> GetCurrent(IEnumerable<Guid> setpointKeys, CancellationToken token)
|
|
{
|
|
var query = GetQueryReadOnly();
|
|
var entities = await query
|
|
.Where(e => setpointKeys.Contains(e.Key))
|
|
.ToArrayAsync(token);
|
|
var dtos = entities.Select(e => e.Adapt<SetpointValueDto>());
|
|
|
|
return dtos;
|
|
}
|
|
|
|
public async Task<IEnumerable<SetpointValueDto>> GetHistory(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment, CancellationToken token)
|
|
{
|
|
var query = GetQueryReadOnly();
|
|
var entities = await query
|
|
.Where(e => setpointKeys.Contains(e.Key))
|
|
.ToArrayAsync(token);
|
|
var filteredEntities = entities
|
|
.GroupBy(e => e.Key)
|
|
.Select(e => e.Where(e => e.Created <= historyMoment).Last());
|
|
var dtos = filteredEntities
|
|
.Select(e => e.Adapt<SetpointValueDto>());
|
|
|
|
return dtos;
|
|
}
|
|
|
|
public async Task<Dictionary<Guid, IEnumerable<SetpointLogDto>>> GetLog(IEnumerable<Guid> setpointKeys, CancellationToken token)
|
|
{
|
|
var query = GetQueryReadOnly();
|
|
var entities = await query
|
|
.Where(e => setpointKeys.Contains(e.Key))
|
|
.ToArrayAsync(token);
|
|
var dtos = entities
|
|
.GroupBy(e => e.Key)
|
|
.ToDictionary(e => e.Key, v => v.Select(z => z.Adapt<SetpointLogDto>()));
|
|
|
|
return dtos;
|
|
}
|
|
|
|
public async Task Save(Guid setpointKey, object newValue, int idUser, CancellationToken token)
|
|
{
|
|
var entity = new Setpoint()
|
|
{
|
|
Key = setpointKey,
|
|
Value = newValue,
|
|
IdUser = idUser,
|
|
Created = DateTimeOffset.UtcNow
|
|
};
|
|
|
|
await db.Set<Setpoint>().AddAsync(entity, token);
|
|
await db.SaveChangesAsync(token);
|
|
}
|
|
}
|
|
}
|