Подключение Microsoft.EntityFrameworkCore. Начало

This commit is contained in:
Olga Nemt 2024-10-31 17:02:48 +05:00
parent 5998d71562
commit f22799f1ef
7 changed files with 228 additions and 32 deletions

View File

@ -0,0 +1,42 @@
namespace Persistence.Models;
/// <summary>
/// Часть записи описывающая изменение
/// </summary>
public class ChangeLogDto<T> where T: class
{
/// <summary>
/// Запись
/// </summary>
public required T Item { get; set; }
/// <summary>
/// Автор
/// </summary>
public UserDto? Author { get; set; }
/// <summary>
/// Автор
/// </summary>
public UserDto? Editor { get; set; }
/// <summary>
/// Дата создания записи
/// </summary>
public DateTimeOffset Creation { get; set; }
/// <summary>
/// Дата устаревания (например при удалении)
/// </summary>
public DateTimeOffset? Obsolete { get; set; }
/// <summary>
/// Id состояния
/// </summary>
public int IdState { get; set; }
/// <summary>
/// Id заменяемой записи
/// </summary>
public int? IdPrevious { get; set; }
}

View File

@ -0,0 +1,62 @@
namespace Persistence.Models;
/// <summary>
/// Часть записи описывающая изменение
/// </summary>
public interface IChangeLogAbstract
{
/// <summary>
/// Актуальная
/// </summary>
public const int IdStateActual = 0;
/// <summary>
/// Замененная
/// </summary>
public const int IdStateReplaced = 1;
/// <summary>
/// Удаленная
/// </summary>
public const int IdStateDeleted = 2;
/// <summary>
/// Очищено при импорте
/// </summary>
public const int IdCleared = 3;
/// <summary>
/// Ид записи
/// </summary>
public int Id { get; set; }
/// <summary>
/// Автор изменения
/// </summary>
public int IdAuthor { get; set; }
/// <summary>
/// Редактор
/// </summary>
public int? IdEditor { get; set; }
/// <summary>
/// Дата создания записи
/// </summary>
public DateTimeOffset Creation { get; set; }
/// <summary>
/// Дата устаревания (например при удалении)
/// </summary>
public DateTimeOffset? Obsolete { get; set; }
/// <summary>
/// "ИД состояния записи: \n0 - актуальная\n1 - замененная\n2 - удаленная
/// </summary>
public int IdState { get; set; }
/// <summary>
/// Id заменяемой записи
/// </summary>
public int? IdPrevious { get; set; }
}

View File

@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Persistence.Models;
public class UserDto
{
/// <inheritdoc/>
public int Id { get; set; }
/// <summary>
/// логин
/// </summary>
public string Login { get; set; } = null!;
/// <summary>
/// Имя
/// </summary>
public string? Name { get; set; }
/// <summary>
/// Фамилия
/// </summary>
public string? Surname { get; set; }
/// <summary>
/// Отчество
/// </summary>
public string? Patronymic { get; set; }
/// <summary>
/// Email
/// </summary>
public string Email { get; set; } = null!;
/// <summary>
/// Phone
/// </summary>
public string? Phone { get; set; }
/// <summary>
/// Должность
/// </summary>
public string? Position { get; set; }
/// <summary>
/// Id компании
/// </summary>
public int IdCompany { get; set; }
/// <summary>
/// Id состояния пользователя
/// 0 - не активен,
/// 1 - активен,
/// 2 - заблокирован
/// </summary>
public short IdState { get; set; }
/// <summary>
/// Получение отображаемого имени
/// </summary>
/// <returns></returns>
}

View File

@ -6,4 +6,8 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.10" />
</ItemGroup>
</Project> </Project>

View File

@ -1,23 +1,29 @@
using System; using Microsoft.EntityFrameworkCore;
using System.Collections.Generic; using Microsoft.EntityFrameworkCore.Infrastructure;
using System.Linq; using Persistence.Models;
using System.Text;
using System.Threading.Tasks;
namespace Persistence.Repositories; namespace Persistence.Repositories;
public abstract class AbstractChangeLogRepository<T, TRequest> : IChangeLogRepository<T, TRequest> public abstract class AbstractChangeLogRepository<TEntity, TRequest, TDto> : IChangeLogRepository<TDto, TRequest>
where TDto : class, new()
where TEntity : class, IChangeLogAbstract
{ {
private readonly DbContext dbContext;
protected AbstractChangeLogRepository(DbContext dbContext)
{
this.dbContext = dbContext;
}
public Task<int> Clear(int idUser, TRequest request, CancellationToken token) public Task<int> Clear(int idUser, TRequest request, CancellationToken token)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<int> ClearAndInsertRange(int idUser, TRequest request, IEnumerable<T> dtos, CancellationToken token) public Task<int> ClearAndInsertRange(int idUser, TRequest request, IEnumerable<TDto> dtos, CancellationToken token)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<IEnumerable<T>> GetCurrent(TRequest request, CancellationToken token) public Task<IEnumerable<TDto>> GetCurrent(TRequest request, CancellationToken token)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -27,27 +33,47 @@ public abstract class AbstractChangeLogRepository<T, TRequest> : IChangeLogRepos
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<IEnumerable<T>> GetGtDate(DateTimeOffset date, CancellationToken token) public Task<IEnumerable<TDto>> GetGtDate(DateTimeOffset date, CancellationToken token)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<int> InsertRange(int idUser, IEnumerable<T> dtos, CancellationToken token) public Task<int> InsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
{ {
throw new NotImplementedException(); this.dbContext.Set<TEntity>();
var db = GetDataBase();
using var transaction = db.BeginTransaction();
try
{
//var result = await InsertRangeWithoutTransaction(idUser, dtos, token);
//await transaction.CommitAsync(token);
//return result;
} }
catch
{
//await transaction.RollbackAsync(token);
throw;
}
}
protected abstract DatabaseFacade GetDataBase();
public Task<int> MarkAsDeleted(int idUser, IEnumerable<int> ids, CancellationToken token) public Task<int> MarkAsDeleted(int idUser, IEnumerable<int> ids, CancellationToken token)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<int> UpdateOrInsertRange(int idUser, IEnumerable<T> dtos, CancellationToken token) public Task<int> UpdateOrInsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<int> UpdateRange(int idUser, IEnumerable<T> dtos, CancellationToken token) public Task<int> UpdateRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
{
throw new NotImplementedException();
}
public Task<IEnumerable<ChangeLogDto<TDto>>> GetChangeLogForDate(TRequest request, DateOnly? date, CancellationToken token)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -1,10 +1,13 @@
namespace Persistence.Repositories; using Persistence.Models;
namespace Persistence.Repositories;
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="TDto"></typeparam>
public interface IChangeLogRepository<T, TRequest> : ISyncRepository<T> public interface IChangeLogRepository<TDto, TRequest> : ISyncRepository<TDto>
where TDto : class
{ {
/// <summary> /// <summary>
/// Добавление записей /// Добавление записей
@ -13,7 +16,7 @@ public interface IChangeLogRepository<T, TRequest> : ISyncRepository<T>
/// <param name="dtos"></param> /// <param name="dtos"></param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
Task<int> InsertRange(int idUser, IEnumerable<T> dtos, CancellationToken token); Task<int> InsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token);
/// <summary> /// <summary>
/// Редактирование записей /// Редактирование записей
@ -22,7 +25,7 @@ public interface IChangeLogRepository<T, TRequest> : ISyncRepository<T>
/// <param name="dtos"></param> /// <param name="dtos"></param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
Task<int> UpdateRange(int idUser, IEnumerable<T> dtos, CancellationToken token); Task<int> UpdateRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token);
/// <summary> /// <summary>
/// Добавляет Dto у которых id == 0, изменяет dto у которых id != 0 /// Добавляет Dto у которых id == 0, изменяет dto у которых id != 0
@ -31,7 +34,7 @@ public interface IChangeLogRepository<T, TRequest> : ISyncRepository<T>
/// <param name="dtos"></param> /// <param name="dtos"></param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
Task<int> UpdateOrInsertRange(int idUser, IEnumerable<T> dtos, CancellationToken token); Task<int> UpdateOrInsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token);
/// <summary> /// <summary>
/// Помечает записи как удаленные /// Помечает записи как удаленные
@ -50,7 +53,7 @@ public interface IChangeLogRepository<T, TRequest> : ISyncRepository<T>
/// <param name="dtos"></param> /// <param name="dtos"></param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
Task<int> ClearAndInsertRange(int idUser, TRequest request, IEnumerable<T> dtos, CancellationToken token); Task<int> ClearAndInsertRange(int idUser, TRequest request, IEnumerable<TDto> dtos, CancellationToken token);
/// <summary> /// <summary>
/// Пометить записи как удаленные /// Пометить записи как удаленные
@ -76,7 +79,7 @@ public interface IChangeLogRepository<T, TRequest> : ISyncRepository<T>
/// <param name="date">Фильтр по дате. Если null - вернет все записи, без привязки к дате</param> /// <param name="date">Фильтр по дате. Если null - вернет все записи, без привязки к дате</param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
//Task<IEnumerable<ChangeLogDto<T>>> GetChangeLogForDate(TRequest request, DateOnly? date, CancellationToken token); Task<IEnumerable<ChangeLogDto<TDto>>> GetChangeLogForDate(TRequest request, DateOnly? date, CancellationToken token);
/// <summary> /// <summary>
/// Получение текущих сейчас записей по параметрам /// Получение текущих сейчас записей по параметрам
@ -84,12 +87,5 @@ public interface IChangeLogRepository<T, TRequest> : ISyncRepository<T>
/// <param name="request"></param> /// <param name="request"></param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
Task<IEnumerable<T>> GetCurrent(TRequest request, CancellationToken token); Task<IEnumerable<TDto>> GetCurrent(TRequest request, CancellationToken token);
/// <summary>
/// Получение объекта, реализующего интерфейс IChangeLogRepositoryBuilder
/// для последующих вызовов методов фильтрации по запросам
/// </summary>
/// <returns></returns>
//IChangeLogQueryBuilder<TDto, TRequest> GetQueryBuilder(ChangeLogRequest request);
} }

View File

@ -3,8 +3,8 @@
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="TDto"></typeparam>
public interface ISyncRepository<T> public interface ISyncRepository<TDto>
{ {
/// <summary> /// <summary>
/// ///
@ -12,5 +12,5 @@ public interface ISyncRepository<T>
/// <param name="date"></param> /// <param name="date"></param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
Task<IEnumerable<T>> GetGtDate(DateTimeOffset date, CancellationToken token); Task<IEnumerable<TDto>> GetGtDate(DateTimeOffset date, CancellationToken token);
} }