Refactored Setting

This commit is contained in:
Cristiano
2025-11-20 15:05:44 +01:00
parent ee9b4675dd
commit 8325aa0768
7 changed files with 53 additions and 47 deletions

View File

@@ -2,10 +2,13 @@
using PyroFetes.DTO.SettingDTO.Request;
using PyroFetes.DTO.SettingDTO.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
namespace PyroFetes.Endpoints.Settings;
public class CreateSettingEndpoint(PyroFetesDbContext database) : Endpoint<CreateSettingDto, GetSettingDto>
public class CreateSettingEndpoint(
SettingsRepository settingsRepository,
AutoMapper.IMapper mapper) : Endpoint<CreateSettingDto, GetSettingDto>
{
public override void Configure()
{
@@ -21,15 +24,8 @@ public class CreateSettingEndpoint(PyroFetesDbContext database) : Endpoint<Creat
Logo = req.Logo
};
database.Settings.Add(setting);
await database.SaveChangesAsync(ct);
await settingsRepository.AddAsync(setting, ct);
GetSettingDto responseDto = new()
{
Id = setting.Id,
ElectronicSignature = setting.ElectronicSignature,
Logo = setting.Logo
};
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(mapper.Map<GetSettingDto>(setting), ct);
}
}

View File

@@ -1,6 +1,8 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Settings;
namespace PyroFetes.Endpoints.Settings;
@@ -9,7 +11,7 @@ public class DeleteSettingRequest
public int Id { get; set; }
}
public class DeleteSettingEndpoint(PyroFetesDbContext database) : Endpoint<DeleteSettingRequest>
public class DeleteSettingEndpoint(SettingsRepository settingsRepository) : Endpoint<DeleteSettingRequest>
{
public override void Configure()
{
@@ -19,7 +21,7 @@ public class DeleteSettingEndpoint(PyroFetesDbContext database) : Endpoint<Delet
public override async Task HandleAsync(DeleteSettingRequest req, CancellationToken ct)
{
Setting? setting = await database.Settings.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
Setting? setting = await settingsRepository.FirstOrDefaultAsync(new GetSettingByIdSpec(req.Id), ct);
if (setting == null)
{
@@ -27,8 +29,7 @@ public class DeleteSettingEndpoint(PyroFetesDbContext database) : Endpoint<Delet
return;
}
database.Settings.Remove(setting);
await database.SaveChangesAsync(ct);
await settingsRepository.DeleteAsync(setting, ct);
await Send.NoContentAsync(ct);
}

View File

@@ -2,6 +2,8 @@
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.SettingDTO.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Settings;
namespace PyroFetes.Endpoints.Settings;
@@ -10,7 +12,9 @@ public class GetSettingRequest
public int Id { get; set; }
}
public class GetSettingEndpoint(PyroFetesDbContext database) : Endpoint<GetSettingRequest, GetSettingDto>
public class GetSettingEndpoint(
SettingsRepository settingsRepository,
AutoMapper.IMapper mapper) : Endpoint<GetSettingRequest, GetSettingDto>
{
public override void Configure()
{
@@ -20,8 +24,7 @@ public class GetSettingEndpoint(PyroFetesDbContext database) : Endpoint<GetSetti
public override async Task HandleAsync(GetSettingRequest req, CancellationToken ct)
{
Setting? setting = await database.Settings
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
Setting? setting = await settingsRepository.FirstOrDefaultAsync(new GetSettingByIdSpec(req.Id), ct);
if (setting == null)
{
@@ -29,12 +32,6 @@ public class GetSettingEndpoint(PyroFetesDbContext database) : Endpoint<GetSetti
return;
}
GetSettingDto responseDto = new()
{
Id = setting.Id,
ElectronicSignature = setting.ElectronicSignature,
Logo = setting.Logo
};
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(mapper.Map<GetSettingDto>(setting), ct);
}
}

View File

@@ -3,10 +3,14 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.SettingDTO.Request;
using PyroFetes.DTO.SettingDTO.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Settings;
namespace PyroFetes.Endpoints.Settings;
public class PatchSettingElectronicSignatureEndpoint(PyroFetesDbContext database) : Endpoint<PatchSettingElectronicSignatureDto, GetSettingDto>
public class PatchSettingElectronicSignatureEndpoint(
SettingsRepository settingsRepository,
AutoMapper.IMapper mapper) : Endpoint<PatchSettingElectronicSignatureDto, GetSettingDto>
{
public override void Configure()
{
@@ -16,8 +20,8 @@ public class PatchSettingElectronicSignatureEndpoint(PyroFetesDbContext database
public override async Task HandleAsync(PatchSettingElectronicSignatureDto req, CancellationToken ct)
{
Setting? setting = await database.Settings.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
Setting? setting = await settingsRepository.FirstOrDefaultAsync(new GetSettingByIdSpec(req.Id), ct);
if (setting == null)
{
await Send.NotFoundAsync(ct);
@@ -25,15 +29,8 @@ public class PatchSettingElectronicSignatureEndpoint(PyroFetesDbContext database
}
setting.ElectronicSignature = req.ElectronicSignature;
await database.SaveChangesAsync(ct);
await settingsRepository.UpdateAsync(setting, ct);
GetSettingDto responseDto = new()
{
Id = setting.Id,
ElectronicSignature = setting.ElectronicSignature,
Logo = setting.Logo
};
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(mapper.Map<GetSettingDto>(setting), ct);
}
}

View File

@@ -3,10 +3,14 @@ using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.SettingDTO.Request;
using PyroFetes.DTO.SettingDTO.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Settings;
namespace PyroFetes.Endpoints.Settings;
public class PatchSettingLogoEndpoint(PyroFetesDbContext database) : Endpoint<PatchSettingLogoDto, GetSettingDto>
public class PatchSettingLogoEndpoint(
SettingsRepository settingsRepository,
AutoMapper.IMapper mapper) : Endpoint<PatchSettingLogoDto, GetSettingDto>
{
public override void Configure()
{
@@ -16,7 +20,7 @@ public class PatchSettingLogoEndpoint(PyroFetesDbContext database) : Endpoint<Pa
public override async Task HandleAsync(PatchSettingLogoDto req, CancellationToken ct)
{
Setting? setting = await database.Settings.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
Setting? setting = await settingsRepository.FirstOrDefaultAsync(new GetSettingByIdSpec(req.Id), ct);
if (setting == null)
{
@@ -25,15 +29,8 @@ public class PatchSettingLogoEndpoint(PyroFetesDbContext database) : Endpoint<Pa
}
setting.Logo = req.Logo;
await database.SaveChangesAsync(ct);
GetSettingDto responseDto = new()
{
Id = setting.Id,
ElectronicSignature = setting.ElectronicSignature,
Logo = setting.Logo
};
await settingsRepository.UpdateAsync(setting, ct);
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(mapper.Map<GetSettingDto>(setting), ct);
}
}

View File

@@ -0,0 +1,5 @@
using PyroFetes.Models;
namespace PyroFetes.Repositories;
public class SettingsRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository<Setting>(pyrofetesContext, mapper);

View File

@@ -0,0 +1,13 @@
using Ardalis.Specification;
using PyroFetes.Models;
namespace PyroFetes.Specifications.Settings;
public sealed class GetSettingByIdSpec : Specification<Setting>
{
public GetSettingByIdSpec(int settingId)
{
Query
.Where(setting => setting.Id == settingId);
}
}