BaseService BL projektbe
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WorkFlowCheck.DL;
|
||||
|
||||
namespace WorkFlowCheck.BL.Services
|
||||
{
|
||||
public class BaseService
|
||||
|
||||
{
|
||||
public readonly AppDbContext _dbContext;
|
||||
public readonly IMapper _mapper;
|
||||
|
||||
public BaseService(AppDbContext db_dbContext, IMapper mapper)
|
||||
{
|
||||
_dbContext = db_dbContext;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteEntityByIdAsync<T>(int id) where T : class
|
||||
{
|
||||
try
|
||||
{
|
||||
var entity = await _dbContext.Set<T>().FindAsync(id);
|
||||
|
||||
if (entity == null)
|
||||
return false;
|
||||
_dbContext.Set<T>().Remove(entity);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error($"Hiba történt a törlés során: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public async Task<T?> GetEntityByIdAsync<T>(int id) where T : class
|
||||
{
|
||||
try
|
||||
{
|
||||
var entity = await _dbContext.Set<T>().FindAsync(id);
|
||||
return entity;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error($"Hiba történt a lekérdezés során: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public async Task<List<TEntity>> GetEntityByFilterAsync<TEntity, TFilter>(TFilter filter) where TEntity : class where TFilter : class
|
||||
{
|
||||
try
|
||||
{
|
||||
IQueryable<TEntity> query = _dbContext.Set<TEntity>();
|
||||
|
||||
|
||||
foreach (var property in typeof(TFilter).GetProperties())
|
||||
{
|
||||
var value = property.GetValue(filter);
|
||||
if (value != null)
|
||||
{
|
||||
var parameter = Expression.Parameter(typeof(TEntity), "x");
|
||||
var member = Expression.Property(parameter, property.Name);
|
||||
var constant = Expression.Constant(value);
|
||||
var equal = Expression.Equal(member, constant);
|
||||
var lambda = Expression.Lambda<Func<TEntity, bool>>(equal, parameter);
|
||||
|
||||
query = query.Where(lambda);
|
||||
}
|
||||
}
|
||||
|
||||
// Adatok lekérdezése
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error($"Hiba történt a lekérdezés során: {ex.Message}");
|
||||
return new List<TEntity>();
|
||||
}
|
||||
}
|
||||
public async Task<PagedResult<TEntity>> GetPagedEntityByFilterAsync<TEntity, TFilter>(TFilter filter, int pageNumber, int pageSize) where TEntity : class where TFilter : class
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
// Az IQueryable típusú lista létrehozása
|
||||
IQueryable<TEntity> query = _dbContext.Set<TEntity>();
|
||||
|
||||
// Dinamikus szűrés a filter tulajdonságai alapján
|
||||
foreach (var property in typeof(TFilter).GetProperties())
|
||||
{
|
||||
var value = property.GetValue(filter);
|
||||
if (value != null)
|
||||
{
|
||||
var parameter = Expression.Parameter(typeof(TEntity), "x");
|
||||
var member = Expression.Property(parameter, property.Name);
|
||||
var constant = Expression.Constant(value);
|
||||
var equal = Expression.Equal(member, constant);
|
||||
var lambda = Expression.Lambda<Func<TEntity, bool>>(equal, parameter);
|
||||
query = query.Where(lambda);
|
||||
}
|
||||
}
|
||||
|
||||
// Teljes rekordok száma a szűrő után
|
||||
int totalCount = await query.CountAsync();
|
||||
|
||||
// Lapozás alkalmazása
|
||||
var items = await query
|
||||
.Skip((pageNumber - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync();
|
||||
|
||||
// Lapozási eredmény összerakása
|
||||
return new PagedResult<TEntity>
|
||||
{
|
||||
Items = items,
|
||||
TotalCount = totalCount,
|
||||
PageNumber = pageNumber,
|
||||
PageSize = pageSize
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Hiba történt a lekérdezés során: {ex.Message}");
|
||||
return new PagedResult<TEntity>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public class PagedResult<TEntity>
|
||||
{
|
||||
public List<TEntity> Items { get; set; } = new List<TEntity>();
|
||||
public int TotalCount { get; set; }
|
||||
public int PageNumber { get; set; }
|
||||
public int PageSize { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,8 +21,8 @@ namespace WorkFlowCheck.BL.Services
|
||||
{
|
||||
public class CheckListService : ICheckListService
|
||||
{
|
||||
private AppDbContext _dbContext;
|
||||
private IMapper _mapper;
|
||||
private readonly AppDbContext _dbContext;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly INumberGeneratorService _numberGeneratorService;
|
||||
|
||||
public CheckListService(AppDbContext dbContext, IMapper mapper, INumberGeneratorService numberGeneratorService)
|
||||
|
||||
@@ -7,17 +7,20 @@ namespace WorkFlowCheck.BL.Services.Interfaces
|
||||
{
|
||||
Task<List<CheckPointDTO>> GetAllCheckPointAsync();
|
||||
Task<CheckPointDTO> GetCheckPointAsync(int id);
|
||||
Task<bool> DeleteCheckPointAsync(int id);
|
||||
Task<CheckPointDTO> UpdateCheckPointAsync(CheckPointDTO checkPointDTO);
|
||||
Task<List<CheckPointDTO>> UpdateAllCheckPointAsync(List<CheckPointDTO> checkPointListDTO);
|
||||
|
||||
|
||||
Task<List<LocationDTO>> GetAllLocationAsync();
|
||||
Task<LocationDTO> GetLocationAsync(int id);
|
||||
Task<bool> DeleteLocationAsync(int id);
|
||||
Task<LocationDTO> UpdateLocationAsync(LocationDTO LocationDTO);
|
||||
|
||||
|
||||
Task<List<EquipmentDTO>> GetAllEquipmentAsync();
|
||||
Task<EquipmentDTO> GetEquipmentAsync(int id);
|
||||
Task<bool> DeleteEquipmentAsync(int id);
|
||||
Task<EquipmentDTO> UpdateEquipmentAsync(EquipmentDTO EquipmentDTO);
|
||||
|
||||
|
||||
|
||||
@@ -12,16 +12,9 @@ using WorkFlowCheck.DL.Entities;
|
||||
|
||||
namespace WorkFlowCheck.BL.Services
|
||||
{
|
||||
public class SyncService : ISyncService
|
||||
public class SyncService : BaseService, ISyncService
|
||||
{
|
||||
private AppDbContext _dbContext;
|
||||
private IMapper _mapper;
|
||||
|
||||
public SyncService(AppDbContext dbContext, IMapper mapper)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_mapper = mapper;
|
||||
}
|
||||
public SyncService(AppDbContext dbContext, IMapper mapper) : base(dbContext, mapper) { }
|
||||
|
||||
public async Task<List<CheckPointDTO>> GetAllCheckPointAsync()
|
||||
{
|
||||
@@ -68,6 +61,10 @@ namespace WorkFlowCheck.BL.Services
|
||||
|
||||
return retVal;
|
||||
}
|
||||
public async Task<bool> DeleteCheckPointAsync(int id)
|
||||
{
|
||||
return await DeleteEntityByIdAsync<CheckPoint>(id);
|
||||
}
|
||||
public async Task<CheckPointDTO> UpdateCheckPointAsync(CheckPointDTO checkPointDTO)
|
||||
{
|
||||
var retVal = new CheckPointDTO();
|
||||
@@ -173,6 +170,10 @@ namespace WorkFlowCheck.BL.Services
|
||||
|
||||
return retVal;
|
||||
}
|
||||
public async Task<bool> DeleteLocationAsync(int id)
|
||||
{
|
||||
return await DeleteEntityByIdAsync<Location>(id);
|
||||
}
|
||||
public async Task<LocationDTO> UpdateLocationAsync(LocationDTO LocationDTO)
|
||||
{
|
||||
var retVal = new LocationDTO();
|
||||
@@ -249,6 +250,10 @@ namespace WorkFlowCheck.BL.Services
|
||||
|
||||
return retVal;
|
||||
}
|
||||
public async Task<bool> DeleteEquipmentAsync(int id)
|
||||
{
|
||||
return await DeleteEntityByIdAsync<Equipment>(id);
|
||||
}
|
||||
public async Task<EquipmentDTO> UpdateEquipmentAsync(EquipmentDTO EquipmentDTO)
|
||||
{
|
||||
var retVal = new EquipmentDTO();
|
||||
@@ -284,6 +289,7 @@ namespace WorkFlowCheck.BL.Services
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
public async Task<List<CheckListTemplateHeaderDTO>> GetAllCheckListTemplateAsync(int userId)
|
||||
{
|
||||
var retVal = new List<CheckListTemplateHeaderDTO>();
|
||||
@@ -377,12 +383,12 @@ namespace WorkFlowCheck.BL.Services
|
||||
if (dtoRow != null)
|
||||
{
|
||||
row.Answer = dtoRow.Answer;
|
||||
if (dtoRow.Photo != null && dtoRow.Photo.Length > 0 )
|
||||
if (dtoRow.Photo != null && dtoRow.Photo.Length > 0)
|
||||
{
|
||||
string imageDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Images");
|
||||
var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
|
||||
var fileName = $"PH_{timestamp}_{row.CheckListHeaderId}_{row.Id}.jpeg";
|
||||
row.PhotoFileName = fileName;
|
||||
row.PhotoFileName = fileName;
|
||||
var filePath = Path.Combine(imageDirectory, fileName);
|
||||
await File.WriteAllBytesAsync(filePath, dtoRow.Photo);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WorkFlowCheck.Common.RequestContext;
|
||||
using WorkFlowCheck.Common.Security;
|
||||
using WorkFlowCheck.DL.Configurations;
|
||||
@@ -23,14 +19,6 @@ namespace WorkFlowCheck.DL
|
||||
_requestContext = requestContext;
|
||||
}
|
||||
|
||||
#if !DEBUG
|
||||
private dynamic _requestContext;
|
||||
public AppDbContext(DbContextOptions<AppDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
_requestContext = new { CurrentUserEmail = "", CurrentUsername = "admin", TraceId = 0 };
|
||||
}
|
||||
#endif
|
||||
#region Entities DbSet
|
||||
public DbSet<Entities.CheckListHeader> CheckListHeaders { get; set; } = null!;
|
||||
public DbSet<Entities.CheckListRow> CheckListRows { get; set; } = null!;
|
||||
|
||||
Reference in New Issue
Block a user