Exemplo n.º 1
0
        public async Task <ServiceResult <UpdateSongError> > UpdateCurrentSongForEveryoneInPartyAsync(Party party, PartyGoer user)
        {
            try
            {
                if (party is null)
                {
                    throw new Exception($"Obtaining a party with ID {party.Id} returned null from the database");
                }

                if (!party.Host.Id.Equals(user.Id, StringComparison.CurrentCultureIgnoreCase) &&
                    !party.Listeners.Exists(p => p.Id.Equals(user.Id, StringComparison.CurrentCultureIgnoreCase)))
                {
                    throw new Exception($"A non host or party attendee tried to change the song for the party with ID {party.Id}. Attempted user ID: {user.Id}");
                }

                // Get the current song from the host
                CurrentSongDTO song = await _spotifyHttpClient.GetCurrentSongAsync(user.Id);

                List <Task <ServiceResult <UpdateSongError> > > updateSongForPartyTask = new List <Task <ServiceResult <UpdateSongError> > >();

                foreach (PartyGoer attendee in party.Listeners)
                {
                    updateSongForPartyTask.Add(_spotifyHttpClient.UpdateSongForPartyGoerAsync(attendee.Id, new List <string> {
                        song.TrackUri
                    }, song.ProgressMs));
                }

                await Task.WhenAll(updateSongForPartyTask);

                ServiceResult <UpdateSongError> errors = new ServiceResult <UpdateSongError>();

                // Verify all the updates worked
                foreach (Task <ServiceResult <UpdateSongError> > task in updateSongForPartyTask)
                {
                    if (task.IsCompletedSuccessfully && !task.Result.Success)
                    {
                        foreach (var error in task.Result.Errors)
                        {
                            errors.AddError(error);
                        }
                    }
                }

                return(errors);
            }
            catch (Exception ex)
            {
                await _logService.LogExceptionAsync(ex, "Error occurred in UpdateCurrentSongForEveryoneInPartyAsync");

                ServiceResult <UpdateSongError> error = new ServiceResult <UpdateSongError>();
                error.AddError(new UpdateSongError("Unable to update everyone's song."));

                return(error);
            }
        }
Exemplo n.º 2
0
 public async Task <CurrentSongDTO> GetCurrentSongAsync(string partyGoerId)
 {
     return(await _spotifyHttpClient.GetCurrentSongAsync(partyGoerId));
 }