diff --git a/PyroFetes/Endpoints/Setting/CreateSettingEndpoint.cs b/PyroFetes/Endpoints/Setting/CreateSettingEndpoint.cs new file mode 100644 index 0000000..11dcedd --- /dev/null +++ b/PyroFetes/Endpoints/Setting/CreateSettingEndpoint.cs @@ -0,0 +1,32 @@ +using PyroFetes.DTO.SettingDTO.Request; +using PyroFetes.DTO.SettingDTO.Response; +using FastEndpoints; +namespace PyroFetes.Endpoints.Setting; + +public class CreateSettingEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Post("/api/setting"); + } + + public override async Task HandleAsync(CreateSettingDto req, CancellationToken ct) + { + var setting = new Models.Setting() + { + ElectronicSignature = req.ElectronicSignature, + Logo = req.Logo + }; + + database.Settings.Add(setting); + await database.SaveChangesAsync(ct); + + GetSettingDto responseDto = new() + { + Id = setting.Id, + ElectronicSignature = setting.ElectronicSignature, + Logo = setting.Logo + }; + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Setting/DeleteSettingEndpoint.cs b/PyroFetes/Endpoints/Setting/DeleteSettingEndpoint.cs new file mode 100644 index 0000000..92bfba7 --- /dev/null +++ b/PyroFetes/Endpoints/Setting/DeleteSettingEndpoint.cs @@ -0,0 +1,33 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace PyroFetes.Endpoints.Setting; + +public class DeleteSettingRequest +{ + public int Id { get; set; } +} + +public class DeleteSettingEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Delete("/api/setting/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(DeleteSettingRequest req, CancellationToken ct) + { + var setting = await database.Settings.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (setting == null) + { + await Send.NotFoundAsync(ct); + return; + } + + database.Settings.Remove(setting); + await database.SaveChangesAsync(ct); + + await Send.NoContentAsync(ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Setting/GetSettingEndpoint.cs b/PyroFetes/Endpoints/Setting/GetSettingEndpoint.cs new file mode 100644 index 0000000..9ebd72b --- /dev/null +++ b/PyroFetes/Endpoints/Setting/GetSettingEndpoint.cs @@ -0,0 +1,38 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.SettingDTO.Response; + +namespace PyroFetes.Endpoints.Setting; + +public class GetSettingRequest +{ + public int Id { get; set; } +} + +public class GetSettingEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Get("/api/setting/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(GetSettingRequest req, CancellationToken ct) + { + var setting = await database.Settings + .SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (setting == null) + { + await Send.NotFoundAsync(ct); + return; + } + + GetSettingDto responseDto = new() + { + Id = setting.Id, + ElectronicSignature = setting.ElectronicSignature, + Logo = setting.Logo + }; + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Setting/PatchSettingElectronicSignatureEndpoint.cs b/PyroFetes/Endpoints/Setting/PatchSettingElectronicSignatureEndpoint.cs new file mode 100644 index 0000000..c54d915 --- /dev/null +++ b/PyroFetes/Endpoints/Setting/PatchSettingElectronicSignatureEndpoint.cs @@ -0,0 +1,37 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.SettingDTO.Request; +using PyroFetes.DTO.SettingDTO.Response; + +namespace PyroFetes.Endpoints.Setting; + +public class PatchSettingElectronicSignatureEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Get("/api/setting/{@Id}/ElectronicSignature", x => new {x.Id}); + } + + public override async Task HandleAsync(PatchSettingElectronicSignatureDto req, CancellationToken ct) + { + var setting = await database.Settings.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (setting == null) + { + await Send.NotFoundAsync(ct); + return; + } + + setting.ElectronicSignature = req.ElectronicSignature; + await database.SaveChangesAsync(ct); + + GetSettingDto responseDto = new() + { + Id = setting.Id, + ElectronicSignature = setting.ElectronicSignature, + Logo = setting.Logo + }; + + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Setting/PatchSettingLogoEndpoint.cs b/PyroFetes/Endpoints/Setting/PatchSettingLogoEndpoint.cs new file mode 100644 index 0000000..4bfe5c1 --- /dev/null +++ b/PyroFetes/Endpoints/Setting/PatchSettingLogoEndpoint.cs @@ -0,0 +1,37 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.SettingDTO.Request; +using PyroFetes.DTO.SettingDTO.Response; + +namespace PyroFetes.Endpoints.Setting; + +public class PatchSettingLogoEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Get("/api/setting/{@Id}/Logo", x => new {x.Id}); + } + + public override async Task HandleAsync(PatchSettingLogoDto req, CancellationToken ct) + { + var setting = await database.Settings.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (setting == null) + { + await Send.NotFoundAsync(ct); + return; + } + + setting.Logo = req.Logo; + await database.SaveChangesAsync(ct); + + GetSettingDto responseDto = new() + { + Id = setting.Id, + ElectronicSignature = setting.ElectronicSignature, + Logo = setting.Logo + }; + + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file