public async Task <IActionResult> UserEmail([FromQuery] GetUserEmailRequest model) { var output = await _spotifyAppService.GetUserEmail(new GetUserEmailInput { AccessToken = model.AccessToken }); if (output.HasError) { return(StatusCode(500, new ApiResponse(500, output.ErrorMessage))); } return(Ok(new ApiOkResponse(output.EmailAddress))); }
public async Task <IActionResult> SubscribeToArtists([FromBody] SubscribeToArtistsRequest request) { if (!request.SpotifyArtists.Any()) { return(BadRequest(new ApiResponse(400, "Please select at least one artist to subscribe to."))); } //Get the email address from their Spotify account again to see if we can auto-verify their Subscriber email. //This is more secure than eg trusting a flag in the request, which could be spoofed. //If there are any errors, fall back to assuming we need to verify their email. bool emailVerified = false; string requestEmail = StringUtils.NormaliseEmailAddress(request.EmailAddress); if (!String.IsNullOrWhiteSpace(request.SpotifyAccessToken)) { var emailOutput = await _spotifyAppService.GetUserEmail(new GetUserEmailInput { AccessToken = request.SpotifyAccessToken }); if (!emailOutput.HasError && emailOutput.EmailAddress != null && StringUtils.NormaliseEmailAddress(emailOutput.EmailAddress) == requestEmail) { emailVerified = true; } } var subscriberOutput = await _subscriberAppService.GetOrCreate(new GetOrCreateSubscriberInput { EmailAddress = request.EmailAddress, EmailAddressVerified = emailVerified }); if (subscriberOutput.HasError) { return(StatusCode(500, new ApiResponse(500, subscriberOutput.ErrorMessage))); } var artistsOutput = await _artistAppService.GetOrCreateMany(new GetOrCreateManyInput { Artists = request.SpotifyArtists }); if (artistsOutput.HasError) { return(StatusCode(500, new ApiResponse(500, artistsOutput.ErrorMessage))); } var subscriptionOutput = await _subscriptionAppService.SubscribeToArtists(new SubscribeToArtistsInput { Subscriber = subscriberOutput.Subscriber, Artists = artistsOutput.Artists }); if (subscriptionOutput.HasError) { return(StatusCode(500, new ApiResponse(500, subscriptionOutput.ErrorMessage))); } if (subscriberOutput.CreatedNewSubscriber) { await SendNotificationEmail(subscriberOutput.Subscriber.EmailAddress, artistsOutput.Artists.Count); } if (!subscriberOutput.Subscriber.EmailAddressVerified) { await SendVerificationEmail(subscriberOutput.Subscriber); } return(Ok(new ApiOkResponse(subscriptionOutput))); }