Create RuleType class to manage RuleTypeEnum
This commit is contained in:
parent
2111d56766
commit
7842194637
@ -8,9 +8,10 @@
|
|||||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Dto;
|
using Microsoft.eShopOnContainers.Services.Marketing.API.Dto;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
[Route("api/v1/[controller]")]
|
[Route("api/v1/[controller]")]
|
||||||
[Authorize]
|
//[Authorize]
|
||||||
public class CampaignsController : Controller
|
public class CampaignsController : Controller
|
||||||
{
|
{
|
||||||
private readonly MarketingContext _context;
|
private readonly MarketingContext _context;
|
||||||
@ -27,7 +28,7 @@
|
|||||||
.Include(c => c.Rules)
|
.Include(c => c.Rules)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
var campaignDtoList = CampaignModelListToDtoList(campaignList);
|
var campaignDtoList = MapCampaignModelListToDtoList(campaignList);
|
||||||
|
|
||||||
return Ok(campaignDtoList);
|
return Ok(campaignDtoList);
|
||||||
}
|
}
|
||||||
@ -44,7 +45,7 @@
|
|||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
var campaignDto = CampaignModelToDto(campaign);
|
var campaignDto = MapCampaignModelToDto(campaign);
|
||||||
|
|
||||||
return Ok(campaignDto);
|
return Ok(campaignDto);
|
||||||
}
|
}
|
||||||
@ -57,7 +58,7 @@
|
|||||||
return BadRequest();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
var campaingToCreate = CampaignDtoToModel(campaign);
|
var campaingToCreate = MapCampaignDtoToModel(campaign);
|
||||||
|
|
||||||
await _context.Campaigns.AddAsync(campaingToCreate);
|
await _context.Campaigns.AddAsync(campaingToCreate);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
@ -110,17 +111,17 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
private List<CampaignDTO> CampaignModelListToDtoList(List<Campaign> campaignList)
|
private List<CampaignDTO> MapCampaignModelListToDtoList(List<Campaign> campaignList)
|
||||||
{
|
{
|
||||||
var campaignDtoList = new List<CampaignDTO>();
|
var campaignDtoList = new List<CampaignDTO>();
|
||||||
|
|
||||||
campaignList.ForEach(campaign => campaignDtoList
|
campaignList.ForEach(campaign => campaignDtoList
|
||||||
.Add(CampaignModelToDto(campaign)));
|
.Add(MapCampaignModelToDto(campaign)));
|
||||||
|
|
||||||
return campaignDtoList;
|
return campaignDtoList;
|
||||||
}
|
}
|
||||||
|
|
||||||
private CampaignDTO CampaignModelToDto(Campaign campaign)
|
private CampaignDTO MapCampaignModelToDto(Campaign campaign)
|
||||||
{
|
{
|
||||||
var campaignDto = new CampaignDTO
|
var campaignDto = new CampaignDTO
|
||||||
{
|
{
|
||||||
@ -132,7 +133,7 @@
|
|||||||
|
|
||||||
campaign.Rules.ForEach(c =>
|
campaign.Rules.ForEach(c =>
|
||||||
{
|
{
|
||||||
switch ((RuleTypeEnum)c.RuleTypeId)
|
switch (RuleType.From(c.RuleTypeId))
|
||||||
{
|
{
|
||||||
case RuleTypeEnum.UserLocationRule:
|
case RuleTypeEnum.UserLocationRule:
|
||||||
var userLocationRule = c as UserLocationRule;
|
var userLocationRule = c as UserLocationRule;
|
||||||
@ -149,7 +150,7 @@
|
|||||||
return campaignDto;
|
return campaignDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Campaign CampaignDtoToModel(CampaignDTO campaignDto)
|
private Campaign MapCampaignDtoToModel(CampaignDTO campaignDto)
|
||||||
{
|
{
|
||||||
var campaingModel = new Campaign
|
var campaingModel = new Campaign
|
||||||
{
|
{
|
||||||
@ -161,13 +162,13 @@
|
|||||||
|
|
||||||
campaignDto.Rules.ForEach(c =>
|
campaignDto.Rules.ForEach(c =>
|
||||||
{
|
{
|
||||||
switch (c.RuleType)
|
switch (RuleType.From(c.RuleTypeId))
|
||||||
{
|
{
|
||||||
case RuleTypeEnum.UserLocationRule:
|
case RuleTypeEnum.UserLocationRule:
|
||||||
campaingModel.Rules.Add(new UserLocationRule
|
campaingModel.Rules.Add(new UserLocationRule
|
||||||
{
|
{
|
||||||
LocationId = c.LocationId.Value,
|
LocationId = c.LocationId.Value,
|
||||||
RuleTypeId = (int)c.RuleType,
|
RuleTypeId = c.RuleTypeId,
|
||||||
Description = c.Description,
|
Description = c.Description,
|
||||||
Campaign = campaingModel
|
Campaign = campaingModel
|
||||||
});
|
});
|
||||||
|
@ -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 class RuleDTO
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
||||||
public RuleTypeEnum RuleType => (RuleTypeEnum) RuleTypeId;
|
|
||||||
|
|
||||||
public int RuleTypeId { get; set; }
|
public int RuleTypeId { get; set; }
|
||||||
|
|
||||||
public int CampaignId { get; set; }
|
public int CampaignId { get; set; }
|
||||||
@ -14,6 +17,4 @@
|
|||||||
|
|
||||||
public string Description { get; set; }
|
public string Description { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum RuleTypeEnum { UserProfileRule = 1, PurchaseHistoryRule = 2, UserLocationRule = 3 }
|
|
||||||
}
|
}
|
20
src/Services/Marketing/Marketing.API/Dto/RuleTypeEnum.cs
Normal file
20
src/Services/Marketing/Marketing.API/Dto/RuleTypeEnum.cs
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user