diff --git a/PyroFetes/Endpoints/Settings/CreateSettingEndpoint.cs b/PyroFetes/Endpoints/Settings/CreateSettingEndpoint.cs index 1fd6ec9..0a6f1dd 100644 --- a/PyroFetes/Endpoints/Settings/CreateSettingEndpoint.cs +++ b/PyroFetes/Endpoints/Settings/CreateSettingEndpoint.cs @@ -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 +public class CreateSettingEndpoint( + SettingsRepository settingsRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -21,15 +24,8 @@ public class CreateSettingEndpoint(PyroFetesDbContext database) : Endpoint(setting), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Settings/DeleteSettingEndpoint.cs b/PyroFetes/Endpoints/Settings/DeleteSettingEndpoint.cs index 14fcac3..f608259 100644 --- a/PyroFetes/Endpoints/Settings/DeleteSettingEndpoint.cs +++ b/PyroFetes/Endpoints/Settings/DeleteSettingEndpoint.cs @@ -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 +public class DeleteSettingEndpoint(SettingsRepository settingsRepository) : Endpoint { public override void Configure() { @@ -19,7 +21,7 @@ public class DeleteSettingEndpoint(PyroFetesDbContext database) : Endpoint 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 +public class GetSettingEndpoint( + SettingsRepository settingsRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -20,8 +24,7 @@ public class GetSettingEndpoint(PyroFetesDbContext database) : Endpoint 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(setting), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Settings/PatchSettingElectronicSignatureEndpoint.cs b/PyroFetes/Endpoints/Settings/PatchSettingElectronicSignatureEndpoint.cs index b62776c..4e54c0e 100644 --- a/PyroFetes/Endpoints/Settings/PatchSettingElectronicSignatureEndpoint.cs +++ b/PyroFetes/Endpoints/Settings/PatchSettingElectronicSignatureEndpoint.cs @@ -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 +public class PatchSettingElectronicSignatureEndpoint( + SettingsRepository settingsRepository, + AutoMapper.IMapper mapper) : Endpoint { 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(setting), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Settings/PatchSettingLogoEndpoint.cs b/PyroFetes/Endpoints/Settings/PatchSettingLogoEndpoint.cs index ef974c7..99ff25a 100644 --- a/PyroFetes/Endpoints/Settings/PatchSettingLogoEndpoint.cs +++ b/PyroFetes/Endpoints/Settings/PatchSettingLogoEndpoint.cs @@ -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 +public class PatchSettingLogoEndpoint( + SettingsRepository settingsRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -16,7 +20,7 @@ public class PatchSettingLogoEndpoint(PyroFetesDbContext database) : Endpoint 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(setting), ct); } } \ No newline at end of file diff --git a/PyroFetes/Repositories/SettingsRepository.cs b/PyroFetes/Repositories/SettingsRepository.cs new file mode 100644 index 0000000..5f4050b --- /dev/null +++ b/PyroFetes/Repositories/SettingsRepository.cs @@ -0,0 +1,5 @@ +using PyroFetes.Models; + +namespace PyroFetes.Repositories; + +public class SettingsRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository(pyrofetesContext, mapper); \ No newline at end of file diff --git a/PyroFetes/Specifications/Settings/GetSettingByIdSpec.cs b/PyroFetes/Specifications/Settings/GetSettingByIdSpec.cs new file mode 100644 index 0000000..cc786e4 --- /dev/null +++ b/PyroFetes/Specifications/Settings/GetSettingByIdSpec.cs @@ -0,0 +1,13 @@ +using Ardalis.Specification; +using PyroFetes.Models; + +namespace PyroFetes.Specifications.Settings; + +public sealed class GetSettingByIdSpec : Specification +{ + public GetSettingByIdSpec(int settingId) + { + Query + .Where(setting => setting.Id == settingId); + } +} \ No newline at end of file