Finalisation endpoints

This commit is contained in:
2025-11-13 15:11:34 +01:00
parent 5c12a45ae6
commit 3a09bfc8ad
20 changed files with 922 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
using FastEndpoints;
using PyroFetes.DTO.Show.Request;
using PyroFetes.DTO.Show.Response;
namespace PyroFetes.Endpoints.Show;
public class CreateShowEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<CreateShowDto, ReadShowDto>
{
public override void Configure()
{
Post("/api/shows");
AllowAnonymous();
}
public override async Task HandleAsync(CreateShowDto req, CancellationToken ct)
{
var show = new PyroFetes.Models.Show
{
Name = req.Name,
Place = req.Place,
Description = req.Description,
PyrotechnicImplementationPlan = req.PyrotechnicImplementationPlan,
Date = req.Date.HasValue ? DateOnly.FromDateTime(req.Date.Value) : null
};
pyroFetesDbContext.Shows.Add(show);
await pyroFetesDbContext.SaveChangesAsync(ct);
var result = new ReadShowDto
{
Id = show.Id,
Name = show.Name,
Place = show.Place,
Description = show.Description,
PyrotechnicImplementationPlan = show.PyrotechnicImplementationPlan,
Date = show.Date.HasValue ? show.Date.Value.ToDateTime(TimeOnly.MinValue) : null
};
await Send.OkAsync(result, ct);
}
}

View File

@@ -0,0 +1,53 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Show.Request;
namespace PyroFetes.Endpoints.Show;
public class DeleteShowEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<IdShowDto>
{
public override void Configure()
{
Delete("/api/shows/{Id}");
AllowAnonymous();
}
public override async Task HandleAsync(IdShowDto req, CancellationToken ct)
{
if (!req.Id.HasValue)
{
await Send.NotFoundAsync(ct);
return;
}
var show = await pyroFetesDbContext.Shows
.Include(s => s.ShowTrucks)
.Include(s => s.ShowStaffs)
.Include(s => s.SoundTimecodes)
.Include(s => s.ProductTimecodes)
.Include(s => s.Contracts)
.Include(s => s.ShowMaterials)
.FirstOrDefaultAsync(s => s.Id == req.Id.Value, ct);
if (show is null)
{
await Send.NotFoundAsync(ct);
return;
}
// Supprimer les relations associées
if (show.ShowTrucks != null && show.ShowTrucks.Any())
{
pyroFetesDbContext.ShowTrucks.RemoveRange(show.ShowTrucks);
}
// Note: Les autres relations (ShowStaffs, SoundTimecodes, etc.) devront aussi être gérées
// en fonction de votre modèle de données et de vos règles métier
// Pour l'instant, je laisse juste ShowTrucks comme exemple
pyroFetesDbContext.Shows.Remove(show);
await pyroFetesDbContext.SaveChangesAsync(ct);
await Send.OkAsync(ct);
}
}

View File

@@ -0,0 +1,31 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Show.Response;
namespace PyroFetes.Endpoints.Show;
public class GetAllShowsEndpoint(PyroFetesDbContext pyroFetesDbContext) : EndpointWithoutRequest<List<ReadShowDto>>
{
public override void Configure()
{
Get("/api/shows");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
var shows = await pyroFetesDbContext.Shows
.Select(s => new ReadShowDto
{
Id = s.Id,
Name = s.Name,
Place = s.Place,
Description = s.Description,
PyrotechnicImplementationPlan = s.PyrotechnicImplementationPlan,
Date = s.Date.HasValue ? s.Date.Value.ToDateTime(TimeOnly.MinValue) : null
})
.ToListAsync(ct);
await Send.OkAsync(shows, ct);
}
}

View File

@@ -0,0 +1,45 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Show.Request;
using PyroFetes.DTO.Show.Response;
namespace PyroFetes.Endpoints.Show;
public class GetShowEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<IdShowDto, ReadShowDto>
{
public override void Configure()
{
Get("/api/shows/{Id}");
AllowAnonymous();
}
public override async Task HandleAsync(IdShowDto req, CancellationToken ct)
{
if (!req.Id.HasValue)
{
await Send.NotFoundAsync(ct);
return;
}
var show = await pyroFetesDbContext.Shows
.Where(s => s.Id == req.Id.Value)
.Select(s => new ReadShowDto
{
Id = s.Id,
Name = s.Name,
Place = s.Place,
Description = s.Description,
PyrotechnicImplementationPlan = s.PyrotechnicImplementationPlan,
Date = s.Date.HasValue ? s.Date.Value.ToDateTime(TimeOnly.MinValue) : null
})
.FirstOrDefaultAsync(ct);
if (show is null)
{
await Send.NotFoundAsync(ct);
return;
}
await Send.OkAsync(show, ct);
}
}

View File

@@ -0,0 +1,57 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Show.Request;
using PyroFetes.DTO.Show.Response;
namespace PyroFetes.Endpoints.Show;
public class UpdateShowEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<UpdateShowDto, ReadShowDto>
{
public override void Configure()
{
Put("/api/shows/{Id}");
AllowAnonymous();
}
public override async Task HandleAsync(UpdateShowDto req, CancellationToken ct)
{
if (!req.Id.HasValue)
{
await Send.NotFoundAsync(ct);
return;
}
var show = await pyroFetesDbContext.Shows
.FirstOrDefaultAsync(s => s.Id == req.Id.Value, ct);
if (show is null)
{
await Send.NotFoundAsync(ct);
return;
}
show.Name = req.Name ?? show.Name;
show.Place = req.Place ?? show.Place;
show.Description = req.Description ?? show.Description;
show.PyrotechnicImplementationPlan = req.PyrotechnicImplementationPlan ?? show.PyrotechnicImplementationPlan;
if (req.Date.HasValue)
{
show.Date = DateOnly.FromDateTime(req.Date.Value);
}
await pyroFetesDbContext.SaveChangesAsync(ct);
var result = new ReadShowDto
{
Id = show.Id,
Name = show.Name,
Place = show.Place,
Description = show.Description,
PyrotechnicImplementationPlan = show.PyrotechnicImplementationPlan,
Date = show.Date.HasValue ? show.Date.Value.ToDateTime(TimeOnly.MinValue) : null
};
await Send.OkAsync(result, ct);
}
}

View File

@@ -0,0 +1,47 @@
using FastEndpoints;
using PyroFetes.DTO.Sound.Request;
using PyroFetes.DTO.Sound.Response;
namespace PyroFetes.Endpoints.Sound;
public class CreateSoundEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<CreateSoundDto, ReadSoundDto>
{
public override void Configure()
{
Post("/api/sounds");
AllowAnonymous();
}
public override async Task HandleAsync(CreateSoundDto req, CancellationToken ct)
{
var sound = new PyroFetes.Models.Sound
{
Name = req.Name ?? string.Empty,
Type = req.Type,
Artist = req.Artist,
Duration = int.TryParse(req.Duration, out var duration) ? duration : null,
Kind = req.Kind,
Format = req.Format,
CreationDate = req.CreationDate,
SoundCategoryId = int.TryParse(req.SoundCategoryId, out var categoryId) ? categoryId : 0
};
pyroFetesDbContext.Sounds.Add(sound);
await pyroFetesDbContext.SaveChangesAsync(ct);
var result = new ReadSoundDto
{
Id = sound.Id,
Name = sound.Name,
Type = sound.Type,
Artist = sound.Artist,
Duration = sound.Duration?.ToString(),
Kind = sound.Kind,
Format = sound.Format,
CreationDate = sound.CreationDate,
SoundCategoryId = sound.SoundCategoryId.ToString()
};
await Send.OkAsync(result, ct);
}
}

View File

@@ -0,0 +1,44 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Sound.Request;
namespace PyroFetes.Endpoints.Sound;
public class DeleteSoundEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<IdSoundto>
{
public override void Configure()
{
Delete("/api/sounds/{Id}");
AllowAnonymous();
}
public override async Task HandleAsync(IdSoundto req, CancellationToken ct)
{
if (!req.Id.HasValue)
{
await Send.NotFoundAsync(ct);
return;
}
var sound = await pyroFetesDbContext.Sounds
.Include(s => s.SoundTimecodes)
.FirstOrDefaultAsync(s => s.Id == req.Id.Value, ct);
if (sound is null)
{
await Send.NotFoundAsync(ct);
return;
}
// Supprimer les relations SoundTimecode associées si nécessaire
if (sound.SoundTimecodes != null && sound.SoundTimecodes.Any())
{
pyroFetesDbContext.SoundTimecodes.RemoveRange(sound.SoundTimecodes);
}
pyroFetesDbContext.Sounds.Remove(sound);
await pyroFetesDbContext.SaveChangesAsync(ct);
await Send.OkAsync(ct);
}
}

View File

@@ -0,0 +1,34 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Sound.Response;
namespace PyroFetes.Endpoints.Sound;
public class GetAllSoundsEndpoint(PyroFetesDbContext pyroFetesDbContext) : EndpointWithoutRequest<List<ReadSoundDto>>
{
public override void Configure()
{
Get("/api/sounds");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
var sounds = await pyroFetesDbContext.Sounds
.Select(s => new ReadSoundDto
{
Id = s.Id,
Name = s.Name,
Type = s.Type,
Artist = s.Artist,
Duration = s.Duration != null ? s.Duration.Value.ToString() : null,
Kind = s.Kind,
Format = s.Format,
CreationDate = s.CreationDate,
SoundCategoryId = s.SoundCategoryId.ToString()
})
.ToListAsync(ct);
await Send.OkAsync(sounds, ct);
}
}

View File

@@ -0,0 +1,48 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Sound.Request;
using PyroFetes.DTO.Sound.Response;
namespace PyroFetes.Endpoints.Sound;
public class GetSoundEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<IdSoundto, ReadSoundDto>
{
public override void Configure()
{
Get("/api/sounds/{Id}");
AllowAnonymous();
}
public override async Task HandleAsync(IdSoundto req, CancellationToken ct)
{
if (!req.Id.HasValue)
{
await Send.NotFoundAsync(ct);
return;
}
var sound = await pyroFetesDbContext.Sounds
.Where(s => s.Id == req.Id.Value)
.Select(s => new ReadSoundDto
{
Id = s.Id,
Name = s.Name,
Type = s.Type,
Artist = s.Artist,
Duration = s.Duration != null ? s.Duration.Value.ToString() : null,
Kind = s.Kind,
Format = s.Format,
CreationDate = s.CreationDate,
SoundCategoryId = s.SoundCategoryId.ToString()
})
.FirstOrDefaultAsync(ct);
if (sound is null)
{
await Send.NotFoundAsync(ct);
return;
}
await Send.OkAsync(sound, ct);
}
}

View File

@@ -0,0 +1,68 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Sound.Request;
using PyroFetes.DTO.Sound.Response;
namespace PyroFetes.Endpoints.Sound;
public class UpdateSoundEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<UpdateSoundDto, ReadSoundDto>
{
public override void Configure()
{
Put("/api/sounds/{Id}");
AllowAnonymous();
}
public override async Task HandleAsync(UpdateSoundDto req, CancellationToken ct)
{
if (!req.Id.HasValue)
{
await Send.NotFoundAsync(ct);
return;
}
var sound = await pyroFetesDbContext.Sounds
.FirstOrDefaultAsync(s => s.Id == req.Id.Value, ct);
if (sound is null)
{
await Send.NotFoundAsync(ct);
return;
}
sound.Name = req.Name ?? sound.Name;
sound.Type = req.Type ?? sound.Type;
sound.Artist = req.Artist ?? sound.Artist;
if (int.TryParse(req.Duration, out var duration))
{
sound.Duration = duration;
}
sound.Kind = req.Kind ?? sound.Kind;
sound.Format = req.Format ?? sound.Format;
sound.CreationDate = req.CreationDate ?? sound.CreationDate;
if (int.TryParse(req.SoundCategoryId, out var categoryId))
{
sound.SoundCategoryId = categoryId;
}
await pyroFetesDbContext.SaveChangesAsync(ct);
var result = new ReadSoundDto
{
Id = sound.Id,
Name = sound.Name,
Type = sound.Type,
Artist = sound.Artist,
Duration = sound.Duration?.ToString(),
Kind = sound.Kind,
Format = sound.Format,
CreationDate = sound.CreationDate,
SoundCategoryId = sound.SoundCategoryId.ToString()
};
await Send.OkAsync(result, ct);
}
}

View File

@@ -0,0 +1,38 @@
using FastEndpoints;
using PyroFetes.DTO.SoundTimecode.Request;
using PyroFetes.DTO.SoundTimecode.Response;
namespace PyroFetes.Endpoints.SoundTimecode;
public class CreateSoundTimecodeEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<CreateSoundTimecodeDto, ReadSoundTimecodeDto>
{
public override void Configure()
{
Post("/api/soundtimecodes");
AllowAnonymous();
}
public override async Task HandleAsync(CreateSoundTimecodeDto req, CancellationToken ct)
{
var soundTimecode = new PyroFetes.Models.SoundTimecode
{
ShowId = req.ShowId,
SoundId = req.SoundId,
Start = req.Start,
End = req.End
};
pyroFetesDbContext.SoundTimecodes.Add(soundTimecode);
await pyroFetesDbContext.SaveChangesAsync(ct);
var result = new ReadSoundTimecodeDto
{
ShowId = soundTimecode.ShowId,
SoundId = soundTimecode.SoundId,
Start = (int)soundTimecode.Start,
End = (int)soundTimecode.End
};
await Send.OkAsync(result, ct);
}
}

View File

@@ -0,0 +1,37 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.SoundTimecode;
// DTO pour la route avec clé composite
public class DeleteSoundTimecodeRequest
{
public int ShowId { get; set; }
public int SoundId { get; set; }
}
public class DeleteSoundTimecodeEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<DeleteSoundTimecodeRequest>
{
public override void Configure()
{
Delete("/api/soundtimecodes/{ShowId}/{SoundId}");
AllowAnonymous();
}
public override async Task HandleAsync(DeleteSoundTimecodeRequest req, CancellationToken ct)
{
var soundTimecode = await pyroFetesDbContext.SoundTimecodes
.FirstOrDefaultAsync(st => st.ShowId == req.ShowId && st.SoundId == req.SoundId, ct);
if (soundTimecode is null)
{
await Send.NotFoundAsync(ct);
return;
}
pyroFetesDbContext.SoundTimecodes.Remove(soundTimecode);
await pyroFetesDbContext.SaveChangesAsync(ct);
await Send.OkAsync(ct);
}
}

View File

@@ -0,0 +1,29 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.SoundTimecode.Response;
namespace PyroFetes.Endpoints.SoundTimecode;
public class GetAllSoundTimecodesEndpoint(PyroFetesDbContext pyroFetesDbContext) : EndpointWithoutRequest<List<ReadSoundTimecodeDto>>
{
public override void Configure()
{
Get("/api/soundtimecodes");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
var soundTimecodes = await pyroFetesDbContext.SoundTimecodes
.Select(st => new ReadSoundTimecodeDto
{
ShowId = st.ShowId,
SoundId = st.SoundId,
Start = (int)st.Start,
End = (int)st.End
})
.ToListAsync(ct);
await Send.OkAsync(soundTimecodes, ct);
}
}

View File

@@ -0,0 +1,43 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.SoundTimecode.Response;
namespace PyroFetes.Endpoints.SoundTimecode;
// DTO pour la route avec clé composite
public class GetSoundTimecodeRequest
{
public int ShowId { get; set; }
public int SoundId { get; set; }
}
public class GetSoundTimecodeEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<GetSoundTimecodeRequest, ReadSoundTimecodeDto>
{
public override void Configure()
{
Get("/api/soundtimecodes/{ShowId}/{SoundId}");
AllowAnonymous();
}
public override async Task HandleAsync(GetSoundTimecodeRequest req, CancellationToken ct)
{
var soundTimecode = await pyroFetesDbContext.SoundTimecodes
.Where(st => st.ShowId == req.ShowId && st.SoundId == req.SoundId)
.Select(st => new ReadSoundTimecodeDto
{
ShowId = st.ShowId,
SoundId = st.SoundId,
Start = (int)st.Start,
End = (int)st.End
})
.FirstOrDefaultAsync(ct);
if (soundTimecode is null)
{
await Send.NotFoundAsync(ct);
return;
}
await Send.OkAsync(soundTimecode, ct);
}
}

View File

@@ -0,0 +1,51 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.SoundTimecode.Request;
using PyroFetes.DTO.SoundTimecode.Response;
namespace PyroFetes.Endpoints.SoundTimecode;
// DTO pour la route avec clé composite
public class UpdateSoundTimecodeRequest
{
public int ShowId { get; set; }
public int SoundId { get; set; }
public int Start { get; set; }
public int End { get; set; }
}
public class UpdateSoundTimecodeEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<UpdateSoundTimecodeRequest, ReadSoundTimecodeDto>
{
public override void Configure()
{
Put("/api/soundtimecodes/{ShowId}/{SoundId}");
AllowAnonymous();
}
public override async Task HandleAsync(UpdateSoundTimecodeRequest req, CancellationToken ct)
{
var soundTimecode = await pyroFetesDbContext.SoundTimecodes
.FirstOrDefaultAsync(st => st.ShowId == req.ShowId && st.SoundId == req.SoundId, ct);
if (soundTimecode is null)
{
await Send.NotFoundAsync(ct);
return;
}
soundTimecode.Start = req.Start;
soundTimecode.End = req.End;
await pyroFetesDbContext.SaveChangesAsync(ct);
var result = new ReadSoundTimecodeDto
{
ShowId = soundTimecode.ShowId,
SoundId = soundTimecode.SoundId,
Start = (int)soundTimecode.Start,
End = (int)soundTimecode.End
};
await Send.OkAsync(result, ct);
}
}

View File

@@ -0,0 +1,52 @@
using FastEndpoints;
using PyroFetes.DTO.Truck.Request;
using PyroFetes.DTO.Truck.Response;
namespace PyroFetes.Endpoints.Truck;
public class CreateTruckEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<CreateTruckDto, ReadTruckDto>
{
public override void Configure()
{
Post("/api/trucks");
AllowAnonymous();
}
public override async Task HandleAsync(CreateTruckDto req, CancellationToken ct)
{
var truck = new PyroFetes.Models.Truck
{
Type = req.Type ?? string.Empty,
MaxExplosiveCapacity = req.MaxExplosiveCapacity,
Sizes = req.Sizes,
Status = req.Status
};
pyroFetesDbContext.Trucks.Add(truck);
await pyroFetesDbContext.SaveChangesAsync(ct);
// Ajouter la relation ShowTruck si ShowId est fourni
if (req.ShowId.HasValue && req.ShowId.Value != 0)
{
var showTruck = new PyroFetes.Models.ShowTruck
{
TruckId = truck.Id,
ShowId = req.ShowId.Value
};
pyroFetesDbContext.ShowTrucks.Add(showTruck);
await pyroFetesDbContext.SaveChangesAsync(ct);
}
var result = new ReadTruckDto
{
Id = truck.Id,
Type = truck.Type,
MaxExplosiveCapacity = truck.MaxExplosiveCapacity,
Sizes = truck.Sizes,
Statut = truck.Status,
ShowId = req.ShowId
};
await Send.OkAsync(result, ct);
}
}

View File

@@ -0,0 +1,45 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Truck.Request;
namespace PyroFetes.Endpoints.Truck;
public class DeleteTruckEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<IdTruckDto>
{
public override void Configure()
{
Delete("/api/trucks/{Id}");
AllowAnonymous();
}
public override async Task HandleAsync(IdTruckDto req, CancellationToken ct)
{
if (!req.Id.HasValue)
{
await Send.NotFoundAsync(ct);
return;
}
var truck = await pyroFetesDbContext.Trucks
.Include(t => t.ShowTrucks)
.FirstOrDefaultAsync(t => t.Id == req.Id.Value, ct);
if (truck is null)
{
await Send.NotFoundAsync(ct);
return;
}
// Supprimer les relations ShowTruck associées
if (truck.ShowTrucks != null && truck.ShowTrucks.Any())
{
pyroFetesDbContext.ShowTrucks.RemoveRange(truck.ShowTrucks);
}
// Supprimer le truck
pyroFetesDbContext.Trucks.Remove(truck);
await pyroFetesDbContext.SaveChangesAsync(ct);
await Send.OkAsync(ct);
}
}

View File

@@ -0,0 +1,32 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Truck.Response;
namespace PyroFetes.Endpoints.Truck;
public class GetAllTrucksEndpoint(PyroFetesDbContext pyroFetesDbContext) : EndpointWithoutRequest<List<ReadTruckDto>>
{
public override void Configure()
{
Get("/api/trucks");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
var trucks = await pyroFetesDbContext.Trucks
.Include(t => t.ShowTrucks)
.Select(t => new ReadTruckDto
{
Id = t.Id,
Type = t.Type,
MaxExplosiveCapacity = t.MaxExplosiveCapacity,
Sizes = t.Sizes,
Statut = t.Status,
ShowId = t.ShowTrucks!.FirstOrDefault() != null ? t.ShowTrucks.FirstOrDefault()!.ShowId : null
})
.ToListAsync(ct);
await Send.OkAsync(trucks, ct);
}
}

View File

@@ -0,0 +1,45 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Truck.Request;
using PyroFetes.DTO.Truck.Response;
namespace PyroFetes.Endpoints.Truck;
public class GetTruckEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<IdTruckDto, ReadTruckDto>
{
public override void Configure()
{
Get("/api/trucks/{Id}");
AllowAnonymous();
}
public override async Task HandleAsync(IdTruckDto req, CancellationToken ct)
{
if (!req.Id.HasValue)
{
await Send.NotFoundAsync(ct);
return;
}
var truck = await pyroFetesDbContext.Trucks
.Where(t => t.Id == req.Id.Value)
.Select(t => new ReadTruckDto
{
Id = t.Id,
Type = t.Type,
MaxExplosiveCapacity = t.MaxExplosiveCapacity,
Sizes = t.Sizes,
Statut = t.Status,
ShowId = t.ShowTrucks!.FirstOrDefault() != null ? t.ShowTrucks.FirstOrDefault()!.ShowId : null
})
.FirstOrDefaultAsync(ct);
if (truck is null)
{
await Send.NotFoundAsync(ct);
return;
}
await Send.OkAsync(truck, ct);
}
}

View File

@@ -0,0 +1,82 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Truck.Request;
using PyroFetes.DTO.Truck.Response;
namespace PyroFetes.Endpoints.Truck;
public class UpdateTruckEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<UpdateTruckDto, ReadTruckDto>
{
public override void Configure()
{
Put("/api/trucks/{Id}");
AllowAnonymous();
}
public override async Task HandleAsync(UpdateTruckDto req, CancellationToken ct)
{
if (!req.Id.HasValue)
{
await Send.NotFoundAsync(ct);
return;
}
var truck = await pyroFetesDbContext.Trucks
.Include(t => t.ShowTrucks)
.FirstOrDefaultAsync(t => t.Id == req.Id.Value, ct);
if (truck is null)
{
await Send.NotFoundAsync(ct);
return;
}
// Mise à jour des propriétés du truck
truck.Type = req.Type ?? truck.Type;
if (double.TryParse(req.MaxExplosiveCapacity, out var capacity))
{
truck.MaxExplosiveCapacity = capacity;
}
truck.Sizes = req.Sizes ?? truck.Sizes;
truck.Status = req.Statut ?? truck.Status;
// Gérer la relation ShowTruck
if (req.ShowId.HasValue)
{
// Supprimer l'ancienne relation
if (truck.ShowTrucks != null && truck.ShowTrucks.Any())
{
var existingShowTrucks = truck.ShowTrucks.ToList();
foreach (var st in existingShowTrucks)
{
pyroFetesDbContext.ShowTrucks.Remove(st);
}
}
// Ajouter la nouvelle relation si ShowId n'est pas 0
if (req.ShowId.Value != 0)
{
var newShowTruck = new PyroFetes.Models.ShowTruck
{
TruckId = truck.Id,
ShowId = req.ShowId.Value
};
pyroFetesDbContext.ShowTrucks.Add(newShowTruck);
}
}
await pyroFetesDbContext.SaveChangesAsync(ct);
var result = new ReadTruckDto
{
Id = truck.Id,
Type = truck.Type,
MaxExplosiveCapacity = truck.MaxExplosiveCapacity,
Sizes = truck.Sizes,
Statut = truck.Status,
ShowId = req.ShowId
};
await Send.OkAsync(result, ct);
}
}