Refactor all code
This commit is contained in:
@@ -1,14 +1,11 @@
|
||||
using FastEndpoints;
|
||||
using PyroFetes.DTO.SettingDTO.Request;
|
||||
using PyroFetes.DTO.SettingDTO.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
|
||||
namespace PyroFetes.Endpoints.Settings;
|
||||
|
||||
public class CreateSettingEndpoint(
|
||||
SettingsRepository settingsRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<CreateSettingDto, GetSettingDto>
|
||||
public class CreateSettingEndpoint(SettingsRepository settingsRepository) : Endpoint<CreateSettingDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -18,14 +15,21 @@ public class CreateSettingEndpoint(
|
||||
|
||||
public override async Task HandleAsync(CreateSettingDto req, CancellationToken ct)
|
||||
{
|
||||
Setting setting = new Setting()
|
||||
{
|
||||
ElectronicSignature = req.ElectronicSignature,
|
||||
Logo = req.Logo
|
||||
};
|
||||
|
||||
await settingsRepository.AddAsync(setting, ct);
|
||||
// Encodage en base64
|
||||
using MemoryStream memoryStream = new();
|
||||
if (req.Logo != null) await req.Logo.CopyToAsync(memoryStream, ct);
|
||||
byte[] logoBytes = memoryStream.ToArray();
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetSettingDto>(setting), ct);
|
||||
if (req.ElectronicSignature != null) await req.ElectronicSignature.CopyToAsync(memoryStream, ct);
|
||||
byte[] signatureBytes = memoryStream.ToArray();
|
||||
|
||||
Setting setting = new()
|
||||
{
|
||||
ElectronicSignature = Convert.ToBase64String(signatureBytes),
|
||||
Logo = Convert.ToBase64String(logoBytes)
|
||||
};
|
||||
|
||||
await settingsRepository.AddAsync(setting, ct);
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.Settings;
|
||||
@@ -15,22 +14,21 @@ public class DeleteSettingEndpoint(SettingsRepository settingsRepository) : Endp
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/settings/{@Id}", x => new {x.Id});
|
||||
Delete("/settings/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
|
||||
public override async Task HandleAsync(DeleteSettingRequest req, CancellationToken ct)
|
||||
{
|
||||
Setting? setting = await settingsRepository.FirstOrDefaultAsync(new GetSettingByIdSpec(req.Id), ct);
|
||||
Setting? setting = await settingsRepository.SingleOrDefaultAsync(new GetSettingByIdSpec(req.Id), ct);
|
||||
|
||||
if (setting == null)
|
||||
if (setting is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
await settingsRepository.DeleteAsync(setting, ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.SettingDTO.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
@@ -18,15 +17,15 @@ public class GetSettingEndpoint(
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/settings/{@Id}", x => new {x.Id});
|
||||
Get("/settings/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
|
||||
public override async Task HandleAsync(GetSettingRequest req, CancellationToken ct)
|
||||
{
|
||||
Setting? setting = await settingsRepository.FirstOrDefaultAsync(new GetSettingByIdSpec(req.Id), ct);
|
||||
Setting? setting = await settingsRepository.SingleOrDefaultAsync(new GetSettingByIdSpec(req.Id), ct);
|
||||
|
||||
if (setting == null)
|
||||
if (setting is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
|
||||
@@ -1,36 +1,37 @@
|
||||
using FastEndpoints;
|
||||
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(
|
||||
SettingsRepository settingsRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<PatchSettingElectronicSignatureDto, GetSettingDto>
|
||||
public class PatchSettingElectronicSignatureEndpoint(SettingsRepository settingsRepository) : Endpoint<PatchSettingElectronicSignatureDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Patch("/settings/{@Id}/ElectronicSignature", x => new {x.Id});
|
||||
Patch("/settings/{@Id}/ElectronicSignature", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
|
||||
public override async Task HandleAsync(PatchSettingElectronicSignatureDto req, CancellationToken ct)
|
||||
{
|
||||
Setting? setting = await settingsRepository.FirstOrDefaultAsync(new GetSettingByIdSpec(req.Id), ct);
|
||||
|
||||
if (setting == null)
|
||||
Setting? setting = await settingsRepository.SingleOrDefaultAsync(new GetSettingByIdSpec(req.Id), ct);
|
||||
|
||||
if (setting is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
setting.ElectronicSignature = req.ElectronicSignature;
|
||||
await settingsRepository.UpdateAsync(setting, ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetSettingDto>(setting), ct);
|
||||
// Encodage en base64
|
||||
using MemoryStream memoryStream = new();
|
||||
if (req.ElectronicSignature != null) await req.ElectronicSignature.CopyToAsync(memoryStream, ct);
|
||||
byte[] signatureBytes = memoryStream.ToArray();
|
||||
|
||||
setting.ElectronicSignature = Convert.ToBase64String(signatureBytes);
|
||||
|
||||
await settingsRepository.SaveChangesAsync(ct);
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,37 @@
|
||||
using FastEndpoints;
|
||||
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(
|
||||
SettingsRepository settingsRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<PatchSettingLogoDto, GetSettingDto>
|
||||
public class PatchSettingLogoEndpoint(SettingsRepository settingsRepository) : Endpoint<PatchSettingLogoDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Patch("/settings/{@Id}/logo", x => new {x.Id});
|
||||
Patch("/settings/{@Id}/logo", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
|
||||
public override async Task HandleAsync(PatchSettingLogoDto req, CancellationToken ct)
|
||||
{
|
||||
Setting? setting = await settingsRepository.FirstOrDefaultAsync(new GetSettingByIdSpec(req.Id), ct);
|
||||
Setting? setting = await settingsRepository.SingleOrDefaultAsync(new GetSettingByIdSpec(req.Id), ct);
|
||||
|
||||
if (setting == null)
|
||||
if (setting is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
setting.Logo = req.Logo;
|
||||
await settingsRepository.UpdateAsync(setting, ct);
|
||||
// Encodage en base64
|
||||
using MemoryStream memoryStream = new();
|
||||
if (req.Logo != null) await req.Logo.CopyToAsync(memoryStream, ct);
|
||||
byte[] logoBytes = memoryStream.ToArray();
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetSettingDto>(setting), ct);
|
||||
setting.Logo = Convert.ToBase64String(logoBytes);
|
||||
|
||||
await settingsRepository.SaveChangesAsync(ct);
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user