Example #1
0
        public async Task <int?> SaveUserAsync(PartyGoer partier)
        {
            string newUserSql = @"INSERT INTO Users(name, display_name)
                                  VALUES(@Name, @Name)";

            string lastLoginSql = @"INSERT INTO LastLogin(user_id, time)
                                    VALUES(@UserId, NOW())";


            await _connection.ExecuteAsync(newUserSql, new
            {
                Name = partier.GetSpotifyId()
            });

            int?userId = await GetUserIdAsync(partier);

            if (!userId.HasValue)
            {
                throw new Exception($"User {partier.GetSpotifyId()} does not have an id after saving");
            }

            await _connection.ExecuteAsync(lastLoginSql, new
            {
                UserId = userId.Value
            });

            return(await GetUserIdAsync(partier));
        }
Example #2
0
        public async Task <int> LoginUser(PartyGoer user)
        {
            int?userId = await _userRepository.GetUserIdAsync(user);

            if (!userId.HasValue)
            {
                userId = await _userRepository.SaveUserAsync(user);

                if (userId.HasValue)
                {
                    if (!_userIds.ContainsKey(user.GetSpotifyId()))
                    {
                        _userIds.Add(user.GetSpotifyId(), userId.Value);
                    }
                }
                else
                {
                    throw new Exception($"User {user.GetSpotifyId()} does not have a user id even after saving user");
                }
            }

            await _userRepository.UpdateLastLoginTimeAsync(userId.Value);

            return(userId.Value);
        }
Example #3
0
        private async Task <int> GetUserIdAsync(PartyGoer user)
        {
            if (_userIds.ContainsKey(user.GetSpotifyId()))
            {
                return(_userIds[user.GetSpotifyId()]);
            }

            int?userId = await _userRepository.GetUserIdAsync(user);

            if (!userId.HasValue)
            {
                throw new Exception($"The user {user.GetSpotifyId()} does not have a user id. They need to be registered");
            }

            return(userId.Value);
        }
Example #4
0
 private Contribution CreateContribution(PartyGoer partyGoer, UserContribution userContribution)
 {
     return(new Contribution
     {
         ContributedBy = partyGoer.GetSpotifyId(),
         ContributionId = Guid.NewGuid(),
         Id = userContribution.Id,
         Type = userContribution.Type,
         Name = userContribution.Name
     });
 }
Example #5
0
        public async Task <List <Domain.Track> > GetRecommendedTracksAsync(PartyGoer partyGoer, RecommendedTracksSeed recommendedTrackSeeds)
        {
            List <string> seedTrackUris  = recommendedTrackSeeds?.SeedTrackUris;
            List <string> seedArtistUris = recommendedTrackSeeds?.SeedArtistUris;

            if (seedTrackUris?.Count + seedArtistUris?.Count > 5 || seedTrackUris?.Count + seedArtistUris?.Count == 0)
            {
                throw new ArgumentException("The count of seeds need to be between 1 and 5");
            }

            recommendedTrackSeeds.SeedTrackUris = seedTrackUris.Select(p => p.Replace("spotify:track:", "").Split("+").First()).ToList();

            var response = await SendHttpRequestAsync(partyGoer.GetSpotifyId(), _apiEndpoints[ApiEndpointType.GetRecommendedTracks], AddRecommendedSeedApiParmeters(recommendedTrackSeeds), true);

            RecommendedTracks tracks = await response.Content.ReadFromJsonAsync <RecommendedTracks>();

            List <Domain.Track> recommendedSongs = new List <Domain.Track>();

            foreach (SpotifyModels.Track track in tracks.Tracks)
            {
                if (track.Markets.Contains(partyGoer.GetMarket()) && track != null)
                {
                    recommendedSongs.Add(new Domain.Track
                    {
                        AlbumImageUrl = null,
                        Artists       = track.Artists.Select(p => new SpotifyModels.Artist {
                            Id = p.Id, Name = p.Name
                        }).ToList(),
                        Explicit = track.Explicit,
                        Length   = track.Duration,
                        Name     = track.Name,
                        Id       = track.Id
                    });
                }
            }

            return(recommendedSongs.ToList());
        }
Example #6
0
        public async Task <int?> GetUserIdAsync(PartyGoer user)
        {
            string sql = @"SELECT id 
                           FROM Users
                           WHERE name = @Name";

            int?userId = await _connection.ExecuteScalarAsync <int?>(sql, new { Name = user.GetSpotifyId() });

            return(userId);
        }
Example #7
0
        public async Task <List <SuggestedContribution> > GetSuggestedContributionsAsync(PartyGoer partier, List <string> excludedIds = null)
        {
            if (excludedIds == null)
            {
                excludedIds = new List <string>();
            }

            bool newSuggestionExists = false;
            Task <List <SpotibroModels.Track> >  tracksTask;
            Task <List <SpotibroModels.Artist> > artistsTask;

            List <SpotibroModels.Track>  uniqueTracks  = null;
            List <SpotibroModels.Artist> uniqueArtists = null;

            // How about some nice lil API calls in a while loop :o
            do
            {
                // get items from multiple sources and return that to the client
                //Task<List<SpotibroModels.PlaylistSummary>> playlistsTask = GetUsersPlaylistsAsync(partier, 10);
                tracksTask  = _spotifyHttpClient.GetUserTopTracksAsync(partier.GetSpotifyId(), 15);
                artistsTask = _spotifyApi.GetUsersTopArtistsAsync(partier, 15);

                await Task.WhenAll(artistsTask, tracksTask);

                // Lets make sure we got some results
                foreach (var excludedId in excludedIds)
                {
                    tracksTask.Result.RemoveAll(p => p.Id == excludedId);
                    artistsTask.Result.RemoveAll(p => p.Id == excludedId);
                }

                if (tracksTask.Result.Count + artistsTask.Result.Count > 0)
                {
                    uniqueArtists       = artistsTask.Result.Distinct(new ArtistComparer()).ToList();
                    uniqueTracks        = tracksTask.Result.Distinct(new TrackComparer()).ToList();
                    newSuggestionExists = true;
                }
            } while (!newSuggestionExists);

            List <SuggestedContribution> contributions = new List <SuggestedContribution>();

            lock (_randomLock)
            {
                for (int i = 0; i < 2; i++)
                {
                    SpotibroModels.Track track = uniqueTracks.ElementAt(_random.Next(0, uniqueTracks.Count - 1));

                    contributions.Add(new SuggestedContribution
                    {
                        Id   = track.Id,
                        Name = $"{track.Name} - {track.Artists.First().Name}",
                        Type = ContributionType.Track
                    });
                }

                /*
                 * for (int i = 0; i < 2; i++)
                 * {
                 *  SpotibroModels.PlaylistSummary playlist = playlistsTask.Result.ElementAt(_random.Next(0, playlistsTask.Result.Count - 1));
                 *
                 *  contributions.Add(new SuggestedContribution
                 *  {
                 *      Id = playlist.Id,
                 *      Name = playlist.Name,
                 *      Type = ContributionType.Playlist
                 *  });
                 * }*/

                for (int i = 0; i < 2; i++)
                {
                    SpotibroModels.Artist artist = uniqueArtists.ElementAt(_random.Next(0, uniqueArtists.Count - 1));

                    contributions.Add(new SuggestedContribution
                    {
                        Id   = artist.Id,
                        Name = artist.Name,
                        Type = ContributionType.Artist
                    });
                }
            }

            return(contributions);
        }