public IActionResult Get(string typeString, string typeIdString) { //Validate type as PlayableType and typeId as GUID id: if (!Enum.TryParse(typeof(PlayableType), typeString, true, out object typeObject) || !Guid.TryParse(typeIdString, out Guid typeId)) { return(NotFound()); } PlayableType type = (PlayableType)typeObject; //Get IPlayable for the given type and id: IPlayable iPlayable = null; if (type == PlayableType.Playlist) //user playlist { iPlayable = _helper.GetUserPlaylistById(this.HttpContext, typeId.ToString()); //Check if user is authenticated and owns such playlist. } else if (type == PlayableType.PublicPlaylist) //public playlist { iPlayable = _context.PublicPlaylists.FirstOrDefault(x => x.PublicPlaylistId == typeId); } else if (type == PlayableType.Album) //album { iPlayable = _context.Albums.Include(x1 => x1.Artist).FirstOrDefault(x => x.AlbumId == typeId); } if (iPlayable == null) { return(NotFound()); //no playable was found } //Load entry to access PlayableId _context.Entry(iPlayable).Reference(x => x.Playable).Load(); //Load necessary related playable data (independent of playable type): iPlayable.Playable = _context.Playables .Include(x => x.PlayableSongs) .ThenInclude(x => x.Song) .ThenInclude(x => x.Video) .Include(x => x.PlayableSongs) .ThenInclude(x => x.Song) .ThenInclude(x => x.PublicSong) .ThenInclude(x => x.Album) .ThenInclude(x => x.Artist) .Include(x => x.PlayableSongs) .ThenInclude(x => x.Song) .ThenInclude(x => x.PrivateSong) .FirstOrDefault(x => x.PlayableId == iPlayable.Playable.PlayableId); PlayableVM playableVM = new PlayableVM(iPlayable); return(Ok(playableVM)); }
public async Task <IActionResult> Edit([FromBody] PlaylistNameBM newName, string playlistIdString) { if (!ModelState.IsValid) { return(BadRequest()); } Playlist playlist = _helper.GetUserPlaylistById(this.HttpContext, playlistIdString); if (playlist == null) { return(NotFound()); } //change playlist's name, save context: playlist.Name = newName.playlistName; await _context.SaveChangesAsync(); PlaylistBasicVM playlistVM = new PlaylistBasicVM(playlist); return(Ok(playlistVM)); }
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)); }