diff --git a/src/Web/WebMVC/Services/CampaignService.cs b/src/Web/WebMVC/Services/CampaignService.cs new file mode 100644 index 000000000..413f18512 --- /dev/null +++ b/src/Web/WebMVC/Services/CampaignService.cs @@ -0,0 +1,58 @@ +namespace Microsoft.eShopOnContainers.WebMVC.Services +{ + using global::WebMVC.Infrastructure; + using Microsoft.AspNetCore.Authentication; + using Microsoft.AspNetCore.Http; + using Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http; + using Microsoft.eShopOnContainers.WebMVC.Models; + using Microsoft.Extensions.Logging; + using Microsoft.Extensions.Options; + using Newtonsoft.Json; + using System; + using System.Collections.Generic; + using System.Threading.Tasks; + + public class CampaignService : ICampaignService + { + private readonly IOptionsSnapshot _settings; + private readonly IHttpClient _apiClient; + private readonly ILogger _logger; + private readonly string _remoteServiceBaseUrl; + private readonly IHttpContextAccessor _httpContextAccesor; + + public CampaignService(IOptionsSnapshot settings, IHttpClient httpClient, + ILogger logger, IHttpContextAccessor httpContextAccesor) + { + _settings = settings; + _apiClient = httpClient; + _logger = logger; + + _remoteServiceBaseUrl = $"{_settings.Value.MarketingUrl}/api/v1/campaigns/"; + _httpContextAccesor = httpContextAccesor ?? throw new ArgumentNullException(nameof(httpContextAccesor)); + } + + public async Task> GetCampaigns() + { + var userId = GetUserIdentity(); + var allCampaignItemsUri = API.Marketing.GetAllCampaigns(_remoteServiceBaseUrl, userId); + + var authorizationToken = await GetUserTokenAsync(); + var dataString = await _apiClient.GetStringAsync(allCampaignItemsUri, authorizationToken); + + var response = JsonConvert.DeserializeObject>(dataString); + + return response; + } + + private string GetUserIdentity() + { + return _httpContextAccesor.HttpContext.User.FindFirst("sub").Value; + } + + private async Task GetUserTokenAsync() + { + var context = _httpContextAccesor.HttpContext; + return await context.Authentication.GetTokenAsync("access_token"); + } + } +} \ No newline at end of file diff --git a/src/Web/WebMVC/Services/ICampaignService.cs b/src/Web/WebMVC/Services/ICampaignService.cs new file mode 100644 index 000000000..7b474e943 --- /dev/null +++ b/src/Web/WebMVC/Services/ICampaignService.cs @@ -0,0 +1,11 @@ +namespace Microsoft.eShopOnContainers.WebMVC.Services +{ + using Microsoft.eShopOnContainers.WebMVC.Models; + using System.Collections.Generic; + using System.Threading.Tasks; + + public interface ICampaignService + { + Task> GetCampaigns(); + } +} \ No newline at end of file diff --git a/src/Web/WebMVC/Startup.cs b/src/Web/WebMVC/Startup.cs index f119e1d8e..f022b256f 100644 --- a/src/Web/WebMVC/Startup.cs +++ b/src/Web/WebMVC/Startup.cs @@ -70,6 +70,7 @@ namespace Microsoft.eShopOnContainers.WebMVC services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient, IdentityParser>(); if (Configuration.GetValue("UseResilientHttp") == bool.TrueString)