Add campaign Service
This commit is contained in:
parent
9c2df21ab0
commit
6168b52db8
58
src/Web/WebMVC/Services/CampaignService.cs
Normal file
58
src/Web/WebMVC/Services/CampaignService.cs
Normal file
@ -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<AppSettings> _settings;
|
||||
private readonly IHttpClient _apiClient;
|
||||
private readonly ILogger<CampaignService> _logger;
|
||||
private readonly string _remoteServiceBaseUrl;
|
||||
private readonly IHttpContextAccessor _httpContextAccesor;
|
||||
|
||||
public CampaignService(IOptionsSnapshot<AppSettings> settings, IHttpClient httpClient,
|
||||
ILogger<CampaignService> 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<List<CampaignDTO>> 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<List<CampaignDTO>>(dataString);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private string GetUserIdentity()
|
||||
{
|
||||
return _httpContextAccesor.HttpContext.User.FindFirst("sub").Value;
|
||||
}
|
||||
|
||||
private async Task<string> GetUserTokenAsync()
|
||||
{
|
||||
var context = _httpContextAccesor.HttpContext;
|
||||
return await context.Authentication.GetTokenAsync("access_token");
|
||||
}
|
||||
}
|
||||
}
|
11
src/Web/WebMVC/Services/ICampaignService.cs
Normal file
11
src/Web/WebMVC/Services/ICampaignService.cs
Normal file
@ -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<List<CampaignDTO>> GetCampaigns();
|
||||
}
|
||||
}
|
@ -70,6 +70,7 @@ namespace Microsoft.eShopOnContainers.WebMVC
|
||||
services.AddTransient<ICatalogService, CatalogService>();
|
||||
services.AddTransient<IOrderingService, OrderingService>();
|
||||
services.AddTransient<IBasketService, BasketService>();
|
||||
services.AddTransient<ICampaignService, CampaignService>();
|
||||
services.AddTransient<IIdentityParser<ApplicationUser>, IdentityParser>();
|
||||
|
||||
if (Configuration.GetValue<string>("UseResilientHttp") == bool.TrueString)
|
||||
|
Loading…
x
Reference in New Issue
Block a user