Example #1
0
        /********************************************************************************************************/
        /******************************PRIVATE*HELPER*METHODS****************************************************/
        /********************************************************************************************************/

        private User NewTTL(User user, RootObjectTopTracks rootObject, TimeFrame timeFrame)
        {
            var items        = rootObject.items;
            var i            = 1;
            var topTrackList = new TopTrackList();

            topTrackList.Tracks = new List <Track>();

            foreach (Item item in items)
            {
                var artists = new List <Artist>();

                CreateTrackWithArtists(item, artists, i, topTrackList);

                i++;
            }

            topTrackList.User        = user;
            topTrackList.TimeFrame   = timeFrame;
            topTrackList.LastUpdated = DateTime.Now;
            topTrackList.LastChanged = DateTime.Now;

            _billboardDbContext.SaveChanges();

            return(user);
        }
Example #2
0
        private User UpdateTTL(TopTrackList topTrackList, User user, RootObjectTopTracks rootObject, TimeFrame timeFrame)
        {
            var  oldTracks = _billboardDbContext.Tracks.Where(t => t.TopTrackList.TopTrackListId == topTrackList.TopTrackListId).ToList();
            var  items     = rootObject.items;
            var  i         = 1;
            bool changed   = false;

            foreach (Item item in items)
            {
                var existingTrack = oldTracks.FirstOrDefault(t => t.SpotifyTrackId == item.id);

                if (existingTrack != null)
                {
                    changed = UpdateExistingTrack(i, existingTrack) || changed;

                    i++;
                    _billboardDbContext.SaveChanges();

                    oldTracks.Remove(existingTrack);
                    continue;
                }

                var artists = new List <Artist>();
                CreateTrackWithArtists(item, artists, i, topTrackList);
                i++;
            }

            // tracks no longer in chart get position 0, but are not deleted because they may return
            foreach (Track remainingTrack in oldTracks)
            {
                remainingTrack.Position = 0;
            }

            topTrackList.LastUpdated = DateTime.Now;

            if (changed)
            {
                topTrackList.LastChanged = DateTime.Now;
            }

            _billboardDbContext.SaveChanges();

            return(user);
        }
Example #3
0
        private void CreateTrackWithArtists(Item item, List <Artist> artists, int i, TopTrackList topTrackList)
        {
            // for every artist listed for the track, check if the artist already exist in the database.  if yes,
            // pull that artist, otherwise, create a new artist
            foreach (JsonArtist jsonArtist in item.artists)
            {
                var artist = _billboardDbContext.Artists.FirstOrDefault(a => a.SpotifyArtistId == jsonArtist.id);

                if (artist == null)
                {
                    artist = new Artist
                    {
                        Name            = jsonArtist.name,
                        OpenInSpotify   = jsonArtist.external_urls.spotify,
                        SpotifyArtistId = jsonArtist.id
                    };

                    _billboardDbContext.Artists.Add(artist);
                }

                artists.Add(artist);
            }

            var track = new Track(item.album.name, item.album.id, item.album.external_urls.spotify, item.album.images[0].url, DateTime.Now,
                                  item.album.images[1].url, item.name, item.external_urls.spotify, i, 0, item.album.images[2].url, item.id, item.uri, 0, 1, topTrackList);

            if (i == 1)
            {
                track.TimeAtNumberOne = 1;
            }

            _billboardDbContext.Tracks.Add(track);
            CreateTrackArtistRelation(artists, track);

            _billboardDbContext.SaveChanges();
        }