Skip to content

Commit 278083a

Browse files
committed
Check when list is null and campaign details doesn't exist
1 parent ff790af commit 278083a

1 file changed

Lines changed: 39 additions & 2 deletions

File tree

src/Web/WebMVC/Controllers/CampaignsController.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ namespace Microsoft.eShopOnContainers.WebMVC.Controllers
22
{
33
using Microsoft.AspNetCore.Authorization;
44
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.eShopOnContainers.WebMVC.Models;
56
using Microsoft.eShopOnContainers.WebMVC.Services;
67
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
7-
using System;
88
using System.Collections.Generic;
99
using System.Threading.Tasks;
1010

@@ -18,7 +18,14 @@ public CampaignsController(ICampaignService campaignService) =>
1818

1919
public async Task<IActionResult> Index()
2020
{
21-
var campaignList = await _campaignService.GetCampaigns();
21+
var campaignDtoList = await _campaignService.GetCampaigns();
22+
23+
if(campaignDtoList is null)
24+
{
25+
return View();
26+
}
27+
28+
var campaignList = MapCampaignModelListToDtoList(campaignDtoList);
2229

2330
return View(campaignList);
2431
}
@@ -27,6 +34,11 @@ public async Task<IActionResult> Details(int id)
2734
{
2835
var campaignDto = await _campaignService.GetCampaignById(id);
2936

37+
if (campaignDto is null)
38+
{
39+
return NotFound();
40+
}
41+
3042
var campaign = new Campaign
3143
{
3244
Id = campaignDto.Id,
@@ -39,5 +51,30 @@ public async Task<IActionResult> Details(int id)
3951

4052
return View(campaign);
4153
}
54+
55+
private List<Campaign> MapCampaignModelListToDtoList(IEnumerable<CampaignDTO> campaignDtoList)
56+
{
57+
var campaignList = new List<Campaign>();
58+
59+
foreach(var campaignDto in campaignDtoList)
60+
{
61+
campaignList.Add(MapCampaignDtoToModel(campaignDto));
62+
}
63+
64+
return campaignList;
65+
}
66+
67+
private Campaign MapCampaignDtoToModel(CampaignDTO campaign)
68+
{
69+
return new Campaign
70+
{
71+
Id = campaign.Id,
72+
Name = campaign.Name,
73+
Description = campaign.Description,
74+
From = campaign.From,
75+
To = campaign.To,
76+
PictureUri = campaign.PictureUri
77+
};
78+
}
4279
}
4380
}

0 commit comments

Comments
 (0)