|
1 | 1 | namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers |
2 | 2 | { |
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; |
12 | 3 | using System; |
13 | 4 | 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; |
15 | 16 |
|
16 | 17 | [Route("api/v1/[controller]")] |
17 | 18 | [Authorize] |
@@ -124,37 +125,43 @@ public async Task<IActionResult> Delete(int id) |
124 | 125 | } |
125 | 126 |
|
126 | 127 | [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) |
128 | 129 | { |
129 | 130 | var marketingData = await _marketingDataRepository.GetAsync(userId.ToString()); |
130 | 131 |
|
131 | | - if (marketingData is null) |
132 | | - { |
133 | | - return NotFound(); |
134 | | - } |
135 | | - |
136 | 132 | var campaignDtoList = new List<CampaignDTO>(); |
137 | 133 |
|
138 | | - //Get User Location Campaign |
139 | | - foreach(var userLocation in marketingData.Locations) |
| 134 | + if (marketingData != null) |
140 | 135 | { |
141 | | - var userCampaignList = await _context.Rules |
142 | | - .OfType<UserLocationRule>() |
143 | | - .Include(c => c.Campaign) |
144 | | - .Where(c => c.Campaign.From <= DateTime.Now |
145 | | - && c.Campaign.To >= DateTime.Now |
146 | | - && c.LocationId == userLocation.LocationId) |
147 | | - .Select(c => c.Campaign) |
148 | | - .ToListAsync(); |
149 | | - |
150 | | - if (userCampaignList != null && userCampaignList.Any()) |
| 136 | + //Get User Location Campaign |
| 137 | + foreach (var userLocation in marketingData.Locations) |
151 | 138 | { |
152 | | - var userCampaignDtoList = MapCampaignModelListToDtoList(userCampaignList); |
153 | | - campaignDtoList.AddRange(userCampaignDtoList); |
| 139 | + var userCampaignList = await _context.Rules |
| 140 | + .OfType<UserLocationRule>() |
| 141 | + .Include(c => c.Campaign) |
| 142 | + .Where(c => c.Campaign.From <= DateTime.Now |
| 143 | + && c.Campaign.To >= DateTime.Now |
| 144 | + && c.LocationId == userLocation.LocationId) |
| 145 | + .Select(c => c.Campaign) |
| 146 | + .ToListAsync(); |
| 147 | + |
| 148 | + if (userCampaignList != null && userCampaignList.Any()) |
| 149 | + { |
| 150 | + var userCampaignDtoList = MapCampaignModelListToDtoList(userCampaignList); |
| 151 | + campaignDtoList.AddRange(userCampaignDtoList); |
| 152 | + } |
154 | 153 | } |
155 | 154 | } |
156 | 155 |
|
157 | | - return Ok(campaignDtoList); |
| 156 | + var totalItems = campaignDtoList.Count(); |
| 157 | + campaignDtoList = campaignDtoList |
| 158 | + .Skip(pageSize * pageIndex) |
| 159 | + .Take(pageSize).ToList(); |
| 160 | + |
| 161 | + var model = new PaginatedItemsViewModel<CampaignDTO>( |
| 162 | + pageIndex, pageSize, totalItems, campaignDtoList); |
| 163 | + |
| 164 | + return Ok(model); |
158 | 165 | } |
159 | 166 |
|
160 | 167 |
|
|
0 commit comments