Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task<IActionResult> UpdateSongs(SetlistDetailsViewModel model)
- {
- Console.WriteLine("UpdateSongs POST called.");
- Console.WriteLine($"Received {model.Songs.Count} songs in the form.");
- var userId = _userManager.GetUserId(User);
- var setlist = await _context.Setlists
- .Include(s => s.Songs)
- .FirstOrDefaultAsync(s => s.Id == model.SetlistId && s.UserId == userId);
- if (setlist == null)
- return NotFound();
- // Validate all durations first and store valid ones
- var parsedDurations = new Dictionary<int, TimeSpan>(); // for existing songs (Id != 0)
- var newSongDurations = new List<TimeSpan>(); // for new songs (Id == 0), same order as in model.Songs
- for (int i = 0; i < model.Songs.Count; i++)
- {
- var songVm = model.Songs[i];
- var durationInput = songVm.Duration?.Trim();
- var durationParts = durationInput?.Split(':');
- if (durationParts == null || durationParts.Length != 2 ||
- !int.TryParse(durationParts[0], out int minutes) ||
- !int.TryParse(durationParts[1], out int seconds) ||
- minutes < 0 || seconds < 0 || seconds >= 60 || minutes >= 60)
- {
- ModelState.AddModelError($"Duration-{songVm.Title}", $"Invalid duration for song '{songVm.Title}'. Use mm:ss format.");
- }
- else
- {
- var parsedDuration = new TimeSpan(0, minutes, seconds);
- if (songVm.Id != 0)
- {
- parsedDurations[songVm.Id] = parsedDuration;
- }
- else
- {
- newSongDurations.Add(parsedDuration);
- }
- }
- }
- if (!ModelState.IsValid)
- {
- model.Songs = setlist.Songs.OrderBy(s => s.Order).Select(s => new SetlistDetailsViewModel.SongItem
- {
- Id = s.Id,
- Title = s.Title,
- Duration = s.Duration.Hours > 0 ? s.Duration.ToString(@"hh\:mm\:ss") : s.Duration.ToString(@"mm\:ss"),
- BPM = s.BPM,
- Key = s.Key,
- Order = s.Order
- }).ToList();
- return View("Details", model);
- }
- // Remove deleted songs
- var postedSongIds = model.Songs.Where(s => s.Id != 0).Select(s => s.Id).ToList();
- var songsToRemove = setlist.Songs.Where(s => !postedSongIds.Contains(s.Id)).ToList();
- _context.Songs.RemoveRange(songsToRemove);
- // Add or update songs
- Console.WriteLine($"Received {model.Songs.Count} songs in the form.");
- foreach (var songVm in model.Songs)
- {
- Console.WriteLine($"Song: Id={songVm.Id}, Title='{songVm.Title}', Duration={songVm.Duration}");
- }
- int newSongIndex = 0;
- foreach (var songVm in model.Songs)
- {
- if (songVm.Id != 0)
- {
- // Update existing song
- var existingSong = setlist.Songs.FirstOrDefault(s => s.Id == songVm.Id);
- if (existingSong != null)
- {
- existingSong.Title = songVm.Title;
- existingSong.Duration = parsedDurations[songVm.Id];
- existingSong.BPM = songVm.BPM;
- existingSong.Key = songVm.Key;
- existingSong.Order = songVm.Order;
- existingSong.SetlistId = songVm.SetlistId;
- }
- }
- else
- {
- // Add new song
- if (!string.IsNullOrWhiteSpace(songVm.Title))
- {
- var newSong = new Song
- {
- Title = songVm.Title,
- Duration = newSongDurations[newSongIndex],
- BPM = songVm.BPM,
- Key = songVm.Key,
- Order = songVm.Order,
- UserId = userId,
- SetlistId = setlist.Id
- };
- _context.Songs.Add(newSong);
- newSongIndex++;
- }
- }
- }
- await _context.SaveChangesAsync();
- var debugSongs = await _context.Songs.Where(s => s.SetlistId == setlist.Id).ToListAsync();
- Console.WriteLine($"Setlist has {debugSongs.Count} songs after saving.");
- return RedirectToAction("Details", new { id = setlist.Id });
- }
Advertisement
Add Comment
Please, Sign In to add comment