From 3a09bfc8adffd12980762d0118461e603cd124c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9?= Date: Thu, 13 Nov 2025 15:11:34 +0100 Subject: [PATCH] Finalisation endpoints --- .../Endpoints/Show/CreateShowEndpoint.cs | 41 ++++++++++ .../Endpoints/Show/DeleteShowEndpoint.cs | 53 ++++++++++++ .../Endpoints/Show/GetAllShowsEndpoint.cs | 31 +++++++ PyroFetes/Endpoints/Show/GetShowEndpoint.cs | 45 ++++++++++ .../Endpoints/Show/UpdateShowEndpoint.cs | 57 +++++++++++++ .../Endpoints/Sound/CreateSoundEndpoint.cs | 47 +++++++++++ .../Endpoints/Sound/DeleteSoundEndpoint.cs | 44 ++++++++++ .../Endpoints/Sound/GetAllSoundsEndpoint.cs | 34 ++++++++ PyroFetes/Endpoints/Sound/GetSoundEndpoint.cs | 48 +++++++++++ .../Endpoints/Sound/UpdateSoundEndpoint.cs | 68 +++++++++++++++ .../CreateSoundTimecodeEndpoint.cs | 38 +++++++++ .../DeleteSoundTimecodeEndpoint.cs | 37 +++++++++ .../GetAllSoundTimecodesEndpoint.cs | 29 +++++++ .../SoundTimecode/GetSoundTimecodeEndpoint.cs | 43 ++++++++++ .../UpdateSoundTimecodeEndpoint.cs | 51 ++++++++++++ .../Endpoints/Truck/CreateTruckEndpoint.cs | 52 ++++++++++++ .../Endpoints/Truck/DeleteTruckEndpoint.cs | 45 ++++++++++ .../Endpoints/Truck/GetAllTrucksEndpoint.cs | 32 ++++++++ PyroFetes/Endpoints/Truck/GetTruckEndpoint.cs | 45 ++++++++++ .../Endpoints/Truck/UpdateTruckEndpoint.cs | 82 +++++++++++++++++++ 20 files changed, 922 insertions(+) create mode 100644 PyroFetes/Endpoints/Show/CreateShowEndpoint.cs create mode 100644 PyroFetes/Endpoints/Show/DeleteShowEndpoint.cs create mode 100644 PyroFetes/Endpoints/Show/GetAllShowsEndpoint.cs create mode 100644 PyroFetes/Endpoints/Show/GetShowEndpoint.cs create mode 100644 PyroFetes/Endpoints/Show/UpdateShowEndpoint.cs create mode 100644 PyroFetes/Endpoints/Sound/CreateSoundEndpoint.cs create mode 100644 PyroFetes/Endpoints/Sound/DeleteSoundEndpoint.cs create mode 100644 PyroFetes/Endpoints/Sound/GetAllSoundsEndpoint.cs create mode 100644 PyroFetes/Endpoints/Sound/GetSoundEndpoint.cs create mode 100644 PyroFetes/Endpoints/Sound/UpdateSoundEndpoint.cs create mode 100644 PyroFetes/Endpoints/SoundTimecode/CreateSoundTimecodeEndpoint.cs create mode 100644 PyroFetes/Endpoints/SoundTimecode/DeleteSoundTimecodeEndpoint.cs create mode 100644 PyroFetes/Endpoints/SoundTimecode/GetAllSoundTimecodesEndpoint.cs create mode 100644 PyroFetes/Endpoints/SoundTimecode/GetSoundTimecodeEndpoint.cs create mode 100644 PyroFetes/Endpoints/SoundTimecode/UpdateSoundTimecodeEndpoint.cs create mode 100644 PyroFetes/Endpoints/Truck/CreateTruckEndpoint.cs create mode 100644 PyroFetes/Endpoints/Truck/DeleteTruckEndpoint.cs create mode 100644 PyroFetes/Endpoints/Truck/GetAllTrucksEndpoint.cs create mode 100644 PyroFetes/Endpoints/Truck/GetTruckEndpoint.cs create mode 100644 PyroFetes/Endpoints/Truck/UpdateTruckEndpoint.cs diff --git a/PyroFetes/Endpoints/Show/CreateShowEndpoint.cs b/PyroFetes/Endpoints/Show/CreateShowEndpoint.cs new file mode 100644 index 0000000..7234f1e --- /dev/null +++ b/PyroFetes/Endpoints/Show/CreateShowEndpoint.cs @@ -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 +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/Show/DeleteShowEndpoint.cs b/PyroFetes/Endpoints/Show/DeleteShowEndpoint.cs new file mode 100644 index 0000000..c1fe117 --- /dev/null +++ b/PyroFetes/Endpoints/Show/DeleteShowEndpoint.cs @@ -0,0 +1,53 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Show.Request; + +namespace PyroFetes.Endpoints.Show; + +public class DeleteShowEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/Show/GetAllShowsEndpoint.cs b/PyroFetes/Endpoints/Show/GetAllShowsEndpoint.cs new file mode 100644 index 0000000..917bc9d --- /dev/null +++ b/PyroFetes/Endpoints/Show/GetAllShowsEndpoint.cs @@ -0,0 +1,31 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Show.Response; + +namespace PyroFetes.Endpoints.Show; + +public class GetAllShowsEndpoint(PyroFetesDbContext pyroFetesDbContext) : EndpointWithoutRequest> +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/Show/GetShowEndpoint.cs b/PyroFetes/Endpoints/Show/GetShowEndpoint.cs new file mode 100644 index 0000000..8675f2f --- /dev/null +++ b/PyroFetes/Endpoints/Show/GetShowEndpoint.cs @@ -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 +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/Show/UpdateShowEndpoint.cs b/PyroFetes/Endpoints/Show/UpdateShowEndpoint.cs new file mode 100644 index 0000000..a00d991 --- /dev/null +++ b/PyroFetes/Endpoints/Show/UpdateShowEndpoint.cs @@ -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 +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/Sound/CreateSoundEndpoint.cs b/PyroFetes/Endpoints/Sound/CreateSoundEndpoint.cs new file mode 100644 index 0000000..e2a0a73 --- /dev/null +++ b/PyroFetes/Endpoints/Sound/CreateSoundEndpoint.cs @@ -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 +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/Sound/DeleteSoundEndpoint.cs b/PyroFetes/Endpoints/Sound/DeleteSoundEndpoint.cs new file mode 100644 index 0000000..5cd9517 --- /dev/null +++ b/PyroFetes/Endpoints/Sound/DeleteSoundEndpoint.cs @@ -0,0 +1,44 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Sound.Request; + +namespace PyroFetes.Endpoints.Sound; + +public class DeleteSoundEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/Sound/GetAllSoundsEndpoint.cs b/PyroFetes/Endpoints/Sound/GetAllSoundsEndpoint.cs new file mode 100644 index 0000000..efb4b96 --- /dev/null +++ b/PyroFetes/Endpoints/Sound/GetAllSoundsEndpoint.cs @@ -0,0 +1,34 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Sound.Response; + +namespace PyroFetes.Endpoints.Sound; + +public class GetAllSoundsEndpoint(PyroFetesDbContext pyroFetesDbContext) : EndpointWithoutRequest> +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/Sound/GetSoundEndpoint.cs b/PyroFetes/Endpoints/Sound/GetSoundEndpoint.cs new file mode 100644 index 0000000..0ccf0a5 --- /dev/null +++ b/PyroFetes/Endpoints/Sound/GetSoundEndpoint.cs @@ -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 +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/Sound/UpdateSoundEndpoint.cs b/PyroFetes/Endpoints/Sound/UpdateSoundEndpoint.cs new file mode 100644 index 0000000..7d5e370 --- /dev/null +++ b/PyroFetes/Endpoints/Sound/UpdateSoundEndpoint.cs @@ -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 +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/SoundTimecode/CreateSoundTimecodeEndpoint.cs b/PyroFetes/Endpoints/SoundTimecode/CreateSoundTimecodeEndpoint.cs new file mode 100644 index 0000000..51252e4 --- /dev/null +++ b/PyroFetes/Endpoints/SoundTimecode/CreateSoundTimecodeEndpoint.cs @@ -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 +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/SoundTimecode/DeleteSoundTimecodeEndpoint.cs b/PyroFetes/Endpoints/SoundTimecode/DeleteSoundTimecodeEndpoint.cs new file mode 100644 index 0000000..2177b6f --- /dev/null +++ b/PyroFetes/Endpoints/SoundTimecode/DeleteSoundTimecodeEndpoint.cs @@ -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 +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/SoundTimecode/GetAllSoundTimecodesEndpoint.cs b/PyroFetes/Endpoints/SoundTimecode/GetAllSoundTimecodesEndpoint.cs new file mode 100644 index 0000000..7fa7296 --- /dev/null +++ b/PyroFetes/Endpoints/SoundTimecode/GetAllSoundTimecodesEndpoint.cs @@ -0,0 +1,29 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.SoundTimecode.Response; + +namespace PyroFetes.Endpoints.SoundTimecode; + +public class GetAllSoundTimecodesEndpoint(PyroFetesDbContext pyroFetesDbContext) : EndpointWithoutRequest> +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/SoundTimecode/GetSoundTimecodeEndpoint.cs b/PyroFetes/Endpoints/SoundTimecode/GetSoundTimecodeEndpoint.cs new file mode 100644 index 0000000..f73b01e --- /dev/null +++ b/PyroFetes/Endpoints/SoundTimecode/GetSoundTimecodeEndpoint.cs @@ -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 +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/SoundTimecode/UpdateSoundTimecodeEndpoint.cs b/PyroFetes/Endpoints/SoundTimecode/UpdateSoundTimecodeEndpoint.cs new file mode 100644 index 0000000..f72f26c --- /dev/null +++ b/PyroFetes/Endpoints/SoundTimecode/UpdateSoundTimecodeEndpoint.cs @@ -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 +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/Truck/CreateTruckEndpoint.cs b/PyroFetes/Endpoints/Truck/CreateTruckEndpoint.cs new file mode 100644 index 0000000..2c93a05 --- /dev/null +++ b/PyroFetes/Endpoints/Truck/CreateTruckEndpoint.cs @@ -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 +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/Truck/DeleteTruckEndpoint.cs b/PyroFetes/Endpoints/Truck/DeleteTruckEndpoint.cs new file mode 100644 index 0000000..48729af --- /dev/null +++ b/PyroFetes/Endpoints/Truck/DeleteTruckEndpoint.cs @@ -0,0 +1,45 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Truck.Request; + +namespace PyroFetes.Endpoints.Truck; + +public class DeleteTruckEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/Truck/GetAllTrucksEndpoint.cs b/PyroFetes/Endpoints/Truck/GetAllTrucksEndpoint.cs new file mode 100644 index 0000000..eeb8c9c --- /dev/null +++ b/PyroFetes/Endpoints/Truck/GetAllTrucksEndpoint.cs @@ -0,0 +1,32 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Truck.Response; + +namespace PyroFetes.Endpoints.Truck; + +public class GetAllTrucksEndpoint(PyroFetesDbContext pyroFetesDbContext) : EndpointWithoutRequest> +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/Truck/GetTruckEndpoint.cs b/PyroFetes/Endpoints/Truck/GetTruckEndpoint.cs new file mode 100644 index 0000000..ec2191d --- /dev/null +++ b/PyroFetes/Endpoints/Truck/GetTruckEndpoint.cs @@ -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 +{ + 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); + } +} diff --git a/PyroFetes/Endpoints/Truck/UpdateTruckEndpoint.cs b/PyroFetes/Endpoints/Truck/UpdateTruckEndpoint.cs new file mode 100644 index 0000000..82813aa --- /dev/null +++ b/PyroFetes/Endpoints/Truck/UpdateTruckEndpoint.cs @@ -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 +{ + 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); + } +}