/// <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); } }