Modify RuleType class
This commit is contained in:
parent
e5a59a1bdf
commit
8d170fe83f
51
src/Services/Marketing/Marketing.API/Model/RuleType.cs
Normal file
51
src/Services/Marketing/Marketing.API/Model/RuleType.cs
Normal file
@ -0,0 +1,51 @@
|
||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Model
|
||||
{
|
||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public sealed class RuleType
|
||||
{
|
||||
public static readonly RuleType UserProfileRule = new RuleType(1, nameof(UserProfileRule));
|
||||
public static readonly RuleType PurchaseHistoryRule = new RuleType(2, nameof(UserProfileRule));
|
||||
public static readonly RuleType UserLocationRule = new RuleType(3, nameof(UserProfileRule));
|
||||
|
||||
public readonly int Id;
|
||||
public readonly string Name;
|
||||
|
||||
private RuleType(int id, string name)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public static IEnumerable<RuleType> List() =>
|
||||
new[] { UserProfileRule, PurchaseHistoryRule, UserLocationRule };
|
||||
|
||||
public static RuleType FromName(string name)
|
||||
{
|
||||
var state = List()
|
||||
.SingleOrDefault(s => String.Equals(s.Name, name, StringComparison.CurrentCultureIgnoreCase));
|
||||
|
||||
if (state == null)
|
||||
{
|
||||
throw new MarketingDomainException($"Possible values for RuleType: {String.Join(",", List().Select(s => s.Name))}");
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
public static RuleType From(int id)
|
||||
{
|
||||
var state = List().SingleOrDefault(s => s.Id == id);
|
||||
|
||||
if (state == null)
|
||||
{
|
||||
throw new MarketingDomainException($"Possible values for RuleType: {String.Join(",", List().Select(s => s.Name))}");
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Model
|
||||
{
|
||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions;
|
||||
using System;
|
||||
|
||||
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