Fix: Orden de Fotos y Asignación de Portada
This commit is contained in:
@@ -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<IActionResult> 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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user