Merge from marketing banner mvc
@ -1,17 +1,18 @@
|
||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers
|
||||
{
|
||||
using Infrastructure.Repositories;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Dto;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Collections.Generic;
|
||||
using Infrastructure.Repositories;
|
||||
using AspNetCore.Mvc;
|
||||
using Infrastructure;
|
||||
using System.Threading.Tasks;
|
||||
using Model;
|
||||
using EntityFrameworkCore;
|
||||
using Dto;
|
||||
using AspNetCore.Authorization;
|
||||
using Extensions.Options;
|
||||
using Microsoft.eShopOnContainers.Services.Marketing.API.ViewModel;
|
||||
|
||||
[Route("api/v1/[controller]")]
|
||||
[Authorize]
|
||||
@ -124,24 +125,21 @@
|
||||
}
|
||||
|
||||
[HttpGet("user/{userId:guid}")]
|
||||
public async Task<IActionResult> GetCampaignsByUserId(Guid userId)
|
||||
public async Task<IActionResult> GetCampaignsByUserId(Guid userId, int pageSize = 10, int pageIndex = 0)
|
||||
{
|
||||
var marketingData = await _marketingDataRepository.GetAsync(userId.ToString());
|
||||
|
||||
if (marketingData is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var campaignDtoList = new List<CampaignDTO>();
|
||||
|
||||
//Get User Location Campaign
|
||||
foreach(var userLocation in marketingData.Locations)
|
||||
if (marketingData != null)
|
||||
{
|
||||
var locationIdCandidateList = marketingData.Locations.Select(x => x.LocationId);
|
||||
var userCampaignList = await _context.Rules
|
||||
.OfType<UserLocationRule>()
|
||||
.Include(c => c.Campaign)
|
||||
.Where(c => c.LocationId == userLocation.LocationId)
|
||||
.Where(c => c.Campaign.From <= DateTime.Now
|
||||
&& c.Campaign.To >= DateTime.Now
|
||||
&& locationIdCandidateList.Contains(c.LocationId))
|
||||
.Select(c => c.Campaign)
|
||||
.ToListAsync();
|
||||
|
||||
@ -150,9 +148,18 @@
|
||||
var userCampaignDtoList = MapCampaignModelListToDtoList(userCampaignList);
|
||||
campaignDtoList.AddRange(userCampaignDtoList);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return Ok(campaignDtoList);
|
||||
var totalItems = campaignDtoList.Count();
|
||||
campaignDtoList = campaignDtoList
|
||||
.Skip(pageSize * pageIndex)
|
||||
.Take(pageSize).ToList();
|
||||
|
||||
var model = new PaginatedItemsViewModel<CampaignDTO>(
|
||||
pageIndex, pageSize, totalItems, campaignDtoList);
|
||||
|
||||
return Ok(model);
|
||||
}
|
||||
|
||||
|
||||
|
@ -33,7 +33,7 @@
|
||||
{
|
||||
new Campaign
|
||||
{
|
||||
Name = "Campaign Name 1",
|
||||
Name = ".NET Bot Black Hoodie 50% OFF",
|
||||
Description = "Campaign Description 1",
|
||||
From = DateTime.Now,
|
||||
To = DateTime.Now.AddDays(7),
|
||||
@ -42,24 +42,24 @@
|
||||
{
|
||||
new UserLocationRule
|
||||
{
|
||||
Description = "UserLocationRule1",
|
||||
Description = "Campaign is only for United States users.",
|
||||
LocationId = 1
|
||||
}
|
||||
}
|
||||
},
|
||||
new Campaign
|
||||
{
|
||||
Name = "Campaign Name 2",
|
||||
Name = "Roslyn Red T-Shirt 3x2",
|
||||
Description = "Campaign Description 2",
|
||||
From = DateTime.Now.AddDays(7),
|
||||
From = DateTime.Now.AddDays(-7),
|
||||
To = DateTime.Now.AddDays(14),
|
||||
PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/campaigns/2/pic",
|
||||
Rules = new List<Rule>
|
||||
{
|
||||
new UserLocationRule
|
||||
{
|
||||
Description = "UserLocationRule2",
|
||||
LocationId = 6
|
||||
Description = "Campaign is only for Seattle users.",
|
||||
LocationId = 3
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Before Width: | Height: | Size: 631 B After Width: | Height: | Size: 136 KiB |
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 166 KiB |
Before Width: | Height: | Size: 2.0 KiB |
Before Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 1.6 KiB |
Before Width: | Height: | Size: 2.2 KiB |
@ -2,7 +2,6 @@
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
public class PaginatedItemsViewModel<TEntity> where TEntity : class
|
||||
{
|
||||
public int PageIndex { get; private set; }
|
||||
|
@ -1,33 +1,54 @@
|
||||
namespace Microsoft.eShopOnContainers.WebMVC.Controllers
|
||||
{
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.eShopOnContainers.WebMVC.Services;
|
||||
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using AspNetCore.Authorization;
|
||||
using AspNetCore.Mvc;
|
||||
using Services;
|
||||
using ViewModels;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using ViewModels.Pagination;
|
||||
using global::WebMVC.ViewModels;
|
||||
|
||||
[Authorize]
|
||||
public class CampaignsController : Controller
|
||||
{
|
||||
private ICampaignService _campaignService;
|
||||
private readonly ICampaignService _campaignService;
|
||||
|
||||
public CampaignsController(ICampaignService campaignService) =>
|
||||
_campaignService = campaignService;
|
||||
|
||||
public async Task<IActionResult> Index()
|
||||
public async Task<IActionResult> Index(int page = 0, int pageSize = 10)
|
||||
{
|
||||
var campaignList = await _campaignService.GetCampaigns();
|
||||
var campaignList = await _campaignService.GetCampaigns(pageSize, page);
|
||||
|
||||
return View(campaignList);
|
||||
var vm = new CampaignViewModel()
|
||||
{
|
||||
CampaignItems = campaignList.Data,
|
||||
PaginationInfo = new PaginationInfo()
|
||||
{
|
||||
ActualPage = page,
|
||||
ItemsPerPage = pageSize,
|
||||
TotalItems = campaignList.Count,
|
||||
TotalPages = (int)Math.Ceiling(((decimal)campaignList.Count / pageSize))
|
||||
}
|
||||
};
|
||||
|
||||
vm.PaginationInfo.Next = (vm.PaginationInfo.ActualPage == vm.PaginationInfo.TotalPages - 1) ? "is-disabled" : "";
|
||||
vm.PaginationInfo.Previous = (vm.PaginationInfo.ActualPage == 0) ? "is-disabled" : "";
|
||||
|
||||
return View(vm);
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Details(int id)
|
||||
{
|
||||
var campaignDto = await _campaignService.GetCampaignById(id);
|
||||
|
||||
var campaign = new Campaign
|
||||
if (campaignDto is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var campaign = new CampaignItem
|
||||
{
|
||||
Id = campaignDto.Id,
|
||||
Name = campaignDto.Name,
|
||||
|
@ -84,9 +84,9 @@ namespace WebMVC.Infrastructure
|
||||
|
||||
public static class Marketing
|
||||
{
|
||||
public static string GetAllCampaigns(string baseUri, Guid userId)
|
||||
public static string GetAllCampaigns(string baseUri, string userId, int take, int page)
|
||||
{
|
||||
return $"{baseUri}user/{userId}";
|
||||
return $"{baseUri}user/{userId}?pageSize={take}&pageIndex={page}";
|
||||
}
|
||||
|
||||
public static string GetAllCampaignById(string baseUri, int id)
|
||||
|
@ -1,15 +1,14 @@
|
||||
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 AspNetCore.Authentication;
|
||||
using AspNetCore.Http;
|
||||
using BuildingBlocks.Resilience.Http;
|
||||
using ViewModels;
|
||||
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
|
||||
@ -31,28 +30,28 @@
|
||||
_httpContextAccesor = httpContextAccesor ?? throw new ArgumentNullException(nameof(httpContextAccesor));
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<CampaignDTO>> GetCampaigns()
|
||||
public async Task<Campaign> GetCampaigns(int pageSize, int pageIndex)
|
||||
{
|
||||
var userId = GetUserIdentity();
|
||||
var allCampaignItemsUri = API.Marketing.GetAllCampaigns(_remoteServiceBaseUrl, Guid.Parse(userId));
|
||||
var allCampaignItemsUri = API.Marketing.GetAllCampaigns(_remoteServiceBaseUrl,
|
||||
userId, pageSize, pageIndex);
|
||||
|
||||
var authorizationToken = await GetUserTokenAsync();
|
||||
var dataString = await _apiClient.GetStringAsync(allCampaignItemsUri, authorizationToken);
|
||||
|
||||
var response = JsonConvert.DeserializeObject<IEnumerable<CampaignDTO>>(dataString);
|
||||
var response = JsonConvert.DeserializeObject<Campaign>(dataString);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<CampaignDTO> GetCampaignById(int id)
|
||||
public async Task<CampaignItem> GetCampaignById(int id)
|
||||
{
|
||||
var userId = GetUserIdentity();
|
||||
var campaignByIdItemUri = API.Marketing.GetAllCampaignById(_remoteServiceBaseUrl, id);
|
||||
|
||||
var authorizationToken = await GetUserTokenAsync();
|
||||
var dataString = await _apiClient.GetStringAsync(campaignByIdItemUri, authorizationToken);
|
||||
|
||||
var response = JsonConvert.DeserializeObject<CampaignDTO>(dataString);
|
||||
var response = JsonConvert.DeserializeObject<CampaignItem>(dataString);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
namespace Microsoft.eShopOnContainers.WebMVC.Services
|
||||
{
|
||||
using Microsoft.eShopOnContainers.WebMVC.Models;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using ViewModels;
|
||||
|
||||
public interface ICampaignService
|
||||
{
|
||||
Task<IEnumerable<CampaignDTO>> GetCampaigns();
|
||||
Task<Campaign> GetCampaigns(int pageSize, int pageIndex);
|
||||
|
||||
Task<CampaignDTO> GetCampaignById(int id);
|
||||
Task<CampaignItem> GetCampaignById(int id);
|
||||
}
|
||||
}
|
@ -1,19 +1,12 @@
|
||||
namespace Microsoft.eShopOnContainers.WebMVC.ViewModels
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Campaign
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public string Description { get; set; }
|
||||
|
||||
public DateTime From { get; set; }
|
||||
|
||||
public DateTime To { get; set; }
|
||||
|
||||
public string PictureUri { get; set; }
|
||||
public int PageIndex { get; set; }
|
||||
public int PageSize { get; set; }
|
||||
public int Count { get; set; }
|
||||
public List<CampaignItem> Data { get; set; }
|
||||
}
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
namespace Microsoft.eShopOnContainers.WebMVC.Models
|
||||
namespace Microsoft.eShopOnContainers.WebMVC.ViewModels
|
||||
{
|
||||
using System;
|
||||
|
||||
public class CampaignDTO
|
||||
public class CampaignItem
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
@ -0,0 +1,12 @@
|
||||
namespace WebMVC.ViewModels
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
|
||||
using Microsoft.eShopOnContainers.WebMVC.ViewModels.Pagination;
|
||||
|
||||
public class CampaignViewModel
|
||||
{
|
||||
public IEnumerable<CampaignItem> CampaignItems { get; set; }
|
||||
public PaginationInfo PaginationInfo { get; set; }
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
@{
|
||||
ViewData["Title"] = "Campaign details";
|
||||
@model Microsoft.eShopOnContainers.WebMVC.ViewModels.Campaign
|
||||
@model CampaignItem
|
||||
}
|
||||
<section class="esh-campaigns-hero">
|
||||
<div class="container">
|
||||
|
@ -1,6 +1,6 @@
|
||||
@{
|
||||
ViewData["Title"] = "Campaigns";
|
||||
@model IEnumerable<Campaign>
|
||||
@model WebMVC.ViewModels.CampaignViewModel
|
||||
}
|
||||
|
||||
<section class="esh-campaigns-hero">
|
||||
@ -9,28 +9,29 @@
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@Html.Partial("_Header", new List<Header>() {
|
||||
@Html.Partial("_Header", new List<Header>() {
|
||||
new Header() { Controller = "Catalog", Text = "Back to catalog" } })
|
||||
|
||||
<div class="container">
|
||||
@if (Model.CampaignItems != null && Model.CampaignItems.Any())
|
||||
{
|
||||
@Html.Partial("_pagination", Model.PaginationInfo)
|
||||
|
||||
<div class="card-group esh-campaigns-items row">
|
||||
@foreach (var campaign in Model ?? Enumerable.Empty<Campaign>())
|
||||
@foreach (var catalogItem in Model.CampaignItems)
|
||||
{
|
||||
<div class="esh-campaigns-item col-md-4">
|
||||
<form asp-controller="Campaigns" asp-action="Details" asp-route-id="@campaign.Id">
|
||||
<div class="card-block">
|
||||
<h4 class="card-title esh-campaigns-name">@campaign.Name</h4>
|
||||
<img class="card-img-top esh-campaigns-thumbnail" src="@campaign.PictureUri" alt="@campaign.Name">
|
||||
<input class="esh-campaigns-button" type="submit" value="More details">
|
||||
</div>
|
||||
<div class="card-footer">
|
||||
|
||||
<small class="text-muted">
|
||||
From @campaign.From.ToString("MMMM dd, yyyy") until @campaign.To.ToString("MMMM dd, yyyy")
|
||||
</small>
|
||||
</div>
|
||||
</form>
|
||||
@Html.Partial("_campaign", catalogItem)
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
@Html.Partial("_pagination", Model.PaginationInfo)
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="esh-campaigns-items row">
|
||||
THERE ARE NO CAMPAIGNS
|
||||
</div>
|
||||
}
|
||||
</div>
|
17
src/Web/WebMVC/Views/Campaigns/_campaign.cshtml
Normal file
@ -0,0 +1,17 @@
|
||||
@model CampaignItem
|
||||
|
||||
|
||||
|
||||
<form asp-controller="Campaigns" asp-action="Details" asp-route-id="@Model.Id">
|
||||
<div class="card-block">
|
||||
<h4 class="card-title esh-campaigns-name">@Model.Name</h4>
|
||||
<img class="card-img-top esh-campaigns-thumbnail" src="@Model.PictureUri" alt="@Model.Name">
|
||||
<input class="esh-campaigns-button" type="submit" value="More details">
|
||||
</div>
|
||||
<div class="card-footer">
|
||||
|
||||
<small class="text-muted">
|
||||
From @Model.From.ToString("MMMM dd, yyyy") until @Model.To.ToString("MMMM dd, yyyy")
|
||||
</small>
|
||||
</div>
|
||||
</form>
|
32
src/Web/WebMVC/Views/Campaigns/_pagination.cshtml
Normal file
@ -0,0 +1,32 @@
|
||||
@model Microsoft.eShopOnContainers.WebMVC.ViewModels.Pagination.PaginationInfo
|
||||
|
||||
<div class="esh-pager">
|
||||
<div class="container">
|
||||
<article class="esh-pager-wrapper row">
|
||||
<nav>
|
||||
<a class="esh-pager-item esh-pager-item--navigable @Model.Previous"
|
||||
id="Previous"
|
||||
asp-controller="Campaigns"
|
||||
asp-action="Index"
|
||||
asp-route-page="@(Model.ActualPage -1)"
|
||||
aria-label="Previous">
|
||||
Previous
|
||||
</a>
|
||||
|
||||
<span class="esh-pager-item">
|
||||
Showing @Model.ItemsPerPage of @Model.TotalItems products - Page @(Model.ActualPage + 1) - @Model.TotalPages
|
||||
</span>
|
||||
|
||||
<a class="esh-pager-item esh-pager-item--navigable @Model.Next"
|
||||
id="Next"
|
||||
asp-controller="Campaigns"
|
||||
asp-action="Index"
|
||||
asp-route-page="@(Model.ActualPage + 1)"
|
||||
aria-label="Next">
|
||||
Next
|
||||
</a>
|
||||
</nav>
|
||||
</article>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -79,8 +79,11 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Views\Campaigns\" />
|
||||
<Folder Include="wwwroot\lib\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="ViewModels\CampaignItem.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|