Premier commit
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SIFinance.Modules.SousTraitants.Dtos;
|
||||
using SIFinance.Shared;
|
||||
using SIFinance.Shared.Entities;
|
||||
|
||||
namespace SIFinance.Modules.SousTraitants.Endpoints;
|
||||
|
||||
public static class ContratsPrestatairesEndpoints
|
||||
{
|
||||
public static IEndpointRouteBuilder MapContratsPrestatairesEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
var group = app.MapGroup("/api/soustraitants/contrats").WithTags("SousTraitants-Contrats").RequireAuthorization();
|
||||
|
||||
group.MapGet("", async (SIFinanceDbContext db, CancellationToken ct) =>
|
||||
await db.ContratsPrestataires
|
||||
.AsNoTracking()
|
||||
.Include(c => c.Organisme)
|
||||
.OrderByDescending(c => c.DateCreation)
|
||||
.Select(c => ToDto(c))
|
||||
.ToListAsync(ct));
|
||||
|
||||
group.MapPost("", async (ContratPrestataireCreateDto input, SIFinanceDbContext db, CurrentUserService currentUser, CancellationToken ct) =>
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input.Objet))
|
||||
return Results.BadRequest(new { error = "L'objet du contrat est requis." });
|
||||
if (input.DateFin < input.DateDebut)
|
||||
return Results.BadRequest(new { error = "La date de fin doit être postérieure à la date de début." });
|
||||
|
||||
var contrat = new ContratPrestataire
|
||||
{
|
||||
OrganismeId = input.OrganismeId,
|
||||
Reference = await GenererReferenceAsync(db, ct),
|
||||
Objet = input.Objet.Trim(),
|
||||
DateSignature = input.DateSignature,
|
||||
DateDebut = input.DateDebut,
|
||||
DateFin = input.DateFin,
|
||||
MontantTotal = input.MontantTotal,
|
||||
Statut = "ACTIF",
|
||||
SaisieParId = await currentUser.GetOrCreateCurrentUserIdAsync(ct),
|
||||
DateCreation = DateTime.UtcNow,
|
||||
};
|
||||
|
||||
db.ContratsPrestataires.Add(contrat);
|
||||
try
|
||||
{
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
return Results.BadRequest(new { error = "Impossible d'enregistrer le contrat — vérifiez l'organisme.", detail = ex.InnerException?.Message ?? ex.Message });
|
||||
}
|
||||
|
||||
await db.Entry(contrat).Reference(c => c.Organisme).LoadAsync(ct);
|
||||
return Results.Created($"/api/soustraitants/contrats/{contrat.Id}", ToDto(contrat));
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
private static async Task<string> GenererReferenceAsync(SIFinanceDbContext db, CancellationToken ct)
|
||||
{
|
||||
var prefix = $"ST-CONT-{DateTime.UtcNow.Year}-";
|
||||
var last = await db.ContratsPrestataires
|
||||
.Where(c => c.Reference.StartsWith(prefix))
|
||||
.OrderByDescending(c => c.Reference)
|
||||
.Select(c => c.Reference)
|
||||
.FirstOrDefaultAsync(ct);
|
||||
|
||||
var nextSeq = 1;
|
||||
if (last is not null && int.TryParse(last.AsSpan(prefix.Length), out var lastSeq))
|
||||
nextSeq = lastSeq + 1;
|
||||
|
||||
return $"{prefix}{nextSeq:D3}";
|
||||
}
|
||||
|
||||
private static ContratPrestataireDto ToDto(ContratPrestataire c) => new(
|
||||
c.Id, c.OrganismeId, c.Organisme.RaisonSociale, c.Reference, c.Objet,
|
||||
c.DateSignature, c.DateDebut, c.DateFin, c.MontantTotal, c.Statut);
|
||||
}
|
||||
@@ -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