Premier commit
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SIFinance.Modules.SousTraitants.Dtos;
|
||||
using SIFinance.Shared.Entities;
|
||||
|
||||
namespace SIFinance.Modules.SousTraitants.Endpoints;
|
||||
|
||||
public static class OrganismesEndpoints
|
||||
{
|
||||
public static IEndpointRouteBuilder MapOrganismesEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
var group = app.MapGroup("/api/soustraitants/organismes").WithTags("SousTraitants-Organismes").RequireAuthorization();
|
||||
|
||||
group.MapGet("", async (SIFinanceDbContext db, CancellationToken ct) =>
|
||||
await db.Organismes
|
||||
.AsNoTracking()
|
||||
.Where(o => o.Statut == "ACTIF")
|
||||
.OrderBy(o => o.RaisonSociale)
|
||||
.Select(o => new OrganismeDto(o.Id, o.RaisonSociale, o.Siret, o.Statut))
|
||||
.ToListAsync(ct));
|
||||
|
||||
group.MapGet("/all", async (SIFinanceDbContext db, CancellationToken ct) =>
|
||||
await db.Organismes
|
||||
.AsNoTracking()
|
||||
.OrderBy(o => o.RaisonSociale)
|
||||
.Select(o => new OrganismeDetailDto(o.Id, o.RaisonSociale, o.Siret, o.Email, o.Telephone, o.Adresse, o.Statut, o.DateCreation))
|
||||
.ToListAsync(ct));
|
||||
|
||||
group.MapPost("", async (OrganismeSaveDto input, SIFinanceDbContext db, CancellationToken ct) =>
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input.RaisonSociale))
|
||||
return Results.BadRequest(new { error = "La raison sociale est requise." });
|
||||
|
||||
var organisme = new Organisme
|
||||
{
|
||||
RaisonSociale = input.RaisonSociale.Trim(),
|
||||
Siret = string.IsNullOrWhiteSpace(input.Siret) ? null : input.Siret.Trim(),
|
||||
Email = string.IsNullOrWhiteSpace(input.Email) ? null : input.Email.Trim(),
|
||||
Telephone = string.IsNullOrWhiteSpace(input.Telephone) ? null : input.Telephone.Trim(),
|
||||
Adresse = string.IsNullOrWhiteSpace(input.Adresse) ? null : input.Adresse.Trim(),
|
||||
Statut = input.Statut,
|
||||
DateCreation = DateTime.UtcNow,
|
||||
};
|
||||
|
||||
db.Organismes.Add(organisme);
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
return Results.Created($"/api/soustraitants/organismes/{organisme.Id}",
|
||||
new OrganismeDetailDto(organisme.Id, organisme.RaisonSociale, organisme.Siret, organisme.Email, organisme.Telephone, organisme.Adresse, organisme.Statut, organisme.DateCreation));
|
||||
});
|
||||
|
||||
group.MapPut("/{id:int}", async (int id, OrganismeSaveDto input, SIFinanceDbContext db, CancellationToken ct) =>
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input.RaisonSociale))
|
||||
return Results.BadRequest(new { error = "La raison sociale est requise." });
|
||||
|
||||
var organisme = await db.Organismes.FirstOrDefaultAsync(o => o.Id == id, ct);
|
||||
if (organisme is null) return Results.NotFound();
|
||||
|
||||
organisme.RaisonSociale = input.RaisonSociale.Trim();
|
||||
organisme.Siret = string.IsNullOrWhiteSpace(input.Siret) ? null : input.Siret.Trim();
|
||||
organisme.Email = string.IsNullOrWhiteSpace(input.Email) ? null : input.Email.Trim();
|
||||
organisme.Telephone = string.IsNullOrWhiteSpace(input.Telephone) ? null : input.Telephone.Trim();
|
||||
organisme.Adresse = string.IsNullOrWhiteSpace(input.Adresse) ? null : input.Adresse.Trim();
|
||||
organisme.Statut = input.Statut;
|
||||
|
||||
await db.SaveChangesAsync(ct);
|
||||
return Results.NoContent();
|
||||
});
|
||||
|
||||
group.MapDelete("/{id:int}", async (int id, SIFinanceDbContext db, CancellationToken ct) =>
|
||||
{
|
||||
var organisme = await db.Organismes.FirstOrDefaultAsync(o => o.Id == id, ct);
|
||||
if (organisme is null) return Results.NotFound();
|
||||
|
||||
db.Organismes.Remove(organisme);
|
||||
try
|
||||
{
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
return Results.BadRequest(new { error = "Impossible de supprimer cet organisme : des contrats y sont rattachés." });
|
||||
}
|
||||
|
||||
return Results.NoContent();
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user