-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from AngeloDotNet/develop
Sync Main from Develop
- Loading branch information
Showing
6 changed files
with
268 additions
and
2 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/GSWCloudApp.Common/Entities/BaseEntityConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace GSWCloudApp.Common.Entities; | ||
|
||
public abstract class BaseEntityConfiguration<T> : IEntityTypeConfiguration<T> where T : BaseEntity<Guid> | ||
{ | ||
public virtual void Configure(EntityTypeBuilder<T> builder) | ||
{ | ||
builder.HasKey(x => x.Id); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/GSWCloudApp.Common/Extensions/ApplicationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using GSWCloudApp.Common.Options; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.HttpOverrides; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace GSWCloudApp.Common.Extensions; | ||
|
||
public static class ApplicationExtensions | ||
{ | ||
public static void UseDevSwagger(this WebApplication app, ApplicationOptions options) | ||
{ | ||
if (app.Environment.IsDevelopment() || options.SwaggerEnable) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(options => | ||
{ | ||
var descriptions = app.DescribeApiVersions(); | ||
foreach (var description in descriptions) | ||
{ | ||
var url = $"/swagger/{description.GroupName}/swagger.json"; | ||
options.SwaggerEndpoint(url, description.GroupName); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public static void UseForwardNetworking(this WebApplication app) | ||
{ | ||
app.UseForwardedHeaders(new ForwardedHeadersOptions | ||
{ | ||
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ||
}); | ||
|
||
// Non necessario se viene usato NGINX come proxy | ||
// app.UseHttpsRedirection(); | ||
} | ||
|
||
public static void UseAuthentication(this WebApplication app) | ||
{ | ||
app.UseAuthentication(); | ||
app.UseAuthorization(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Results<Ok<List<TDto>>, NotFound>> GetAllAsync<TEntity, TDto>([FromQuery] bool cacheData, DbContext dbContext, ICacheService cacheService, IMapper mapper) | ||
where TEntity : class | ||
where TDto : class | ||
{ | ||
var cacheKey = typeof(TEntity).Name; | ||
var entity = new List<TEntity>(); | ||
|
||
if (!cacheData) | ||
{ | ||
entity = await dbContext.Set<TEntity>().AsNoTracking().ToListAsync(); | ||
|
||
if (entity.Count == 0) | ||
{ | ||
return TypedResults.NotFound(); | ||
} | ||
|
||
return TypedResults.Ok(mapper.Map<List<TDto>>(entity)); | ||
} | ||
|
||
entity = await cacheService.GetCacheAsync<List<TEntity>>(cacheKey); | ||
|
||
if (entity != null) | ||
{ | ||
return TypedResults.Ok(mapper.Map<List<TDto>>(entity)); | ||
} | ||
|
||
entity = await dbContext.Set<TEntity>() | ||
.AsNoTracking() | ||
.ToListAsync(); | ||
|
||
if (entity.Count == 0) | ||
{ | ||
return TypedResults.NotFound(); | ||
} | ||
|
||
await cacheService.SetCacheAsync(cacheKey, entity); | ||
|
||
var result = mapper.Map<List<TDto>>(entity); | ||
|
||
return TypedResults.Ok(result); | ||
} | ||
|
||
public async Task<Results<Ok<TDto>, NotFound>> GetByIdAsync<TEntity, TDto>(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<TEntity>(cacheKey); | ||
|
||
if (entity != null) | ||
{ | ||
return TypedResults.Ok(mapper.Map<TDto>(entity)); | ||
} | ||
|
||
entity = await dbContext.Set<TEntity>() | ||
.AsNoTracking() | ||
.FirstOrDefaultAsync(e => EF.Property<Guid>(e, "Id") == id); | ||
|
||
if (entity is null) | ||
{ | ||
return TypedResults.NotFound(); | ||
} | ||
|
||
await cacheService.SetCacheAsync(cacheKey, entity); | ||
|
||
var result = mapper.Map<TDto>(entity); | ||
|
||
return TypedResults.Ok(result); | ||
} | ||
|
||
public async Task<Results<Ok<TDto>, BadRequest<string>>> PostAsync<TEntity, TDto, TCreateDto>(TCreateDto createDto, DbContext dbContext, IMapper mapper) | ||
where TEntity : class | ||
where TDto : class | ||
{ | ||
var entity = mapper.Map<TEntity>(createDto); | ||
|
||
dbContext.Set<TEntity>().Add(entity); | ||
|
||
try | ||
{ | ||
await dbContext.SaveChangesAsync(); | ||
|
||
return TypedResults.Ok(mapper.Map<TDto>(entity)); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return TypedResults.BadRequest(ex.Message); | ||
} | ||
} | ||
|
||
public async Task<Results<Ok<TDto>, NotFound, BadRequest<string>>> UpdateAsync<TEntity, TDto, TEditDto>(Guid id, TEditDto editDto, DbContext dbContext, IMapper mapper) | ||
where TEntity : class | ||
where TDto : class | ||
{ | ||
var entity = await dbContext.Set<TEntity>().FindAsync(id); | ||
|
||
if (entity is null) | ||
{ | ||
return TypedResults.NotFound(); | ||
} | ||
|
||
mapper.Map(editDto, entity); | ||
|
||
dbContext.Set<TEntity>().Update(entity); | ||
|
||
try | ||
{ | ||
await dbContext.SaveChangesAsync(); | ||
|
||
return TypedResults.Ok(mapper.Map<TDto>(entity)); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return TypedResults.BadRequest(ex.Message); | ||
} | ||
} | ||
|
||
public async Task<Results<NoContent, NotFound>> DeleteAsync<TEntity>(Guid id, DbContext dbContext) | ||
where TEntity : class | ||
{ | ||
var entity = await dbContext.Set<TEntity>() | ||
.AsNoTracking() | ||
.FirstOrDefaultAsync(e => EF.Property<Guid>(e, "Id") == id); | ||
|
||
if (entity is null) | ||
{ | ||
return TypedResults.NotFound(); | ||
} | ||
|
||
dbContext.Set<TEntity>().Remove(entity); | ||
await dbContext.SaveChangesAsync(); | ||
|
||
return TypedResults.NoContent(); | ||
} | ||
|
||
public async Task<Results<Ok<List<TDto>>, NotFound>> FilterAsync<TEntity, TDto>(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<List<TEntity>>(cacheKey); | ||
|
||
if (entity != null) | ||
{ | ||
return TypedResults.Ok(mapper.Map<List<TDto>>(entity)); | ||
} | ||
|
||
entity = await dbContext.Set<TEntity>() | ||
.AsNoTracking() | ||
.Where(e => EF.Property<Guid>(e, "FestaId") == festaId) | ||
.ToListAsync(); | ||
|
||
if (entity.Count == 0) | ||
{ | ||
return TypedResults.NotFound(); | ||
} | ||
|
||
await cacheService.SetCacheAsync(cacheKey, entity); | ||
|
||
return TypedResults.Ok(mapper.Map<List<TDto>>(entity)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Results<Ok<List<TDto>>, NotFound>> GetAllAsync<TEntity, TDto>([FromQuery] bool cacheData, DbContext dbContext, ICacheService cacheService, IMapper mapper) | ||
where TEntity : class | ||
where TDto : class; | ||
|
||
Task<Results<Ok<TDto>, NotFound>> GetByIdAsync<TEntity, TDto>(Guid id, DbContext dbContext, ICacheService cacheService, IMapper mapper) | ||
where TEntity : class | ||
where TDto : class; | ||
|
||
Task<Results<Ok<TDto>, BadRequest<string>>> PostAsync<TEntity, TDto, TCreateDto>(TCreateDto createDto, DbContext dbContext, IMapper mapper) | ||
where TEntity : class | ||
where TDto : class; | ||
|
||
Task<Results<Ok<TDto>, NotFound, BadRequest<string>>> UpdateAsync<TEntity, TDto, TEditDto>(Guid id, TEditDto editDto, DbContext dbContext, IMapper mapper) | ||
where TEntity : class | ||
where TDto : class; | ||
|
||
Task<Results<NoContent, NotFound>> DeleteAsync<TEntity>(Guid id, DbContext dbContext) | ||
where TEntity : class; | ||
|
||
Task<Results<Ok<List<TDto>>, NotFound>> FilterAsync<TEntity, TDto>(Guid festaId, DbContext dbContext, ICacheService cacheService, IMapper mapper) | ||
where TEntity : class | ||
where TDto : class; | ||
} |