Метод добавления changeLog

This commit is contained in:
Olga Nemt 2024-11-26 14:19:16 +05:00
parent 44730bb66e
commit c5645df2e6
13 changed files with 127 additions and 75 deletions

View File

@ -21,11 +21,11 @@ public class ChangeLogController : ControllerBase, IChangeLogApi
[HttpPost]
public async Task<ActionResult<int>> Add(
[FromRoute] Guid idDiscriminator,
[FromBody]IDictionary<string, object> dtos,
ChangeLogDto dto,
CancellationToken token)
{
var userId = User.GetUserId<Guid>();
var result = await repository.InsertRange(userId, idDiscriminator, [dtos], token);
var result = await repository.InsertRange(userId, idDiscriminator, [dto], token);
return Ok(result);
}
@ -54,7 +54,8 @@ public class ChangeLogController : ControllerBase, IChangeLogApi
throw new NotImplementedException();
}
public Task<ActionResult<IEnumerable<ChangeLogDto<IDictionary<string, object>>>>> GetChangeLogForDate(DateTimeOffset historyMoment, CancellationToken token)
[HttpGet("history")]
public Task<ActionResult<IEnumerable<ChangeLogDto>>> GetChangeLogForDate(DateTimeOffset historyMoment, CancellationToken token)
{
throw new NotImplementedException();
}

View File

@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Npgsql;
namespace Persistence.Database.Model;
@ -11,7 +12,11 @@ public static class DependencyInjection
string connectionStringName = "DefaultConnection";
services.AddDbContext<PersistenceDbContext>(options =>
options.UseNpgsql(configuration.GetConnectionString(connectionStringName)));
{
var dataSourceBuilder = new NpgsqlDataSourceBuilder(configuration.GetConnectionString(connectionStringName));
dataSourceBuilder.EnableDynamicJson();
options.UseNpgsql(dataSourceBuilder.Build());
});
services.AddScoped<DbContext>(provider => provider.GetRequiredService<PersistenceDbContext>());

View File

@ -1,5 +1,6 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
@ -12,8 +13,8 @@ using Persistence.Database.Model;
namespace Persistence.Database.Postgres.Migrations
{
[DbContext(typeof(PersistenceDbContext))]
[Migration("20241122124437_AddChangeLog")]
partial class AddChangeLog
[Migration("20241126071115_Add_ChangeLog")]
partial class Add_ChangeLog
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -38,23 +39,39 @@ namespace Persistence.Database.Postgres.Migrations
.HasColumnType("timestamp with time zone")
.HasColumnName("Creation");
b.Property<int>("IdAuthor")
.HasColumnType("integer")
b.Property<double>("DepthEnd")
.HasColumnType("double precision")
.HasColumnName("DepthEnd");
b.Property<double>("DepthStart")
.HasColumnType("double precision")
.HasColumnName("DepthStart");
b.Property<Guid>("IdAuthor")
.HasColumnType("uuid")
.HasColumnName("IdAuthor");
b.Property<int?>("IdEditor")
.HasColumnType("integer")
b.Property<Guid>("IdDiscriminator")
.HasColumnType("uuid")
.HasColumnName("IdDiscriminator");
b.Property<Guid?>("IdEditor")
.HasColumnType("uuid")
.HasColumnName("IdEditor");
b.Property<int?>("IdNext")
.HasColumnType("integer")
b.Property<Guid?>("IdNext")
.HasColumnType("uuid")
.HasColumnName("IdNext");
b.Property<Guid>("IdSection")
.HasColumnType("uuid")
.HasColumnName("IdSection");
b.Property<DateTimeOffset?>("Obsolete")
.HasColumnType("timestamp with time zone")
.HasColumnName("Obsolete");
b.Property<object>("Value")
b.Property<IDictionary<string, object>>("Value")
.IsRequired()
.HasColumnType("jsonb")
.HasColumnName("Value");

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
@ -6,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Persistence.Database.Postgres.Migrations
{
/// <inheritdoc />
public partial class AddChangeLog : Migration
public partial class Add_ChangeLog : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
@ -16,12 +17,16 @@ namespace Persistence.Database.Postgres.Migrations
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
IdAuthor = table.Column<int>(type: "integer", nullable: false),
IdEditor = table.Column<int>(type: "integer", nullable: true),
IdDiscriminator = table.Column<Guid>(type: "uuid", nullable: false),
IdAuthor = table.Column<Guid>(type: "uuid", nullable: false),
IdEditor = table.Column<Guid>(type: "uuid", nullable: true),
Creation = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
Obsolete = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
IdNext = table.Column<int>(type: "integer", nullable: true),
Value = table.Column<object>(type: "jsonb", nullable: false)
IdNext = table.Column<Guid>(type: "uuid", nullable: true),
DepthStart = table.Column<double>(type: "double precision", nullable: false),
DepthEnd = table.Column<double>(type: "double precision", nullable: false),
IdSection = table.Column<Guid>(type: "uuid", nullable: false),
Value = table.Column<IDictionary<string, object>>(type: "jsonb", nullable: false)
},
constraints: table =>
{

View File

@ -1,5 +1,6 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
@ -35,23 +36,39 @@ namespace Persistence.Database.Postgres.Migrations
.HasColumnType("timestamp with time zone")
.HasColumnName("Creation");
b.Property<int>("IdAuthor")
.HasColumnType("integer")
b.Property<double>("DepthEnd")
.HasColumnType("double precision")
.HasColumnName("DepthEnd");
b.Property<double>("DepthStart")
.HasColumnType("double precision")
.HasColumnName("DepthStart");
b.Property<Guid>("IdAuthor")
.HasColumnType("uuid")
.HasColumnName("IdAuthor");
b.Property<int?>("IdEditor")
.HasColumnType("integer")
b.Property<Guid>("IdDiscriminator")
.HasColumnType("uuid")
.HasColumnName("IdDiscriminator");
b.Property<Guid?>("IdEditor")
.HasColumnType("uuid")
.HasColumnName("IdEditor");
b.Property<int?>("IdNext")
.HasColumnType("integer")
b.Property<Guid?>("IdNext")
.HasColumnType("uuid")
.HasColumnName("IdNext");
b.Property<Guid>("IdSection")
.HasColumnType("uuid")
.HasColumnName("IdSection");
b.Property<DateTimeOffset?>("Obsolete")
.HasColumnType("timestamp with time zone")
.HasColumnName("Obsolete");
b.Property<object>("Value")
b.Property<IDictionary<string, object>>("Value")
.IsRequired()
.HasColumnType("jsonb")
.HasColumnName("Value");

View File

@ -3,3 +3,9 @@
dotnet ef migrations add <MigrationName> --project Persistence.Database.Postgres
```
## Откатить миграцию
```
dotnet ef migrations remove --project Persistence.Database.Postgres
```
Удаляется последняя созданная миграция.

View File

@ -51,6 +51,24 @@ public class ChangeLog : IChangeLog
[Column("IdNext")]
public Guid? IdNext { get; set; }
/// <summary>
/// Глубина забоя на дату начала интервала
/// </summary>
[Column("DepthStart")]
public double DepthStart { get; set; }
/// <summary>
/// Глубина забоя на дату окончания интервала
/// </summary>
[Column("DepthEnd")]
public double DepthEnd { get; set; }
/// <summary>
/// Ключ секции
/// </summary>
[Column("IdSection")]
public Guid IdSection { get; set; }
/// <summary>
/// Значение
/// </summary>

View File

@ -1,29 +0,0 @@

using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace Persistence.Database.Model;
/// <summary>
/// Часть записи, описывающая изменение данных, содержащие начальную и конечную глубину, а также секцию
/// </summary>
public class ChangeLogWithWellDepthAndSectionId : ChangeLog
{
/// <summary>
/// Глубина забоя на дату начала интервала
/// </summary>
[Column("DepthStart")]
public double? DepthStart { get; set; }
/// <summary>
/// Глубина забоя на дату окончания интервала
/// </summary>
[Column("DepthEnd")]
public double? DepthEnd { get; set; }
/// <summary>
/// Ключ секции
/// </summary>
[Column("IdSection")]
public Guid IdSection { get; set; }
}

View File

@ -4,7 +4,7 @@ using System.ComponentModel.DataAnnotations.Schema;
namespace Persistence.Database.Model;
/// <summary>
/// Часть записи описывающая изменение
/// Часть записи, описывающая изменение
/// </summary>
public interface IChangeLog
{

View File

@ -24,7 +24,7 @@ public class ChangeLogRepository : IChangeLogRepository
throw new NotImplementedException();
}
public Task<IEnumerable<ChangeLogDto<IDictionary<string, object>>>> GetChangeLogForDate(DateTimeOffset? updateFrom, CancellationToken token)
public Task<IEnumerable<ChangeLogDto>> GetChangeLogForDate(DateTimeOffset? updateFrom, CancellationToken token)
{
throw new NotImplementedException();
}
@ -44,7 +44,7 @@ public class ChangeLogRepository : IChangeLogRepository
throw new NotImplementedException();
}
public Task<int> InsertRange(Guid idUser, Guid idDiscriminator, IEnumerable<IDictionary<string, object>> dtos, CancellationToken token)
public Task<int> InsertRange(Guid idUser, Guid idDiscriminator, IEnumerable<ChangeLogDto> dtos, CancellationToken token)
{
var entities = new List<ChangeLog>();
foreach (var dto in dtos)
@ -54,8 +54,11 @@ public class ChangeLogRepository : IChangeLogRepository
IdAuthor = idUser,
IdDiscriminator = idDiscriminator,
IdEditor = idUser,
Value = dto,
Creation = DateTimeOffset.UtcNow
Value = dto.Value,
Creation = DateTimeOffset.UtcNow,
IdSection = dto.IdSection,
DepthStart = dto.DepthStart,
DepthEnd = dto.DepthEnd,
};
entity.Id = idUser;

View File

@ -21,7 +21,7 @@ public interface IChangeLogApi
/// <param name="historyMoment"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<ActionResult<IEnumerable<ChangeLogDto<IDictionary<string, object>>>>> GetChangeLogForDate(DateTimeOffset historyMoment, CancellationToken token);
Task<ActionResult<IEnumerable<ChangeLogDto>>> GetChangeLogForDate(DateTimeOffset historyMoment, CancellationToken token);
/// <summary>
/// Добавить одну запись
@ -29,7 +29,7 @@ public interface IChangeLogApi
/// <param name="dto"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<ActionResult<int>> Add(Guid idDiscriminator, IDictionary<string, object> dto, CancellationToken token);
Task<ActionResult<int>> Add(Guid idDiscriminator, ChangeLogDto dto, CancellationToken token);
/// <summary>
/// Добавить несколько записей

View File

@ -1,20 +1,14 @@
namespace Persistence.Models;
/// <summary>
/// Часть записи описывающая изменение
/// </summary>
public class ChangeLogDto<T> : IChangeLogDto
where T: IDictionary<string, object>
public class ChangeLogDto
{
public ChangeLogDto()
{
}
/// <summary>
/// Запись
/// </summary>
public required T Item { get; set; }
/// <summary>
///
@ -46,8 +40,23 @@ public class ChangeLogDto<T> : IChangeLogDto
/// </summary>
public Guid? IdNext { get; set; }
/// <summary>
/// Глубина забоя на дату начала интервала
/// </summary>
public double DepthStart { get; set; }
/// <summary>
/// Глубина забоя на дату окончания интервала
/// </summary>
public double DepthEnd { get; set; }
/// <summary>
/// Ключ секции
/// </summary>
public Guid IdSection { get; set; }
/// <summary>
/// Объект записи
/// </summary>
public required object Value { get; set; }
public required IDictionary<string, object> Value { get; set; }
}

View File

@ -16,7 +16,7 @@ public interface IChangeLogRepository //: ISyncRepository<TDto>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> InsertRange(Guid idUser, Guid idDiscriminator, IEnumerable<IDictionary<string, object>> dtos, CancellationToken token);
Task<int> InsertRange(Guid idUser, Guid idDiscriminator, IEnumerable<ChangeLogDto> dtos, CancellationToken token);
/// <summary>
/// Редактирование записей
@ -76,7 +76,7 @@ public interface IChangeLogRepository //: ISyncRepository<TDto>
/// <param name="updateFrom"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<ChangeLogDto<IDictionary<string, object>>>> GetChangeLogForDate(DateTimeOffset? updateFrom, CancellationToken token);
Task<IEnumerable<ChangeLogDto>> GetChangeLogForDate(DateTimeOffset? updateFrom, CancellationToken token);
/// <summary>
/// Получение текущих сейчас записей по параметрам