Skip to content

Commit 9d76e79

Browse files
committed
Merge from marketing banner mvc
2 parents 9fee40e + b36a05b commit 9d76e79

21 files changed

Lines changed: 181 additions & 97 deletions

File tree

src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers
22
{
3-
using Infrastructure.Repositories;
4-
using Microsoft.AspNetCore.Mvc;
5-
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure;
6-
using System.Threading.Tasks;
7-
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
8-
using Microsoft.EntityFrameworkCore;
9-
using Microsoft.eShopOnContainers.Services.Marketing.API.Dto;
10-
using System.Collections.Generic;
11-
using Microsoft.AspNetCore.Authorization;
123
using System;
134
using System.Linq;
14-
using Microsoft.Extensions.Options;
5+
using System.Collections.Generic;
6+
using Infrastructure.Repositories;
7+
using AspNetCore.Mvc;
8+
using Infrastructure;
9+
using System.Threading.Tasks;
10+
using Model;
11+
using EntityFrameworkCore;
12+
using Dto;
13+
using AspNetCore.Authorization;
14+
using Extensions.Options;
15+
using Microsoft.eShopOnContainers.Services.Marketing.API.ViewModel;
1516

1617
[Route("api/v1/[controller]")]
1718
[Authorize]
@@ -124,35 +125,41 @@ public async Task<IActionResult> Delete(int id)
124125
}
125126

126127
[HttpGet("user/{userId:guid}")]
127-
public async Task<IActionResult> GetCampaignsByUserId(Guid userId)
128+
public async Task<IActionResult> GetCampaignsByUserId(Guid userId, int pageSize = 10, int pageIndex = 0)
128129
{
129130
var marketingData = await _marketingDataRepository.GetAsync(userId.ToString());
130131

131-
if (marketingData is null)
132-
{
133-
return NotFound();
134-
}
135-
136132
var campaignDtoList = new List<CampaignDTO>();
137-
138-
//Get User Location Campaign
139-
foreach(var userLocation in marketingData.Locations)
133+
134+
if (marketingData != null)
140135
{
136+
var locationIdCandidateList = marketingData.Locations.Select(x => x.LocationId);
141137
var userCampaignList = await _context.Rules
142138
.OfType<UserLocationRule>()
143139
.Include(c => c.Campaign)
144-
.Where(c => c.LocationId == userLocation.LocationId)
145-
.Select(c => c.Campaign)
146-
.ToListAsync();
140+
.Where(c => c.Campaign.From <= DateTime.Now
141+
&& c.Campaign.To >= DateTime.Now
142+
&& locationIdCandidateList.Contains(c.LocationId))
143+
.Select(c => c.Campaign)
144+
.ToListAsync();
147145

148146
if (userCampaignList != null && userCampaignList.Any())
149147
{
150148
var userCampaignDtoList = MapCampaignModelListToDtoList(userCampaignList);
151149
campaignDtoList.AddRange(userCampaignDtoList);
152150
}
151+
153152
}
154153

155-
return Ok(campaignDtoList);
154+
var totalItems = campaignDtoList.Count();
155+
campaignDtoList = campaignDtoList
156+
.Skip(pageSize * pageIndex)
157+
.Take(pageSize).ToList();
158+
159+
var model = new PaginatedItemsViewModel<CampaignDTO>(
160+
pageIndex, pageSize, totalItems, campaignDtoList);
161+
162+
return Ok(model);
156163
}
157164

158165

src/Services/Marketing/Marketing.API/Infrastructure/MarketingContextSeed.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static List<Campaign> GetPreconfiguredMarketings()
3333
{
3434
new Campaign
3535
{
36-
Name = "Campaign Name 1",
36+
Name = ".NET Bot Black Hoodie 50% OFF",
3737
Description = "Campaign Description 1",
3838
From = DateTime.Now,
3939
To = DateTime.Now.AddDays(7),
@@ -42,24 +42,24 @@ static List<Campaign> GetPreconfiguredMarketings()
4242
{
4343
new UserLocationRule
4444
{
45-
Description = "UserLocationRule1",
45+
Description = "Campaign is only for United States users.",
4646
LocationId = 1
4747
}
4848
}
4949
},
5050
new Campaign
5151
{
52-
Name = "Campaign Name 2",
52+
Name = "Roslyn Red T-Shirt 3x2",
5353
Description = "Campaign Description 2",
54-
From = DateTime.Now.AddDays(7),
54+
From = DateTime.Now.AddDays(-7),
5555
To = DateTime.Now.AddDays(14),
5656
PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/campaigns/2/pic",
5757
Rules = new List<Rule>
5858
{
5959
new UserLocationRule
6060
{
61-
Description = "UserLocationRule2",
62-
LocationId = 6
61+
Description = "Campaign is only for Seattle users.",
62+
LocationId = 3
6363
}
6464
}
6565
}
136 KB
Loading
165 KB
Loading
-2 KB
Binary file not shown.
-1.26 KB
Binary file not shown.
-1.64 KB
Binary file not shown.
-2.25 KB
Binary file not shown.

src/Services/Marketing/Marketing.API/ViewModel/PaginatedItemsViewModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
using System.Collections.Generic;
44

5-
65
public class PaginatedItemsViewModel<TEntity> where TEntity : class
76
{
87
public int PageIndex { get; private set; }
@@ -21,4 +20,4 @@ public PaginatedItemsViewModel(int pageIndex, int pageSize, long count, IEnumera
2120
this.Data = data;
2221
}
2322
}
24-
}
23+
}

src/Web/WebMVC/Controllers/CampaignsController.cs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,54 @@
11
namespace Microsoft.eShopOnContainers.WebMVC.Controllers
22
{
3-
using Microsoft.AspNetCore.Authorization;
4-
using Microsoft.AspNetCore.Mvc;
5-
using Microsoft.eShopOnContainers.WebMVC.Services;
6-
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
7-
using System;
8-
using System.Collections.Generic;
3+
using AspNetCore.Authorization;
4+
using AspNetCore.Mvc;
5+
using Services;
6+
using ViewModels;
97
using System.Threading.Tasks;
8+
using System;
9+
using ViewModels.Pagination;
10+
using global::WebMVC.ViewModels;
1011

1112
[Authorize]
1213
public class CampaignsController : Controller
1314
{
14-
private ICampaignService _campaignService;
15+
private readonly ICampaignService _campaignService;
1516

1617
public CampaignsController(ICampaignService campaignService) =>
1718
_campaignService = campaignService;
1819

19-
public async Task<IActionResult> Index()
20+
public async Task<IActionResult> Index(int page = 0, int pageSize = 10)
2021
{
21-
var campaignList = await _campaignService.GetCampaigns();
22+
var campaignList = await _campaignService.GetCampaigns(pageSize, page);
23+
24+
var vm = new CampaignViewModel()
25+
{
26+
CampaignItems = campaignList.Data,
27+
PaginationInfo = new PaginationInfo()
28+
{
29+
ActualPage = page,
30+
ItemsPerPage = pageSize,
31+
TotalItems = campaignList.Count,
32+
TotalPages = (int)Math.Ceiling(((decimal)campaignList.Count / pageSize))
33+
}
34+
};
35+
36+
vm.PaginationInfo.Next = (vm.PaginationInfo.ActualPage == vm.PaginationInfo.TotalPages - 1) ? "is-disabled" : "";
37+
vm.PaginationInfo.Previous = (vm.PaginationInfo.ActualPage == 0) ? "is-disabled" : "";
2238

23-
return View(campaignList);
39+
return View(vm);
2440
}
2541

2642
public async Task<IActionResult> Details(int id)
2743
{
2844
var campaignDto = await _campaignService.GetCampaignById(id);
2945

30-
var campaign = new Campaign
46+
if (campaignDto is null)
47+
{
48+
return NotFound();
49+
}
50+
51+
var campaign = new CampaignItem
3152
{
3253
Id = campaignDto.Id,
3354
Name = campaignDto.Name,

0 commit comments

Comments
 (0)