Skip to content

Commit 7842194

Browse files
committed
Create RuleType class to manage RuleTypeEnum
1 parent 2111d56 commit 7842194

3 files changed

Lines changed: 38 additions & 16 deletions

File tree

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
using Microsoft.eShopOnContainers.Services.Marketing.API.Dto;
99
using System.Collections.Generic;
1010
using Microsoft.AspNetCore.Authorization;
11+
using System.Linq;
1112

1213
[Route("api/v1/[controller]")]
13-
[Authorize]
14+
//[Authorize]
1415
public class CampaignsController : Controller
1516
{
1617
private readonly MarketingContext _context;
@@ -27,7 +28,7 @@ public async Task<IActionResult> GetAllCampaigns()
2728
.Include(c => c.Rules)
2829
.ToListAsync();
2930

30-
var campaignDtoList = CampaignModelListToDtoList(campaignList);
31+
var campaignDtoList = MapCampaignModelListToDtoList(campaignList);
3132

3233
return Ok(campaignDtoList);
3334
}
@@ -44,7 +45,7 @@ public async Task<IActionResult> GetCampaignById(int id)
4445
return NotFound();
4546
}
4647

47-
var campaignDto = CampaignModelToDto(campaign);
48+
var campaignDto = MapCampaignModelToDto(campaign);
4849

4950
return Ok(campaignDto);
5051
}
@@ -57,7 +58,7 @@ public async Task<IActionResult> CreateCampaign([FromBody] CampaignDTO campaign)
5758
return BadRequest();
5859
}
5960

60-
var campaingToCreate = CampaignDtoToModel(campaign);
61+
var campaingToCreate = MapCampaignDtoToModel(campaign);
6162

6263
await _context.Campaigns.AddAsync(campaingToCreate);
6364
await _context.SaveChangesAsync();
@@ -110,17 +111,17 @@ public async Task<IActionResult> Delete(int id)
110111

111112

112113

113-
private List<CampaignDTO> CampaignModelListToDtoList(List<Campaign> campaignList)
114+
private List<CampaignDTO> MapCampaignModelListToDtoList(List<Campaign> campaignList)
114115
{
115116
var campaignDtoList = new List<CampaignDTO>();
116117

117118
campaignList.ForEach(campaign => campaignDtoList
118-
.Add(CampaignModelToDto(campaign)));
119+
.Add(MapCampaignModelToDto(campaign)));
119120

120121
return campaignDtoList;
121122
}
122123

123-
private CampaignDTO CampaignModelToDto(Campaign campaign)
124+
private CampaignDTO MapCampaignModelToDto(Campaign campaign)
124125
{
125126
var campaignDto = new CampaignDTO
126127
{
@@ -132,7 +133,7 @@ private CampaignDTO CampaignModelToDto(Campaign campaign)
132133

133134
campaign.Rules.ForEach(c =>
134135
{
135-
switch ((RuleTypeEnum)c.RuleTypeId)
136+
switch (RuleType.From(c.RuleTypeId))
136137
{
137138
case RuleTypeEnum.UserLocationRule:
138139
var userLocationRule = c as UserLocationRule;
@@ -149,7 +150,7 @@ private CampaignDTO CampaignModelToDto(Campaign campaign)
149150
return campaignDto;
150151
}
151152

152-
private Campaign CampaignDtoToModel(CampaignDTO campaignDto)
153+
private Campaign MapCampaignDtoToModel(CampaignDTO campaignDto)
153154
{
154155
var campaingModel = new Campaign
155156
{
@@ -161,13 +162,13 @@ private Campaign CampaignDtoToModel(CampaignDTO campaignDto)
161162

162163
campaignDto.Rules.ForEach(c =>
163164
{
164-
switch (c.RuleType)
165+
switch (RuleType.From(c.RuleTypeId))
165166
{
166167
case RuleTypeEnum.UserLocationRule:
167168
campaingModel.Rules.Add(new UserLocationRule
168169
{
169170
LocationId = c.LocationId.Value,
170-
RuleTypeId = (int)c.RuleType,
171+
RuleTypeId = c.RuleTypeId,
171172
Description = c.Description,
172173
Campaign = campaingModel
173174
});
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
1+
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
27
{
38
public class RuleDTO
49
{
510
public int Id { get; set; }
611

7-
public RuleTypeEnum RuleType => (RuleTypeEnum) RuleTypeId;
8-
912
public int RuleTypeId { get; set; }
1013

1114
public int CampaignId { get; set; }
@@ -14,6 +17,4 @@ public class RuleDTO
1417

1518
public string Description { get; set; }
1619
}
17-
18-
public enum RuleTypeEnum { UserProfileRule = 1, PurchaseHistoryRule = 2, UserLocationRule = 3 }
1920
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions;
2+
using System;
3+
4+
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
5+
{
6+
public enum RuleTypeEnum { UserProfileRule = 1, PurchaseHistoryRule = 2, UserLocationRule = 3 }
7+
8+
public static class RuleType
9+
{
10+
public static RuleTypeEnum From(int id)
11+
{
12+
if (!Enum.IsDefined(typeof(RuleTypeEnum), id))
13+
{
14+
throw new MarketingDomainException($"Invalid value for RuleType, RuleTypeId: {id}");
15+
}
16+
17+
return (RuleTypeEnum)id;
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)