/// <summary> /// Gets the playlist specified for the user specified. /// </summary> public PlaylistDto GetPlaylistForUser(string username, string playlistName) { // Create a new empty playlist object var playlist = new PlaylistDto { Username = username, PlaylistName = playlistName }; // Read the tracks from the database PreparedStatement prepared = _session.Prepare("SELECT username, playlist_name, sequence_no, artist, track_name, track_id, genre, track_length_in_seconds " + "FROM playlist_tracks WHERE username = ? and playlist_name = ?"); BoundStatement bound = prepared.Bind(username, playlistName); RowSet results = _session.Execute(bound); foreach (Row row in results.GetRows()) { PlaylistTrackDto track = MapRowToPlaylistTrack(row); playlist.TrackList.Add(track); // Pre-aggregate the playlist length in seconds; playlist.PlaylistLengthInSeconds += track.TrackLengthInSeconds; } return(playlist); }
/// <summary> /// Deletes a track from a playlist by its sequence number. /// </summary> public void DeleteTrackFromPlaylist(PlaylistDto playlist, long sequenceNumber) { // Find the track to delete, and delete it from the list // Find the track that has a matching sequence number int index = playlist.TrackList.FindIndex(track => track.SequenceNumber == sequenceNumber); // If the track was not found nothing to do if (index < 0) { return; } // Get the track to delete, remove it from the playlist's track list PlaylistTrackDto playlistTrackToDelete = playlist.TrackList[index]; playlist.TrackList.RemoveAt(index); // Adjust the playlist length playlist.PlaylistLengthInSeconds -= playlistTrackToDelete.TrackLengthInSeconds; // Remove it from the database PreparedStatement prepared = _session.Prepare("DELETE from playlist_tracks where username = ? and playlist_name = ? and sequence_no = ?"); BoundStatement bound = prepared.Bind(playlist.Username, playlist.PlaylistName, SequenceToDateTimeOffset(playlistTrackToDelete.SequenceNumber)); _session.Execute(bound); }
public ActionResult AddTrackToPlaylist(AddTrackToPlaylistModel model) { // Grab the PlaylistTrack information from the DB TrackDto track = _tracksDao.GetTrackById(model.TrackId); var playlistTrack = new PlaylistTrackDto(track); var user = (UserDto)Session["user"]; PlaylistDto playlist = _playlistsDao.GetPlaylistForUser(user.Username, model.PlaylistName); _playlistsDao.AddTrackToPlaylist(playlist, playlistTrack); return(RedirectToAction("ListTracks", "PlaylistTracks", new { playlistName = model.PlaylistName })); }
/// <summary> /// Adds a track to a playlist. /// </summary> public void AddTrackToPlaylist(PlaylistDto playlist, PlaylistTrackDto playlistTrack) { PreparedStatement prepared = _session.Prepare("INSERT into playlist_tracks" + " (username, playlist_name, sequence_no, artist, track_name, genre, track_id, track_length_in_seconds) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); // Since the playlistTrack sequence is like a time-series, set it's sequence to the current time // Also update the total time for the playlist locally. playlistTrack.SequenceNumber = DateTimeOffsetToSequence(DateTimeOffset.Now); playlist.PlaylistLengthInSeconds += playlistTrack.TrackLengthInSeconds; // TODO: If the C# driver adds support for binding parameters by name, use that here BoundStatement bound = prepared.Bind(playlist.Username, playlist.PlaylistName, SequenceToDateTimeOffset(playlistTrack.SequenceNumber), playlistTrack.Artist, playlistTrack.TrackName, playlistTrack.Genre, playlistTrack.TrackId, playlistTrack.TrackLengthInSeconds); _session.Execute(bound); playlist.TrackList.Add(playlistTrack); }