Setpoint API #1
48
Persistence.API/Controllers/SetpointController.cs
Normal file
48
Persistence.API/Controllers/SetpointController.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Persistence.Models;
|
||||
using Persistence.Repositories;
|
||||
|
||||
namespace Persistence.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class SetpointController : ControllerBase, ISetpointApi
|
||||
{
|
||||
private readonly ISetpointRepository setpointRepository;
|
||||
|
||||
public SetpointController(ISetpointRepository setpointRepository)
|
||||
{
|
||||
this.setpointRepository = setpointRepository;
|
||||
}
|
||||
|
||||
[HttpPost("current")]
|
||||
public Task<ActionResult<IEnumerable<SetpointValueDto>>> GetCurrentAsync(IEnumerable<Guid> setpointKeys, CancellationToken token)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
[HttpPost("history")]
|
||||
public async Task<ActionResult<IEnumerable<SetpointValueDto>>> GetHistoryAsync(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment, CancellationToken token)
|
||||
{
|
||||
var result = await setpointRepository.GetHistoryAsync(setpointKeys, historyMoment, token);
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPost("log")]
|
||||
public async Task<ActionResult<Dictionary<Guid, IEnumerable<SetpointLogDto>>>> GetLogAsync([FromBody] IEnumerable<Guid> setpointKeys, CancellationToken token)
|
||||
{
|
||||
var result = await setpointRepository.GetLogAsync(setpointKeys, token);
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPost("save")]
|
||||
public async Task<ActionResult<int>> SaveAsync(Guid setpointKey, object newValue, CancellationToken token)
|
||||
{
|
||||
var result = await setpointRepository.SaveAsync(setpointKey, newValue, token);
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
}
|
@ -11,6 +11,7 @@ namespace Persistence.Database.Model;
|
||||
public interface IPersistenceDbContext : IDisposable
|
||||
{
|
||||
DbSet<DataSaub> DataSaub { get; }
|
||||
DatabaseFacade Database { get; }
|
||||
DbSet<Setpoint> Setpoint { get; }
|
||||
DatabaseFacade Database { get; }
|
||||
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
|
10
Persistence.Database/Model/ISetpointData.cs
Normal file
10
Persistence.Database/Model/ISetpointData.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace Persistence.Database.Model
|
||||
{
|
||||
public interface ISetpointData
|
||||
{
|
||||
public Guid Key { get; set; }
|
||||
public object Value { get; set; }
|
||||
public DateTimeOffset Created { get; set; }
|
||||
public int IdUser { get; set; }
|
||||
}
|
||||
}
|
@ -1,17 +1,16 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Metrics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Persistence.Database.Model;
|
||||
public partial class PersistenceDbContext : DbContext, IPersistenceDbContext
|
||||
{
|
||||
public DbSet<DataSaub> DataSaub => Set<DataSaub>();
|
||||
|
||||
public DbSet<Setpoint> Setpoint => Set<Setpoint>();
|
||||
|
||||
public PersistenceDbContext()
|
||||
{
|
||||
}
|
||||
|
||||
public PersistenceDbContext(DbContextOptions<PersistenceDbContext> options)
|
||||
{
|
||||
}
|
||||
|
22
Persistence.Database/Model/Setpoint.cs
Normal file
22
Persistence.Database/Model/Setpoint.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
|
||||
namespace Persistence.Database.Model
|
||||
{
|
||||
[PrimaryKey(nameof(Key), nameof(Created))]
|
||||
public class Setpoint : ISetpointData
|
||||
{
|
||||
[Comment("Ключ")]
|
||||
public Guid Key { get; set; }
|
||||
|
||||
[Column(TypeName = "jsonb"), Comment("Значение уставки")]
|
||||
public required object Value { get; set; }
|
||||
|
||||
[Comment("Дата изменения уставки")]
|
||||
public DateTimeOffset Created { get; set; }
|
||||
|
||||
[Comment("Id автора последнего изменения")]
|
||||
public int IdUser { get; set; }
|
||||
}
|
||||
}
|
18
Persistence.Database/Model/SetpointDictionary.cs
Normal file
18
Persistence.Database/Model/SetpointDictionary.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
|
||||
namespace Persistence.Database.Model
|
||||
{
|
||||
public class SetpointDictionary
|
||||
{
|
||||
[Key, Comment("Ключ")]
|
||||
public Guid Key { get; set; }
|
||||
|
||||
[Comment("Наименование")]
|
||||
public required string Name { get; set; }
|
||||
|
||||
[Comment("Описание")]
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
}
|
10
Persistence.Repository/Data/SetpointDto.cs
Normal file
10
Persistence.Repository/Data/SetpointDto.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace Persistence.Repository.Data
|
||||
{
|
||||
public class SetpointDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public required object Value { get; set; }
|
||||
public DateTimeOffset Edit { get; set; }
|
||||
public int IdUser { get; set; }
|
||||
}
|
||||
}
|
@ -21,9 +21,10 @@ public static class DependencyInjection
|
||||
services.AddDbContext<PersistenceDbContext>(options =>
|
||||
options.UseNpgsql(configuration.GetConnectionString(connectionStringName)));
|
||||
|
||||
services.AddScoped<IPersistenceDbContext>(provider => provider.GetRequiredService<PersistenceDbContext>());
|
||||
services.AddScoped<DbContext>(provider => provider.GetRequiredService<PersistenceDbContext>());
|
||||
|
||||
services.AddTransient<ITimeSeriesDataRepository<DataSaubDto>, TimeSeriesDataRepository<DataSaub, DataSaubDto>>();
|
||||
services.AddTransient<ISetpointRepository, SetpointRepository>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
70
Persistence.Repository/Repositories/SetpointRepository.cs
Normal file
70
Persistence.Repository/Repositories/SetpointRepository.cs
Normal file
@ -0,0 +1,70 @@
|
||||
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>> GetHistoryAsync(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment, CancellationToken token)
|
||||
{
|
||||
var query = GetQueryReadOnly();
|
||||
var entities = await query
|
||||
.Where(e => setpointKeys.Contains(e.Key) && e.Created.Date == historyMoment.Date)
|
||||
.ToArrayAsync(token);
|
||||
var dtos = entities.Select(e => e.Adapt<SetpointValueDto>());
|
||||
|
||||
return dtos;
|
||||
}
|
||||
|
||||
public async Task<Dictionary<Guid, IEnumerable<SetpointLogDto>>> GetLogAsync(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)
|
||||
.Select(e => new KeyValuePair<Guid, IEnumerable<SetpointLogDto>>(
|
||||
e.Key,
|
||||
e.Select(s => s.Adapt<SetpointLogDto>())
|
||||
)).ToDictionary();
|
||||
|
||||
return dtos;
|
||||
}
|
||||
|
||||
public async Task<int> SaveAsync(Guid setpointKey, object newValue, CancellationToken token)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
var entity = new Setpoint()
|
||||
{
|
||||
Key = setpointKey,
|
||||
Value = newValue,
|
||||
IdUser = 0, // ToDo: откуда тянуть?
|
||||
Created = DateTimeOffset.Now.ToUniversalTime()
|
||||
};
|
||||
|
||||
await db.Set<Setpoint>().AddAsync(entity, token);
|
||||
var result = await db.SaveChangesAsync(token);
|
||||
|
||||
return result;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
var t = ex.Message;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,6 @@ using System.Threading.Tasks;
|
||||
namespace Persistence.Models;
|
||||
public class SetpointLogDto : SetpointValueDto
|
||||
{
|
||||
public DateTimeOffset Edit { get; set; }
|
||||
public DateTimeOffset Created { get; set; }
|
||||
public int IdUser { get; set; }
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ namespace Persistence.Models;
|
||||
|
||||
public class SetpointValueDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public object Value { get; set; }
|
||||
public Guid Key { get; set; }
|
||||
public required object Value { get; set; }
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,4 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Persistence.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Persistence.Models;
|
||||
|
||||
namespace Persistence.Repositories;
|
||||
|
||||
@ -17,19 +11,19 @@ public interface ISetpointRepository
|
||||
/// <summary>
|
||||
/// Получить значения уставок за определенный момент времени
|
||||
/// </summary>
|
||||
/// <param name="setpoitKeys"></param>
|
||||
/// <param name="setpointKeys"></param>
|
||||
/// <param name="historyMoment">дата, на которую получаем данные</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<SetpointValueDto>> GetHistoryAsync(IEnumerable<Guid> setpoitKeys, DateTimeOffset historyMoment, CancellationToken token);
|
||||
Task<IEnumerable<SetpointValueDto>> GetHistoryAsync(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Получить историю изменений значений уставок
|
||||
/// </summary>
|
||||
/// <param name="setpoitKeys"></param>
|
||||
/// <param name="setpointKeys"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<Dictionary<Guid, IEnumerable<SetpointLogDto>>> GetLogAsync(IEnumerable<Guid> setpoitKeys, CancellationToken token);
|
||||
Task<Dictionary<Guid, IEnumerable<SetpointLogDto>>> GetLogAsync(IEnumerable<Guid> setpointKeys, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Метод сохранения уставки
|
||||
@ -41,5 +35,5 @@ public interface ISetpointRepository
|
||||
/// <returns></returns>
|
||||
/// to do
|
||||
/// id User учесть в соответствующем методе репозитория
|
||||
Task<int> SaveAsync(Guid setpointKey, int idUser, object newValue, CancellationToken token);
|
||||
Task<int> SaveAsync(Guid setpointKey, object newValue, CancellationToken token);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user
Лучше так:
var dtos = entities
.GroupBy(e => e.Key)
.ToDictionary(e => e.Key, v => v.Select(z => z.Adapt()))