160 lines
4.8 KiB
C#
160 lines
4.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
||
using Microsoft.Extensions.Logging;
|
||
using PARR.DAL.Context;
|
||
using PARR.DAL.DomainModels;
|
||
using PARR.DAL.Models;
|
||
using PARR.DAL.Models.Base;
|
||
using PARR.DAL.Services.Interfaces.Base;
|
||
|
||
namespace PARR.DAL.Services.Abstracts
|
||
{
|
||
internal abstract class BaseService<T> : IBaseService<T> where T : class, IBase
|
||
{
|
||
private readonly ILogger<BaseService<T>> logger;
|
||
|
||
protected abstract DbSet<T> EntitySet { get; }
|
||
protected abstract DataContext EntitiContext { get; }
|
||
|
||
public BaseService(ILogger<BaseService<T>> logger)
|
||
{
|
||
this.logger = logger;
|
||
}
|
||
|
||
public virtual async Task<bool> AddRangeAsync(List<T> objs)
|
||
{
|
||
objs.ForEach(item => item.DateCreated = DateTimeOffset.UtcNow);
|
||
|
||
try
|
||
{
|
||
await EntitySet.AddRangeAsync(objs);
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
logger.LogError(ex, "Ошибка при добавлении диапазона в БД.");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public async Task<bool> CommitAsync()
|
||
{
|
||
#region test
|
||
////https://stackoverflow.com/questions/53064528/entityframework-core-get-changes-occured-in-entity-and-related-data
|
||
//var modifiedEntrities = EntitiContext.ChangeTracker.Entries()
|
||
// .Where(t => t.State == EntityState.Modified || t.State == EntityState.Deleted);
|
||
|
||
//foreach (var change in modifiedEntrities)
|
||
//{
|
||
// ////var type = change.Entity.GetType();
|
||
// ////var userType = typeof(User);
|
||
// if (change.Entity.GetType() == typeof(User))
|
||
// {
|
||
// // это пользователь
|
||
// var user = change.Entity as User;
|
||
// if (user!.Name == "string")
|
||
// {
|
||
// //user.LastLogon = DateTimeOffset.UtcNow;
|
||
// user.Description = "Новый description " + DateTime.Now.Microsecond;
|
||
// }
|
||
|
||
// }
|
||
|
||
// // интерфейс IBase?
|
||
// //if (change.Entity.GetType().IsAssignableFrom(typeof(IBase)))
|
||
|
||
// if (change.Entity.GetType().GetInterface(nameof(IBase)) != null)
|
||
// {
|
||
// (change.Entity as IBase)!.DateModified = DateTimeOffset.UtcNow;
|
||
// }
|
||
|
||
//}
|
||
|
||
|
||
#endregion
|
||
|
||
try
|
||
{
|
||
await EntitiContext.SaveChangesAsync();
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
logger.LogError(ex, "Ошибка при Commit");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public virtual async Task<bool> CreateAsync(T obj)
|
||
{
|
||
obj.DateCreated = DateTimeOffset.UtcNow;
|
||
|
||
try
|
||
{
|
||
await EntitySet.AddAsync(obj);
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
logger.LogError(ex, "Ошибка при добавлении в БД.");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public virtual bool Delete(T obj)
|
||
{
|
||
try
|
||
{
|
||
EntitySet.Remove(obj);
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
logger.LogError(ex, "Ошибка при удалении из БД.");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public virtual async Task<bool> DeleteAsync(Guid id)
|
||
{
|
||
try
|
||
{
|
||
var exist = await GetAsync(id);
|
||
if (exist == null)
|
||
{
|
||
logger.LogError($"Ошибка при удалении из БД. Не найдена запись в БД с id: {id}");
|
||
return false;
|
||
}
|
||
|
||
EntitySet.Remove(exist);
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
logger.LogError(ex, "Ошибка при удалении из БД.");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public virtual IQueryable<T> Get()
|
||
{
|
||
return EntitySet;
|
||
}
|
||
|
||
public virtual async Task<T?> GetAsync(Guid id)
|
||
{
|
||
return await EntitySet.FirstOrDefaultAsync(t => t.Id == id);
|
||
}
|
||
|
||
public virtual IQueryable<T> GetPage(IQueryable<T> query, PaginationFilter paginationFilter)
|
||
{
|
||
//if (paginationFilter.IsDisabledPagination)
|
||
// return query;
|
||
|
||
|
||
int skip = (paginationFilter.PageNumber - 1) * paginationFilter.PageSize;
|
||
|
||
return query.Skip(skip).Take(paginationFilter.PageSize);
|
||
}
|
||
}
|
||
}
|