/// <summary> /// Adds and saved the full track to the database. /// If the track already exists it skips adding it. /// </summary> /// <param name="track">The spotify "FullTrack" object to add</param> public static void AddFullTrackToDb(FullTrack track) { using (var db = new AiApiDbContext()) { var fullTrack = new dFullTrack { Id = track.Id, Explicit = track.Explicit, Name = track.Name, PreviewUrl = track.PreviewUrl, TrackNumber = track.TrackNumber, Uri = track.Uri, Type = track.Type }; if (track.Artists.Any()) { fullTrack.Artist1Id = track.Artists[0].Id; if (track.Artists.Count > 1) { fullTrack.Artist2Id = track.Artists[1].Id; } } //let's make sure we don't already have the song in here if (!db.Songs.Any(s => s.Name == fullTrack.Name && s.Artist1Id == fullTrack.Artist1Id)) { //add the track to query db.Songs.Add(fullTrack); } db.SaveChangesAsync(); } }
public static async Task <bool> AddHistoryToDatabase(PlaybackContext data, string userId) { using (var db = new AiApiDbContext()) { try { var fullTrack = new dFullTrack { Id = data.Item.Id, Explicit = data.Item.Explicit, Name = data.Item.Name, PreviewUrl = data.Item.PreviewUrl, TrackNumber = data.Item.TrackNumber, Uri = data.Item.Uri, Type = data.Item.Type }; if (data.Item.Artists.Any()) { fullTrack.Artist1Id = data.Item.Artists[0].Id; if (data.Item.Artists.Count > 1) { fullTrack.Artist2Id = data.Item.Artists[1].Id; } } //let's make sure we don't already have the song in here if (!db.Songs.Any(s => s.Name == fullTrack.Name && s.Artist1Id == fullTrack.Artist1Id)) { //add the track to query db.Songs.Add(fullTrack); } //then we'll grab the instance of it. var playHistory = new dPlayHistory { PrivateProfile = db.PrivateProfiles.SingleOrDefault(x => x.Id == userId), PrivateProfilesId = userId, Name = data.Item.Name, PlayedAt = DateTime.Now, TrackId = data.Item.Id }; db.RecentlyPlayed.Add(playHistory); await db.SaveChangesAsync(); } catch (Exception ex) { return(false); } return(true); } }
public void UdpateSyncDate(string userId) { try { using (var db = new AiApiDbContext()) { db.LastSaveSyncTable.Add(new sLastSyncTable { SavedTrackLastSync = DateTime.Today, UserId = userId }); db.SaveChanges(); } } catch (Exception ex) { Console.WriteLine(ex); } }
public DateTime GetLastSyncDate(string userId) { try { using (var db = new AiApiDbContext()) { var date = db.LastSaveSyncTable.Where(x => x.UserId == userId).ToList().Last(); if (date != null) { return(date.SavedTrackLastSync); } } } catch (Exception ex) { Console.WriteLine(ex); } return(DateTime.Today); }
//public void AddFullArtist(string id) //{ // foreach (var simpleArtist in track.Artists) // { // var artist = new dArtist() // { // Id = simpleArtist.Id, // Name = simpleArtist.Name, // Href = simpleArtist.Href, // Uri = simpleArtist.Uri // }; // } //} /// <summary> /// Gets all the saved tracks from the database /// </summary> public List <dSavedTrack> GetSavedTracksByUser(string userName) { try { //launch up a new Db context using (var db = new AiApiDbContext()) { var returnedRecords = db.SavedTracks.Where(x => x.UserName == userName).ToList(); //removes all the records return(returnedRecords); } } catch (Exception ex) { Console.WriteLine("====================="); Console.WriteLine(ex.Message); Console.WriteLine(ex.InnerException); Console.WriteLine(ex.StackTrace); Console.WriteLine("====================="); } return(new List <dSavedTrack>()); }