Пример #1
0
        public async Task <Domain.Errors.ServiceResult <UpdateSongError> > UpdateSongForPartyGoerAsync(PartyGoer partyGoer, List <string> songUris, int currentSongProgressInMs)
        {
            string perferredDeviceId = partyGoer.GetPerferredDeviceId();

            ApiParameters parameters = null;

            if (!string.IsNullOrWhiteSpace(perferredDeviceId))
            {
                parameters = new ApiParameters
                {
                    Parameters = new Dictionary <string, string>
                    {
                        { "device_id", perferredDeviceId }
                    }
                };
            }

            HttpResponseMessage response = null;

            if (parameters != null)
            {
                response = await SendHttpRequestAsync(partyGoer, _apiEndpoints[ApiEndpointType.PlaySong], parameters,
                                                      new StartUserPlaybackSong { uris = songUris.Select(song => song.Contains("spotify:track:") ? song : $"spotify:track:{song}".Split('+').First()).ToList(), position_ms = currentSongProgressInMs });
            }
            else
            {
                response = await SendHttpRequestAsync(partyGoer, _apiEndpoints[ApiEndpointType.PlaySong],
                                                      new StartUserPlaybackSong { uris = songUris.Select(song => song.Contains("spotify:track:") ? song : $"spotify:track:{song}".Split('+').First()).ToList(), position_ms = currentSongProgressInMs });
            }

            Domain.Errors.ServiceResult <UpdateSongError> error = new Domain.Errors.ServiceResult <UpdateSongError>();

            if (response.IsSuccessStatusCode)
            {
                return(error);
            }
            else
            {
                await _logService.LogExceptionAsync(new Exception($"Unable to update song for {partyGoer.GetId()}"), await response.Content.ReadAsStringAsync());

                // TODO: Check status codes and add specific messaging for status codes based on Spotifys API
                error.AddError(new UpdateSongError($"Unable to update song for {partyGoer.GetId()}"));
                return(error);
            }
        }