ChangeLog репозиторий

This commit is contained in:
Olga Nemt 2024-11-27 17:59:37 +05:00
parent 5094f4556d
commit a3d90e05f7
9 changed files with 290 additions and 98 deletions

View File

@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Persistence.Database.Model;
using Persistence.Models;
using Persistence.Repositories;
@ -20,8 +19,8 @@ public class ChangeLogController : ControllerBase, IChangeLogApi
[HttpPost]
public async Task<ActionResult<int>> Add(
[FromRoute] Guid idDiscriminator,
ChangeLogDto dto,
Guid idDiscriminator,
[FromBody] DataWithWellDepthAndSectionDto dto,
CancellationToken token)
{
var userId = User.GetUserId<Guid>();
@ -31,50 +30,88 @@ public class ChangeLogController : ControllerBase, IChangeLogApi
}
[HttpPost("range")]
public Task<ActionResult<int>> AddRange(IEnumerable<IDictionary<string, object>> dtos, CancellationToken token)
public async Task<ActionResult<int>> AddRange(
Guid idDiscriminator,
[FromBody] IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token)
{
throw new NotImplementedException();
var userId = User.GetUserId<Guid>();
var result = await repository.InsertRange(userId, idDiscriminator, dtos, token);
return Ok(result);
}
[HttpDelete]
public Task<ActionResult<int>> Delete(int id, CancellationToken token)
public async Task<ActionResult<int>> Delete(Guid id, CancellationToken token)
{
throw new NotImplementedException();
var userId = User.GetUserId<Guid>();
var result = await repository.MarkAsDeleted(userId, [id], token);
return Ok(result);
}
[HttpDelete("range")]
public Task<ActionResult<int>> DeleteRange(IEnumerable<int> ids, CancellationToken token)
public async Task<ActionResult<int>> DeleteRange(IEnumerable<Guid> ids, CancellationToken token)
{
throw new NotImplementedException();
var userId = User.GetUserId<Guid>();
var result = await repository.MarkAsDeleted(userId, ids, token);
return Ok(result);
}
[HttpGet]
public Task<ActionResult<IEnumerable<IDictionary<string, object>>>> GetChangeLogCurrent(CancellationToken token)
public async Task<ActionResult<IEnumerable<DataWithWellDepthAndSectionDto>>> GetCurrent(
Guid idDiscriminator,
CancellationToken token)
{
throw new NotImplementedException();
var date = new DateTimeOffset(3000, 1, 1, 0, 0, 0, TimeSpan.Zero);
var result = await repository.GetByDate(idDiscriminator, date, token);
return Ok(result);
}
[HttpGet("moment")]
public async Task<ActionResult<IEnumerable<DataWithWellDepthAndSectionDto>>> GetByDate(
Guid idDiscriminator,
DateTimeOffset moment,
CancellationToken token)
{
var result = await repository.GetByDate(idDiscriminator, moment, token);
return Ok(result);
}
[HttpGet("history")]
public Task<ActionResult<IEnumerable<ChangeLogDto>>> GetChangeLogForDate(DateTimeOffset historyMoment, CancellationToken token)
public async Task<ActionResult<IEnumerable<ChangeLogDto>>> GetChangeLogForDate(Guid idDiscriminator, DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token)
{
throw new NotImplementedException();
var result = await repository.GetChangeLogForDate(idDiscriminator, dateBegin, dateEnd, token);
return Ok(result);
}
//[HttpGet("history")]
//public Task<ActionResult<IEnumerable<ChangeLogDto<IDictionary<string, object>>>>> GetChangeLogForDate(DateTimeOffset historyMoment, CancellationToken token)
//{
// throw new NotImplementedException();
//}
[HttpPut]
public Task<ActionResult<int>> Update(IDictionary<string, object> dto, CancellationToken token)
public async Task<ActionResult<int>> Update(
Guid idDiscriminator,
DataWithWellDepthAndSectionDto dto,
CancellationToken token)
{
throw new NotImplementedException();
var userId = User.GetUserId<Guid>();
var result = await repository.UpdateRange(userId, idDiscriminator, [dto], token);
return Ok(result);
}
[HttpPut("range")]
public Task<ActionResult<int>> UpdateRange(IEnumerable<IDictionary<string, object>> dtos, CancellationToken token)
public async Task<ActionResult<int>> UpdateRange(
Guid idDiscriminator,
IEnumerable<DataWithWellDepthAndSectionDto> dtos,
CancellationToken token)
{
throw new NotImplementedException();
var userId = User.GetUserId<Guid>();
var result = await repository.UpdateRange(userId, idDiscriminator, dtos, token);
return Ok(result);
}
}

View File

@ -8,7 +8,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.11" />
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.2.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

View File

@ -22,6 +22,7 @@ public class Startup
services.AddSwagger(Configuration);
services.AddInfrastructure();
services.AddPersistenceDbContext(Configuration);
services.AddAuthorization();
services.AddJWTAuthentication(Configuration);
}
@ -38,6 +39,9 @@ public class Startup
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();

View File

@ -14,24 +14,91 @@ public class ChangeLogRepository : IChangeLogRepository
this.db = db;
}
public Task<int> Clear(int idUser, CancellationToken token)
public async Task<int> Clear(Guid idUser, Guid idDiscriminator, CancellationToken token)
{
throw new NotImplementedException();
var updateTime = DateTimeOffset.UtcNow;
var dbSet = db.Set<ChangeLog>();
var query = dbSet
.Where(s => s.IdDiscriminator == idDiscriminator)
.Where(e => e.Obsolete == null);
var entitiesToDelete = await query.ToArrayAsync(token);
foreach (var entity in entitiesToDelete)
{
entity.Obsolete = updateTime;
entity.IdEditor = idUser;
}
public Task<int> ClearAndInsertRange(int idUser, IEnumerable<IDictionary<string, object>> dtos, CancellationToken token)
{
throw new NotImplementedException();
return await db.SaveChangesAsync(token);
}
public Task<IEnumerable<ChangeLogDto>> GetChangeLogForDate(DateTimeOffset? updateFrom, CancellationToken token)
public async Task<int> ClearAndInsertRange(Guid idUser, Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token)
{
throw new NotImplementedException();
var result = 0;
using var transaction = await db.Database.BeginTransactionAsync(token);
try
{
result += await Clear(idUser, idDiscriminator, token);
result += await InsertRange(idUser, idDiscriminator, dtos, token);
await transaction.CommitAsync(token);
return result;
}
catch
{
await transaction.RollbackAsync(token);
throw;
}
}
public Task<IEnumerable<IDictionary<string, object>>> GetCurrent(DateTimeOffset moment, CancellationToken token)
public async Task<IEnumerable<ChangeLogDto>> GetChangeLogForDate(Guid idDiscriminator, DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token)
{
throw new NotImplementedException();
var dbSet = db.Set<ChangeLog>();
var query = dbSet.Where(s => s.IdDiscriminator == idDiscriminator);
var min = dateBegin;
var max = dateEnd;
var createdQuery = query.Where(e => e.Creation >= min && e.Creation <= max);
var editedQuery = query.Where(e => e.Obsolete != null && e.Obsolete >= min && e.Obsolete <= max);
query = createdQuery.Union(editedQuery);
var entities = await query.ToListAsync(token);
var dtos = entities.Select(e => new ChangeLogDto
{
Creation = e.Creation,
IdAuthor = e.IdAuthor,
IdEditor = e.IdEditor,
IdNext = e.IdNext,
Obsolete = e.Obsolete,
Value = new DataWithWellDepthAndSectionDto()
{
Value = e.Value,
IdSection = e.IdSection,
Id = e.Id,
DepthEnd = e.DepthEnd,
DepthStart = e.DepthStart
}
});
return dtos;
}
public async Task<IEnumerable<DataWithWellDepthAndSectionDto>> GetByDate(Guid idDiscriminator, DateTimeOffset momentUtc, CancellationToken token)
{
var dbSet = db.Set<ChangeLog>();
var dbQuery = dbSet
.Where(s => s.IdDiscriminator == idDiscriminator)
.Where(e => e.Creation <= momentUtc)
.Where(e => e.Obsolete == null || e.Obsolete >= momentUtc);
var entities = await dbQuery.ToArrayAsync(token);
var dtos = entities.Select(e => e.Adapt<DataWithWellDepthAndSectionDto>());
return dtos;
}
public Task<IEnumerable<DateOnly>> GetDatesChange(CancellationToken token)
@ -44,36 +111,54 @@ public class ChangeLogRepository : IChangeLogRepository
throw new NotImplementedException();
}
public Task<int> InsertRange(Guid idUser, Guid idDiscriminator, IEnumerable<ChangeLogDto> dtos, CancellationToken token)
public Task<int> InsertRange(Guid idUser, Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token)
{
var entities = new List<ChangeLog>();
foreach (var dto in dtos)
{
var entity = new ChangeLog()
{
IdAuthor = idUser,
IdDiscriminator = idDiscriminator,
IdEditor = idUser,
Value = dto.Value,
Creation = DateTimeOffset.UtcNow,
IdSection = dto.IdSection,
DepthStart = dto.DepthStart,
DepthEnd = dto.DepthEnd,
};
entity.Id = idUser;
entities.Add(entity);
var entity = CreateEntityFromDto(idUser, idDiscriminator, dto);
db.Set<ChangeLog>().Add(entity);
}
db.Set<ChangeLog>().AddRange(entities);
var result = db.SaveChangesAsync(token);
return result;
}
public Task<int> MarkAsDeleted(int idUser, IEnumerable<int> ids, CancellationToken token)
private ChangeLog CreateEntityFromDto(Guid idUser, Guid idDiscriminator, DataWithWellDepthAndSectionDto dto)
{
throw new NotImplementedException();
var entity = new ChangeLog()
{
Id = default,
Creation = DateTimeOffset.UtcNow,
IdAuthor = idUser,
IdDiscriminator = idDiscriminator,
IdEditor = idUser,
Value = dto.Value,
IdSection = dto.IdSection,
DepthStart = dto.DepthStart,
DepthEnd = dto.DepthEnd,
};
return entity;
}
public async Task<int> MarkAsDeleted(Guid idUser, IEnumerable<Guid> ids, CancellationToken token)
{
var result = 0;
var dbSet = db.Set<ChangeLog>();
var updatedEntity = dbSet
.Where(s => ids.Contains(s.Id))
.FirstOrDefault();
if (updatedEntity != null)
{
updatedEntity.Obsolete = DateTimeOffset.UtcNow;
result = await db.SaveChangesAsync(token);
}
return result;
}
public Task<int> UpdateOrInsertRange(int idUser, IEnumerable<IDictionary<string, object>> dtos, CancellationToken token)
@ -81,8 +166,29 @@ public class ChangeLogRepository : IChangeLogRepository
throw new NotImplementedException();
}
public Task<int> UpdateRange(int idUser, IEnumerable<IDictionary<string, object>> dtos, CancellationToken token)
public async Task<int> UpdateRange(Guid idUser, Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token)
{
throw new NotImplementedException();
var dbSet = db.Set<ChangeLog>();
var updatedIds = dtos.Select(d => d.Id);
var updatedEntities = dbSet
.Where(s => updatedIds.Contains(s.Id))
.ToDictionary(s => s.Id);
//todo
foreach (var dto in dtos)
{
var newEntity = CreateEntityFromDto(idUser, idDiscriminator, dto);
dbSet.Add(newEntity);
var updatedEntity = updatedEntities.GetValueOrDefault(dto.Id)!;
updatedEntity.IdNext = newEntity.Id;
updatedEntity.Obsolete = DateTimeOffset.UtcNow;
updatedEntity.IdEditor = idUser;
}
var result = await db.SaveChangesAsync(token);
return result;
}
}

View File

@ -9,51 +9,67 @@ namespace Persistence.API;
public interface IChangeLogApi
{
/// <summary>
/// Получение исторических данных на текущую дату
/// Получение данных на текущую дату
/// </summary>
/// <param name="idDiscriminator"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<ActionResult<IEnumerable<IDictionary<string, object>>>> GetChangeLogCurrent(CancellationToken token);
Task<ActionResult<IEnumerable<DataWithWellDepthAndSectionDto>>> GetCurrent(Guid idDiscriminator, CancellationToken token);
/// <summary>
/// Получение исторических данных на определенную дату
/// Получение данных на определенную дату
/// </summary>
/// <param name="historyMoment"></param>
/// <param name="idDiscriminator"></param>
/// <param name="moment"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<ActionResult<IEnumerable<ChangeLogDto>>> GetChangeLogForDate(DateTimeOffset historyMoment, CancellationToken token);
Task<ActionResult<IEnumerable<DataWithWellDepthAndSectionDto>>> GetByDate(Guid idDiscriminator, DateTimeOffset moment, CancellationToken token);
/// <summary>
/// Получение исторических данных за определенный период времени
/// </summary>
/// <param name="idDiscriminator"></param>
/// <param name="dateBegin"></param>
/// <param name="dateEnd"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<ActionResult<IEnumerable<ChangeLogDto>>> GetChangeLogForDate(Guid idDiscriminator, DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token);
/// <summary>
/// Добавить одну запись
/// </summary>
/// <param name="idDiscriminator"></param>
/// <param name="dto"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<ActionResult<int>> Add(Guid idDiscriminator, ChangeLogDto dto, CancellationToken token);
Task<ActionResult<int>> Add(Guid idDiscriminator, DataWithWellDepthAndSectionDto dto, CancellationToken token);
/// <summary>
/// Добавить несколько записей
/// </summary>
/// <param name="idDiscriminator"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<ActionResult<int>> AddRange(IEnumerable<IDictionary<string, object>> dtos, CancellationToken token);
Task<ActionResult<int>> AddRange(Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token);
/// <summary>
/// Обновить одну запись
/// </summary>
/// <param name="idDiscriminator"></param>
/// <param name="dto"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<ActionResult<int>> Update(IDictionary<string, object> dto, CancellationToken token);
Task<ActionResult<int>> Update(Guid idDiscriminator, DataWithWellDepthAndSectionDto dto, CancellationToken token);
/// <summary>
/// Обновить несколько записей
/// </summary>
/// <param name="idDiscriminator"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<ActionResult<int>> UpdateRange(IEnumerable<IDictionary<string, object>> dtos, CancellationToken token);
Task<ActionResult<int>> UpdateRange(Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token);
/// <summary>
/// Удалить одну запись
@ -61,7 +77,7 @@ public interface IChangeLogApi
/// <param name="id"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<ActionResult<int>> Delete(int id, CancellationToken token);
Task<ActionResult<int>> Delete(Guid id, CancellationToken token);
/// <summary>
/// Удалить несколько записей
@ -69,5 +85,5 @@ public interface IChangeLogApi
/// <param name="ids"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<ActionResult<int>> DeleteRange(IEnumerable<int> ids, CancellationToken token);
Task<ActionResult<int>> DeleteRange(IEnumerable<Guid> ids, CancellationToken token);
}

View File

@ -10,11 +10,6 @@ public class ChangeLogDto
}
/// <summary>
///
/// </summary>
public Guid Id { get; set; }
/// <summary>
/// Создатель записи
/// </summary>
@ -40,23 +35,8 @@ public class ChangeLogDto
/// </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 IDictionary<string, object> Value { get; set; }
public DataWithWellDepthAndSectionDto Value { get; set; }
}

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Persistence.Models;
/// <summary>
/// Dto для хранения записей, содержащих начальную и конечную глубину забоя, а также секцию
/// </summary>
public class DataWithWellDepthAndSectionDto
{
public DataWithWellDepthAndSectionDto()
{
}
/// <summary>
/// Ключ записи
/// </summary>
public Guid Id { 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 IDictionary<string, object> Value { get; set; } = default!;
}

View File

@ -16,16 +16,17 @@ public interface IChangeLogRepository //: ISyncRepository<TDto>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> InsertRange(Guid idUser, Guid idDiscriminator, IEnumerable<ChangeLogDto> dtos, CancellationToken token);
Task<int> InsertRange(Guid idUser, Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token);
/// <summary>
/// Редактирование записей
/// </summary>
/// <param name="idUser">пользователь, который редактирует</param>
/// <param name="idDiscriminator"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> UpdateRange(int idUser, IEnumerable<IDictionary<string, object>> dtos, CancellationToken token);
Task<int> UpdateRange(Guid idUser, Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token);
/// <summary>
/// Добавляет Dto у которых id == 0, изменяет dto у которых id != 0
@ -39,19 +40,20 @@ public interface IChangeLogRepository //: ISyncRepository<TDto>
/// <summary>
/// Помечает записи как удаленные
/// </summary>
/// <param name="idUser">пользователь, который чистит</param>
/// <param name="idUser"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> Clear(int idUser, CancellationToken token);
Task<int> Clear(Guid idUser, Guid idDiscriminator, CancellationToken token);
/// <summary>
/// Очистить и добавить новые
/// </summary>
/// <param name="idUser"></param>
/// <param name="idDiscriminator"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> ClearAndInsertRange(int idUser, IEnumerable<IDictionary<string, object>> dtos, CancellationToken token);
Task<int> ClearAndInsertRange(Guid idUser, Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token);
/// <summary>
/// Пометить записи как удаленные
@ -60,7 +62,7 @@ public interface IChangeLogRepository //: ISyncRepository<TDto>
/// <param name="ids"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> MarkAsDeleted(int idUser, IEnumerable<int> ids, CancellationToken token);
Task<int> MarkAsDeleted(Guid idUser, IEnumerable<Guid> ids, CancellationToken token);
/// <summary>
/// Получение дат изменений записей
@ -71,18 +73,21 @@ public interface IChangeLogRepository //: ISyncRepository<TDto>
Task<IEnumerable<DateOnly>> GetDatesChange(CancellationToken token);
/// <summary>
/// Получение измененных записей за определенную дату
/// Получение измененных записей за период времени
/// </summary>
/// <param name="updateFrom"></param>
/// <param name="idDiscriminator"></param>
/// <param name="dateBegin"></param>
/// <param name="dateEnd"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<ChangeLogDto>> GetChangeLogForDate(DateTimeOffset? updateFrom, CancellationToken token);
Task<IEnumerable<ChangeLogDto>> GetChangeLogForDate(Guid idDiscriminator, DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token);
/// <summary>
/// Получение текущих сейчас записей по параметрам
/// Получение актуальных записей на определенный момент времени
/// </summary>
/// <param name="request"></param>
/// <param name="idDiscriminator"></param>
/// <param name="moment">текущий момент времени</param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<IDictionary<string, object>>> GetCurrent(DateTimeOffset moment, CancellationToken token);
Task<IEnumerable<DataWithWellDepthAndSectionDto>> GetByDate(Guid idDiscriminator, DateTimeOffset moment, CancellationToken token);
}