From 8bd838471526d58465830b178373fd8fcf027780 Mon Sep 17 00:00:00 2001 From: dmolinari Date: Wed, 18 Mar 2026 15:52:24 -0300 Subject: [PATCH] =?UTF-8?q?Fix:=20Orden=20de=20Fotos=20y=20Asignaci=C3=B3n?= =?UTF-8?q?=20de=20Portada?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/AdsV2Controller.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Backend/MotoresArgentinosV2.API/Controllers/AdsV2Controller.cs b/Backend/MotoresArgentinosV2.API/Controllers/AdsV2Controller.cs index fa7ac2a..fff185e 100644 --- a/Backend/MotoresArgentinosV2.API/Controllers/AdsV2Controller.cs +++ b/Backend/MotoresArgentinosV2.API/Controllers/AdsV2Controller.cs @@ -521,6 +521,34 @@ public class AdsV2Controller : ControllerBase return Ok(models); } + private async Task NormalizeAdPhotosAsync(int adId) + { + var photos = await _context.AdPhotos + .Where(p => p.AdID == adId) + .OrderBy(p => p.SortOrder) + .ThenBy(p => p.PhotoID) + .ToListAsync(); + + bool coverAssigned = false; + for (int i = 0; i < photos.Count; i++) + { + photos[i].SortOrder = i; + + if (photos[i].IsCover && !coverAssigned) { + coverAssigned = true; + } else { + photos[i].IsCover = false; + } + } + + if (!coverAssigned && photos.Count > 0) + { + photos[0].IsCover = true; + } + + await _context.SaveChangesAsync(); + } + [HttpPost("{id}/upload-photos")] public async Task UploadPhotos(int id, [FromForm] IFormFileCollection files) { @@ -573,6 +601,7 @@ public class AdsV2Controller : ControllerBase if (uploadedCount > 0) { await _context.SaveChangesAsync(); + await NormalizeAdPhotosAsync(id); return Ok(new { message = $"{uploadedCount} fotos procesadas.", @@ -604,6 +633,8 @@ public class AdsV2Controller : ControllerBase _context.AdPhotos.Remove(photo); await _context.SaveChangesAsync(); + await NormalizeAdPhotosAsync(photo.AdID); + return NoContent(); }