public async Task <IActionResult> Remove(string playlistIdString, string songIdString) { //Validate string id as a valid GUID id: if (!Guid.TryParse(songIdString, out Guid songId)) { return(NotFound()); } //Find the playlist: Playlist playlist = _helper.GetUserPlaylistById(this.HttpContext, playlistIdString); if (playlist == null) { return(NotFound()); } //Get PlayableSong, if it exists: _context.Entry(playlist).Reference(x => x.Playable).Load(); _context.Entry(playlist.Playable).Collection(x => x.PlayableSongs).Load(); PlayableSong playableSong = playlist.PlayableSongs.FirstOrDefault(x => x.SongId == songId); if (playableSong == null) { return(NotFound()); } //Update positions, every song that comes after the //removed position should go back a position: int removedPosition = playableSong.Position; foreach (PlayableSong z in playlist.PlayableSongs) { if (z.Position > removedPosition) { z.Position--; } } //Remove song, save changes: playlist.PlayableSongs.Remove(playableSong); await _context.SaveChangesAsync(); PlaylistBasicVM playlistVM = new PlaylistBasicVM { PlaylistId = playlist.PlaylistId }; return(Ok(playlistVM)); }
public async Task <IActionResult> Create([FromBody] PrivateSongBM privateSongModel) { //automatically validate all input with model binding if (!ModelState.IsValid) { return(BadRequest()); } //get authenticated user MyUser user = _context.Users.FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name); if (user == null) { return(NotFound()); } _context.Entry(user).Reference(x => x.PrivateSongPlaylist).Load(); // Create private song object: PrivateSong pvs = new PrivateSong(); pvs.PrivateSongId = Guid.NewGuid(); pvs.MyUser = user; pvs.ArtistName = privateSongModel.ArtistName; pvs.AlbumName = privateSongModel.AlbumName; pvs.Name = privateSongModel.Name; Video v = new Video(); v.VideoId = Guid.NewGuid(); v.VideoUrl = privateSongModel.VideoUrl; v.IsLive = false; //not implemented yet, default to false v.StartSec = Video.GetTimeInSeconds(privateSongModel.StartAt); v.EndSec = Video.GetTimeInSeconds(privateSongModel.EndAt); v.Duration = Video.GetDuration(v.StartSec, v.EndSec); if (v.Duration == null) { return(BadRequest()); //invalid video duration } Song song = new Song(); song.SongId = Guid.NewGuid(); song.PrivateSong = pvs; song.PrivateSongId = pvs.PrivateSongId; v.Song = song; song.Video = v; //Add private song to private song's playlist: Playable p = _context.Playables.FirstOrDefault(y => y.PlaylistId == user.PrivateSongPlaylist.PlaylistId); PlayableSong ps = new PlayableSong(); ps.PlayableId = p.PlayableId; ps.Playable = p; ps.SongId = song.SongId; ps.Song = song; ps.Position = _context.Entry(p).Collection(x => x.PlayableSongs).Query().Count(); //Add to data context and save: _context.Songs.Add(song); _context.Videos.Add(v); _context.PrivateSongs.Add(pvs); _context.PlayableSongs.Add(ps); await _context.SaveChangesAsync(); PrivateSongBasicVM privateSongVM = new PrivateSongBasicVM(pvs); return(Ok(privateSongVM)); }
public async Task <IActionResult> CreateMultiple([FromBody] string[] songIdsStrings, string playlistIdString) { //validate the array of songIds: if (songIdsStrings == null || songIdsStrings.Length == 0 || songIdsStrings.Length >= 50) { return(NotFound()); } //validate songIds as valid GUID ids: Guid[] songIds = new Guid[songIdsStrings.Length]; for (int i = 0; i < songIds.Length; i++) { if (!Guid.TryParse(songIdsStrings[i], out songIds[i])) { return(NotFound()); } } //If any of the song ids doesn't exist, return 'NotFound': List <Song> songs = new List <Song>(); foreach (Guid id in songIds) { Song s = _context.Songs.FirstOrDefault(x => x.SongId == id); if (s == null) { return(NotFound()); } else { songs.Add(s); } } //Find the playlist to which songs should be added: Playlist playlist = _helper.GetUserPlaylistById(this.HttpContext, playlistIdString); if (playlist == null) { return(NotFound()); } _context.Entry(playlist).Reference(x => x.Playable).Load(); _context.Entry(playlist.Playable).Collection(x => x.PlayableSongs).Load(); //Add songs by creating the corresponding PlayableSong objects: foreach (Song s in songs) { //check if the song already exists in playlist, if so, skip if (!playlist.PlayableSongs.Any(x => x.SongId == s.SongId)) { PlayableSong ps = new PlayableSong(); ps.PlayableId = playlist.Playable.PlayableId; ps.Playable = playlist.Playable; ps.SongId = s.SongId; ps.Song = s; ps.Position = playlist.PlayableSongs.Count(); //add at the end of playlist! playlist.PlayableSongs.Add(ps); } } await _context.SaveChangesAsync(); PlaylistBasicVM playlistVM = new PlaylistBasicVM { PlaylistId = playlist.PlaylistId }; return(Ok(playlistVM)); }
public async Task <IActionResult> EditPosition(string playlistIdString, [FromBody] PlayableSongBM playableSongModel) { //Find the playlist: Playlist playlist = _helper.GetUserPlaylistById(this.HttpContext, playlistIdString); if (playlist == null) { return(NotFound()); } _context.Entry(playlist).Reference(x => x.Playable).Load(); _context.Entry(playlist.Playable).Collection(x => x.PlayableSongs).Load(); //Validate string id as a valid GUID id: if (!Guid.TryParse(playableSongModel.SongId, out Guid songId)) { return(NotFound()); } //Find the PlayableSong: PlayableSong playableSong = playlist.PlayableSongs.FirstOrDefault(x => x.SongId == songId); if (playableSong == null) { return(NotFound()); } //Validate the new position: int wantedPosition = playableSongModel.Position; if (wantedPosition < 0 || wantedPosition >= playlist.PlayableSongs.Count) { return(NotFound()); } //Update positions of other songs in playlist: int currentPosition = playableSong.Position; if (wantedPosition < currentPosition) { foreach (PlayableSong z in playlist.PlayableSongs) //moves songs down { if (z.Position >= wantedPosition && z.Position < currentPosition) { z.Position++; } } } else if (wantedPosition > currentPosition) { foreach (PlayableSong z in playlist.PlayableSongs) //move songs up { if (z.Position <= wantedPosition && z.Position > currentPosition) { z.Position--; } } } //Edit song position and save: playableSong.Position = wantedPosition; await _context.SaveChangesAsync(); PlaylistBasicVM playlistVM = new PlaylistBasicVM { PlaylistId = playlist.PlaylistId }; return(Ok(playlistVM)); }