From 01214355948fd05a0248d85b6834645662791069 Mon Sep 17 00:00:00 2001 From: ikuzenkuna Date: Thu, 16 Oct 2025 17:39:36 +0200 Subject: [PATCH] Creating setting's endpoints --- .../SettingDTO/Request/UpdateSettingDto.cs | 8 ++++ .../Setting/CreateSettingEndpoint.cs | 34 +++++++++++++++++ .../Setting/DeleteSettingEndpoint.cs | 35 +++++++++++++++++ .../Setting/GetAllSettingEndpoint.cs | 27 +++++++++++++ .../Endpoints/Setting/GetSettingEndpoint.cs | 38 +++++++++++++++++++ .../Setting/UpdateSettingEndpoint.cs | 37 ++++++++++++++++++ PyroFetes/PyroFetes.csproj | 1 + 7 files changed, 180 insertions(+) create mode 100644 PyroFetes/DTO/SettingDTO/Request/UpdateSettingDto.cs create mode 100644 PyroFetes/Endpoints/Setting/CreateSettingEndpoint.cs create mode 100644 PyroFetes/Endpoints/Setting/DeleteSettingEndpoint.cs create mode 100644 PyroFetes/Endpoints/Setting/GetAllSettingEndpoint.cs create mode 100644 PyroFetes/Endpoints/Setting/GetSettingEndpoint.cs create mode 100644 PyroFetes/Endpoints/Setting/UpdateSettingEndpoint.cs diff --git a/PyroFetes/DTO/SettingDTO/Request/UpdateSettingDto.cs b/PyroFetes/DTO/SettingDTO/Request/UpdateSettingDto.cs new file mode 100644 index 0000000..c092187 --- /dev/null +++ b/PyroFetes/DTO/SettingDTO/Request/UpdateSettingDto.cs @@ -0,0 +1,8 @@ +namespace PyroFetes.DTO.SettingDTO.Request; + +public class UpdateSettingDto +{ + public int Id { get; set; } + public string? ElectronicSignature { get; set; } + public string? Logo { get; set; } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Setting/CreateSettingEndpoint.cs b/PyroFetes/Endpoints/Setting/CreateSettingEndpoint.cs new file mode 100644 index 0000000..74bd43f --- /dev/null +++ b/PyroFetes/Endpoints/Setting/CreateSettingEndpoint.cs @@ -0,0 +1,34 @@ +using Microsoft.EntityFrameworkCore; + +namespace PyroFetes.Endpoints.Setting; +using PyroFetes.DTO.SettingDTO.Request; +using PyroFetes.DTO.SettingDTO.Response; +using FastEndpoints; + +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..1d065ed --- /dev/null +++ b/PyroFetes/Endpoints/Setting/DeleteSettingEndpoint.cs @@ -0,0 +1,35 @@ +namespace PyroFetes.Endpoint.Setting; +using PyroFetes.DTO.SettingDTO.Request; +using PyroFetes.DTO.SettingDTO.Response; +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +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/GetAllSettingEndpoint.cs b/PyroFetes/Endpoints/Setting/GetAllSettingEndpoint.cs new file mode 100644 index 0000000..432f824 --- /dev/null +++ b/PyroFetes/Endpoints/Setting/GetAllSettingEndpoint.cs @@ -0,0 +1,27 @@ +namespace PyroFetes.Endpoints.Setting; +using PyroFetes.DTO.SettingDTO.Request; +using PyroFetes.DTO.SettingDTO.Response; +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +public class GetAllSettingEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest> +{ + public override void Configure() + { + Get("/api/setting"); + } + + public override async Task HandleAsync(CancellationToken ct) + { + var setting = await database.Settings + .Select(setting => new GetSettingDto() + { + Id = setting.Id, + ElectronicSignature = setting.ElectronicSignature, + Logo = setting.Logo + }) + .ToListAsync(ct); + + await Send.OkAsync(setting, 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..6eda487 --- /dev/null +++ b/PyroFetes/Endpoints/Setting/GetSettingEndpoint.cs @@ -0,0 +1,38 @@ +namespace PyroFetes.Endpoint.Setting; +using PyroFetes.DTO.SettingDTO.Request; +using PyroFetes.DTO.SettingDTO.Response; +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +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/UpdateSettingEndpoint.cs b/PyroFetes/Endpoints/Setting/UpdateSettingEndpoint.cs new file mode 100644 index 0000000..e57b39e --- /dev/null +++ b/PyroFetes/Endpoints/Setting/UpdateSettingEndpoint.cs @@ -0,0 +1,37 @@ +namespace PyroFetes.Endpoint.Setting; +using PyroFetes.DTO.SettingDTO.Request; +using PyroFetes.DTO.SettingDTO.Response; +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +public class UpdateSettingEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Get("/api/setting/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(UpdateSettingDto 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; + 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 diff --git a/PyroFetes/PyroFetes.csproj b/PyroFetes/PyroFetes.csproj index 60e4770..1daa61d 100644 --- a/PyroFetes/PyroFetes.csproj +++ b/PyroFetes/PyroFetes.csproj @@ -7,6 +7,7 @@ +