From 86ca7ba841ac01e2e9c892740de7f780c060a075 Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Mon, 18 Nov 2024 00:13:44 +0100 Subject: [PATCH] Aggiunta nuova implementazione - Services --- .../Service/GenericService.cs | 173 ++++++++++++++++++ .../Service/IGenericService.cs | 33 ++++ 2 files changed, 206 insertions(+) create mode 100644 src/GSWCloudApp.Common/Service/GenericService.cs create mode 100644 src/GSWCloudApp.Common/Service/IGenericService.cs diff --git a/src/GSWCloudApp.Common/Service/GenericService.cs b/src/GSWCloudApp.Common/Service/GenericService.cs new file mode 100644 index 0000000..efa5f4f --- /dev/null +++ b/src/GSWCloudApp.Common/Service/GenericService.cs @@ -0,0 +1,173 @@ +using AutoMapper; +using GSWCloudApp.Common.RedisCache; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.HttpResults; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace GSWCloudApp.Common.Service; + +public class GenericService : IGenericService +{ + public async Task>, NotFound>> GetAllAsync([FromQuery] bool cacheData, DbContext dbContext, ICacheService cacheService, IMapper mapper) + where TEntity : class + where TDto : class + { + var cacheKey = typeof(TEntity).Name; + var entity = new List(); + + if (!cacheData) + { + entity = await dbContext.Set().AsNoTracking().ToListAsync(); + + if (entity.Count == 0) + { + return TypedResults.NotFound(); + } + + return TypedResults.Ok(mapper.Map>(entity)); + } + + entity = await cacheService.GetCacheAsync>(cacheKey); + + if (entity != null) + { + return TypedResults.Ok(mapper.Map>(entity)); + } + + entity = await dbContext.Set() + .AsNoTracking() + .ToListAsync(); + + if (entity.Count == 0) + { + return TypedResults.NotFound(); + } + + await cacheService.SetCacheAsync(cacheKey, entity); + + var result = mapper.Map>(entity); + + return TypedResults.Ok(result); + } + + public async Task, NotFound>> GetByIdAsync(Guid id, DbContext dbContext, ICacheService cacheService, IMapper mapper) + where TEntity : class + where TDto : class + { + var cacheKey = $"{typeof(TEntity).Name}-{id}"; + var entity = await cacheService.GetCacheAsync(cacheKey); + + if (entity != null) + { + return TypedResults.Ok(mapper.Map(entity)); + } + + entity = await dbContext.Set() + .AsNoTracking() + .FirstOrDefaultAsync(e => EF.Property(e, "Id") == id); + + if (entity is null) + { + return TypedResults.NotFound(); + } + + await cacheService.SetCacheAsync(cacheKey, entity); + + var result = mapper.Map(entity); + + return TypedResults.Ok(result); + } + + public async Task, BadRequest>> PostAsync(TCreateDto createDto, DbContext dbContext, IMapper mapper) + where TEntity : class + where TDto : class + { + var entity = mapper.Map(createDto); + + dbContext.Set().Add(entity); + + try + { + await dbContext.SaveChangesAsync(); + + return TypedResults.Ok(mapper.Map(entity)); + } + catch (Exception ex) + { + return TypedResults.BadRequest(ex.Message); + } + } + + public async Task, NotFound, BadRequest>> UpdateAsync(Guid id, TEditDto editDto, DbContext dbContext, IMapper mapper) + where TEntity : class + where TDto : class + { + var entity = await dbContext.Set().FindAsync(id); + + if (entity is null) + { + return TypedResults.NotFound(); + } + + mapper.Map(editDto, entity); + + dbContext.Set().Update(entity); + + try + { + await dbContext.SaveChangesAsync(); + + return TypedResults.Ok(mapper.Map(entity)); + } + catch (Exception ex) + { + return TypedResults.BadRequest(ex.Message); + } + } + + public async Task> DeleteAsync(Guid id, DbContext dbContext) + where TEntity : class + { + var entity = await dbContext.Set() + .AsNoTracking() + .FirstOrDefaultAsync(e => EF.Property(e, "Id") == id); + + if (entity is null) + { + return TypedResults.NotFound(); + } + + dbContext.Set().Remove(entity); + await dbContext.SaveChangesAsync(); + + return TypedResults.NoContent(); + } + + public async Task>, NotFound>> FilterAsync(Guid festaId, DbContext dbContext, ICacheService cacheService, IMapper mapper) + where TEntity : class + where TDto : class + { + var cacheKey = $"{typeof(TEntity).Name}-{festaId}"; + var entity = await cacheService.GetCacheAsync>(cacheKey); + + if (entity != null) + { + return TypedResults.Ok(mapper.Map>(entity)); + } + + entity = await dbContext.Set() + .AsNoTracking() + .Where(e => EF.Property(e, "FestaId") == festaId) + .ToListAsync(); + + if (entity.Count == 0) + { + return TypedResults.NotFound(); + } + + await cacheService.SetCacheAsync(cacheKey, entity); + + return TypedResults.Ok(mapper.Map>(entity)); + } +} \ No newline at end of file diff --git a/src/GSWCloudApp.Common/Service/IGenericService.cs b/src/GSWCloudApp.Common/Service/IGenericService.cs new file mode 100644 index 0000000..e73742f --- /dev/null +++ b/src/GSWCloudApp.Common/Service/IGenericService.cs @@ -0,0 +1,33 @@ +using AutoMapper; +using GSWCloudApp.Common.RedisCache; +using Microsoft.AspNetCore.Http.HttpResults; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace GSWCloudApp.Common.Service; + +public interface IGenericService +{ + Task>, NotFound>> GetAllAsync([FromQuery] bool cacheData, DbContext dbContext, ICacheService cacheService, IMapper mapper) + where TEntity : class + where TDto : class; + + Task, NotFound>> GetByIdAsync(Guid id, DbContext dbContext, ICacheService cacheService, IMapper mapper) + where TEntity : class + where TDto : class; + + Task, BadRequest>> PostAsync(TCreateDto createDto, DbContext dbContext, IMapper mapper) + where TEntity : class + where TDto : class; + + Task, NotFound, BadRequest>> UpdateAsync(Guid id, TEditDto editDto, DbContext dbContext, IMapper mapper) + where TEntity : class + where TDto : class; + + Task> DeleteAsync(Guid id, DbContext dbContext) + where TEntity : class; + + Task>, NotFound>> FilterAsync(Guid festaId, DbContext dbContext, ICacheService cacheService, IMapper mapper) + where TEntity : class + where TDto : class; +} \ No newline at end of file