diff --git a/src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs b/src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs index 1786bc3b7..41773a78c 100644 --- a/src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs +++ b/src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs @@ -8,9 +8,10 @@ using Microsoft.eShopOnContainers.Services.Marketing.API.Dto; using System.Collections.Generic; using Microsoft.AspNetCore.Authorization; + using System.Linq; [Route("api/v1/[controller]")] - [Authorize] + //[Authorize] public class CampaignsController : Controller { private readonly MarketingContext _context; @@ -27,7 +28,7 @@ .Include(c => c.Rules) .ToListAsync(); - var campaignDtoList = CampaignModelListToDtoList(campaignList); + var campaignDtoList = MapCampaignModelListToDtoList(campaignList); return Ok(campaignDtoList); } @@ -44,7 +45,7 @@ return NotFound(); } - var campaignDto = CampaignModelToDto(campaign); + var campaignDto = MapCampaignModelToDto(campaign); return Ok(campaignDto); } @@ -57,7 +58,7 @@ return BadRequest(); } - var campaingToCreate = CampaignDtoToModel(campaign); + var campaingToCreate = MapCampaignDtoToModel(campaign); await _context.Campaigns.AddAsync(campaingToCreate); await _context.SaveChangesAsync(); @@ -110,17 +111,17 @@ - private List CampaignModelListToDtoList(List campaignList) + private List MapCampaignModelListToDtoList(List campaignList) { var campaignDtoList = new List(); campaignList.ForEach(campaign => campaignDtoList - .Add(CampaignModelToDto(campaign))); + .Add(MapCampaignModelToDto(campaign))); return campaignDtoList; } - private CampaignDTO CampaignModelToDto(Campaign campaign) + private CampaignDTO MapCampaignModelToDto(Campaign campaign) { var campaignDto = new CampaignDTO { @@ -132,7 +133,7 @@ campaign.Rules.ForEach(c => { - switch ((RuleTypeEnum)c.RuleTypeId) + switch (RuleType.From(c.RuleTypeId)) { case RuleTypeEnum.UserLocationRule: var userLocationRule = c as UserLocationRule; @@ -149,7 +150,7 @@ return campaignDto; } - private Campaign CampaignDtoToModel(CampaignDTO campaignDto) + private Campaign MapCampaignDtoToModel(CampaignDTO campaignDto) { var campaingModel = new Campaign { @@ -161,13 +162,13 @@ campaignDto.Rules.ForEach(c => { - switch (c.RuleType) + switch (RuleType.From(c.RuleTypeId)) { case RuleTypeEnum.UserLocationRule: campaingModel.Rules.Add(new UserLocationRule { LocationId = c.LocationId.Value, - RuleTypeId = (int)c.RuleType, + RuleTypeId = c.RuleTypeId, Description = c.Description, Campaign = campaingModel }); diff --git a/src/Services/Marketing/Marketing.API/Dto/RuleDTO.cs b/src/Services/Marketing/Marketing.API/Dto/RuleDTO.cs index 6a1d9aef6..a79756e9c 100644 --- a/src/Services/Marketing/Marketing.API/Dto/RuleDTO.cs +++ b/src/Services/Marketing/Marketing.API/Dto/RuleDTO.cs @@ -1,11 +1,14 @@ -namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto +using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto { public class RuleDTO { public int Id { get; set; } - public RuleTypeEnum RuleType => (RuleTypeEnum) RuleTypeId; - public int RuleTypeId { get; set; } public int CampaignId { get; set; } @@ -14,6 +17,4 @@ public string Description { get; set; } } - - public enum RuleTypeEnum { UserProfileRule = 1, PurchaseHistoryRule = 2, UserLocationRule = 3 } } \ No newline at end of file diff --git a/src/Services/Marketing/Marketing.API/Dto/RuleTypeEnum.cs b/src/Services/Marketing/Marketing.API/Dto/RuleTypeEnum.cs new file mode 100644 index 000000000..f00b7fbf9 --- /dev/null +++ b/src/Services/Marketing/Marketing.API/Dto/RuleTypeEnum.cs @@ -0,0 +1,20 @@ +using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions; +using System; + +namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto +{ + public enum RuleTypeEnum { UserProfileRule = 1, PurchaseHistoryRule = 2, UserLocationRule = 3 } + + public static class RuleType + { + public static RuleTypeEnum From(int id) + { + if (!Enum.IsDefined(typeof(RuleTypeEnum), id)) + { + throw new MarketingDomainException($"Invalid value for RuleType, RuleTypeId: {id}"); + } + + return (RuleTypeEnum)id; + } + } +} \ No newline at end of file