From d5e11bc2e4fb39cdb1d926ce3d60fe68efb5e5a8 Mon Sep 17 00:00:00 2001 From: ivanszabo Date: Thu, 3 Apr 2025 12:05:08 +0200 Subject: [PATCH] BaseService BL projektbe --- src/WorkFlowCheck.BL/Services/BaseService.cs | 147 ++++++++++++++++++ .../Services/CheckListService.cs | 4 +- .../Services/Interfaces/ISyncService.cs | 3 + src/WorkFlowCheck.BL/Services/SyncService.cs | 28 ++-- src/WorkFlowCheck.DL/AppDbContext.cs | 12 -- 5 files changed, 169 insertions(+), 25 deletions(-) create mode 100644 src/WorkFlowCheck.BL/Services/BaseService.cs diff --git a/src/WorkFlowCheck.BL/Services/BaseService.cs b/src/WorkFlowCheck.BL/Services/BaseService.cs new file mode 100644 index 0000000..39be028 --- /dev/null +++ b/src/WorkFlowCheck.BL/Services/BaseService.cs @@ -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 DeleteEntityByIdAsync(int id) where T : class + { + try + { + var entity = await _dbContext.Set().FindAsync(id); + + if (entity == null) + return false; + _dbContext.Set().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 GetEntityByIdAsync(int id) where T : class + { + try + { + var entity = await _dbContext.Set().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> GetEntityByFilterAsync(TFilter filter) where TEntity : class where TFilter : class + { + try + { + IQueryable query = _dbContext.Set(); + + + 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>(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(); + } + } + public async Task> GetPagedEntityByFilterAsync(TFilter filter, int pageNumber, int pageSize) where TEntity : class where TFilter : class + { + try + { + + + // Az IQueryable típusú lista létrehozása + IQueryable query = _dbContext.Set(); + + // 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>(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 + { + 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(); + } + } + + } + public class PagedResult + { + public List Items { get; set; } = new List(); + public int TotalCount { get; set; } + public int PageNumber { get; set; } + public int PageSize { get; set; } + } + +} diff --git a/src/WorkFlowCheck.BL/Services/CheckListService.cs b/src/WorkFlowCheck.BL/Services/CheckListService.cs index 773bf38..3025c10 100644 --- a/src/WorkFlowCheck.BL/Services/CheckListService.cs +++ b/src/WorkFlowCheck.BL/Services/CheckListService.cs @@ -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) diff --git a/src/WorkFlowCheck.BL/Services/Interfaces/ISyncService.cs b/src/WorkFlowCheck.BL/Services/Interfaces/ISyncService.cs index 4a63bce..4560d7d 100644 --- a/src/WorkFlowCheck.BL/Services/Interfaces/ISyncService.cs +++ b/src/WorkFlowCheck.BL/Services/Interfaces/ISyncService.cs @@ -7,17 +7,20 @@ namespace WorkFlowCheck.BL.Services.Interfaces { Task> GetAllCheckPointAsync(); Task GetCheckPointAsync(int id); + Task DeleteCheckPointAsync(int id); Task UpdateCheckPointAsync(CheckPointDTO checkPointDTO); Task> UpdateAllCheckPointAsync(List checkPointListDTO); Task> GetAllLocationAsync(); Task GetLocationAsync(int id); + Task DeleteLocationAsync(int id); Task UpdateLocationAsync(LocationDTO LocationDTO); Task> GetAllEquipmentAsync(); Task GetEquipmentAsync(int id); + Task DeleteEquipmentAsync(int id); Task UpdateEquipmentAsync(EquipmentDTO EquipmentDTO); diff --git a/src/WorkFlowCheck.BL/Services/SyncService.cs b/src/WorkFlowCheck.BL/Services/SyncService.cs index 838c4ba..9fa724c 100644 --- a/src/WorkFlowCheck.BL/Services/SyncService.cs +++ b/src/WorkFlowCheck.BL/Services/SyncService.cs @@ -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> GetAllCheckPointAsync() { @@ -68,6 +61,10 @@ namespace WorkFlowCheck.BL.Services return retVal; } + public async Task DeleteCheckPointAsync(int id) + { + return await DeleteEntityByIdAsync(id); + } public async Task UpdateCheckPointAsync(CheckPointDTO checkPointDTO) { var retVal = new CheckPointDTO(); @@ -173,6 +170,10 @@ namespace WorkFlowCheck.BL.Services return retVal; } + public async Task DeleteLocationAsync(int id) + { + return await DeleteEntityByIdAsync(id); + } public async Task UpdateLocationAsync(LocationDTO LocationDTO) { var retVal = new LocationDTO(); @@ -249,6 +250,10 @@ namespace WorkFlowCheck.BL.Services return retVal; } + public async Task DeleteEquipmentAsync(int id) + { + return await DeleteEntityByIdAsync(id); + } public async Task UpdateEquipmentAsync(EquipmentDTO EquipmentDTO) { var retVal = new EquipmentDTO(); @@ -284,6 +289,7 @@ namespace WorkFlowCheck.BL.Services return retVal; } + public async Task> GetAllCheckListTemplateAsync(int userId) { var retVal = new List(); @@ -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); } diff --git a/src/WorkFlowCheck.DL/AppDbContext.cs b/src/WorkFlowCheck.DL/AppDbContext.cs index 730a309..41cc5f5 100644 --- a/src/WorkFlowCheck.DL/AppDbContext.cs +++ b/src/WorkFlowCheck.DL/AppDbContext.cs @@ -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 options) - : base(options) - { - _requestContext = new { CurrentUserEmail = "", CurrentUsername = "admin", TraceId = 0 }; - } -#endif #region Entities DbSet public DbSet CheckListHeaders { get; set; } = null!; public DbSet CheckListRows { get; set; } = null!;