2023-05-17 16:30:13 +10:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using PARR.DAL.Context;
|
2023-06-16 15:00:11 +10:00
|
|
|
|
using PARR.DAL.DomainModels;
|
2023-05-17 16:30:13 +10:00
|
|
|
|
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()
|
|
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-16 15:00:11 +10:00
|
|
|
|
public virtual IQueryable<T> GetPage(IQueryable<T> query, PaginationFilter paginationFilter)
|
|
|
|
|
|
{
|
|
|
|
|
|
int skip = (paginationFilter.PageNumber - 1) * paginationFilter.PageSize;
|
2023-05-17 16:30:13 +10:00
|
|
|
|
|
2023-06-16 15:00:11 +10:00
|
|
|
|
return query.Skip(skip).Take(paginationFilter.PageSize);
|
|
|
|
|
|
}
|
2023-05-17 16:30:13 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|