Guest User

Untitled

a guest
Jun 12th, 2025
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 KB | None | 0 0
  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public async Task<IActionResult> UpdateSongs(SetlistDetailsViewModel model)
  4. {
  5.     Console.WriteLine("UpdateSongs POST called.");
  6.     Console.WriteLine($"Received {model.Songs.Count} songs in the form.");
  7.  
  8.     var userId = _userManager.GetUserId(User);
  9.     var setlist = await _context.Setlists
  10.         .Include(s => s.Songs)
  11.         .FirstOrDefaultAsync(s => s.Id == model.SetlistId && s.UserId == userId);
  12.  
  13.     if (setlist == null)
  14.         return NotFound();
  15.  
  16.     // Validate all durations first and store valid ones
  17.     var parsedDurations = new Dictionary<int, TimeSpan>(); // for existing songs (Id != 0)
  18.     var newSongDurations = new List<TimeSpan>(); // for new songs (Id == 0), same order as in model.Songs
  19.  
  20.     for (int i = 0; i < model.Songs.Count; i++)
  21.     {
  22.         var songVm = model.Songs[i];
  23.         var durationInput = songVm.Duration?.Trim();
  24.         var durationParts = durationInput?.Split(':');
  25.  
  26.         if (durationParts == null || durationParts.Length != 2 ||
  27.             !int.TryParse(durationParts[0], out int minutes) ||
  28.             !int.TryParse(durationParts[1], out int seconds) ||
  29.             minutes < 0 || seconds < 0 || seconds >= 60 || minutes >= 60)
  30.         {
  31.             ModelState.AddModelError($"Duration-{songVm.Title}", $"Invalid duration for song '{songVm.Title}'. Use mm:ss format.");
  32.         }
  33.         else
  34.         {
  35.             var parsedDuration = new TimeSpan(0, minutes, seconds);
  36.             if (songVm.Id != 0)
  37.             {
  38.                 parsedDurations[songVm.Id] = parsedDuration;
  39.             }
  40.             else
  41.             {
  42.                 newSongDurations.Add(parsedDuration);
  43.             }
  44.         }
  45.     }
  46.  
  47.     if (!ModelState.IsValid)
  48.     {
  49.         model.Songs = setlist.Songs.OrderBy(s => s.Order).Select(s => new SetlistDetailsViewModel.SongItem
  50.         {
  51.             Id = s.Id,
  52.             Title = s.Title,
  53.             Duration = s.Duration.Hours > 0 ? s.Duration.ToString(@"hh\:mm\:ss") : s.Duration.ToString(@"mm\:ss"),
  54.             BPM = s.BPM,
  55.             Key = s.Key,
  56.             Order = s.Order
  57.         }).ToList();
  58.  
  59.         return View("Details", model);
  60.     }
  61.  
  62.     // Remove deleted songs
  63.     var postedSongIds = model.Songs.Where(s => s.Id != 0).Select(s => s.Id).ToList();
  64.     var songsToRemove = setlist.Songs.Where(s => !postedSongIds.Contains(s.Id)).ToList();
  65.     _context.Songs.RemoveRange(songsToRemove);
  66.  
  67.     // Add or update songs
  68.     Console.WriteLine($"Received {model.Songs.Count} songs in the form.");
  69.     foreach (var songVm in model.Songs)
  70.     {
  71.         Console.WriteLine($"Song: Id={songVm.Id}, Title='{songVm.Title}', Duration={songVm.Duration}");
  72.     }
  73.  
  74.     int newSongIndex = 0;
  75.     foreach (var songVm in model.Songs)
  76.     {
  77.         if (songVm.Id != 0)
  78.         {
  79.             // Update existing song
  80.             var existingSong = setlist.Songs.FirstOrDefault(s => s.Id == songVm.Id);
  81.             if (existingSong != null)
  82.             {
  83.                 existingSong.Title = songVm.Title;
  84.                 existingSong.Duration = parsedDurations[songVm.Id];
  85.                 existingSong.BPM = songVm.BPM;
  86.                 existingSong.Key = songVm.Key;
  87.                 existingSong.Order = songVm.Order;
  88.                 existingSong.SetlistId = songVm.SetlistId;
  89.             }
  90.         }
  91.         else
  92.         {
  93.             // Add new song
  94.             if (!string.IsNullOrWhiteSpace(songVm.Title))
  95.             {
  96.                 var newSong = new Song
  97.                 {
  98.                     Title = songVm.Title,
  99.                     Duration = newSongDurations[newSongIndex],
  100.                     BPM = songVm.BPM,
  101.                     Key = songVm.Key,
  102.                     Order = songVm.Order,
  103.                     UserId = userId,
  104.                     SetlistId = setlist.Id
  105.                 };
  106.                 _context.Songs.Add(newSong);
  107.                 newSongIndex++;
  108.             }
  109.         }
  110.     }
  111.  
  112.     await _context.SaveChangesAsync();
  113.  
  114.     var debugSongs = await _context.Songs.Where(s => s.SetlistId == setlist.Id).ToListAsync();
  115.     Console.WriteLine($"Setlist has {debugSongs.Count} songs after saving.");
  116.  
  117.     return RedirectToAction("Details", new { id = setlist.Id });
  118. }
Advertisement
Add Comment
Please, Sign In to add comment