Modify RuleType class

This commit is contained in:
Christian Arenas 2017-06-13 14:32:08 +02:00
parent e5a59a1bdf
commit 8d170fe83f
2 changed files with 51 additions and 20 deletions

View 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;
}
}
}

View File

@ -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;
}
}
}