Browse Source

Modify RuleType class

pull/223/head
Christian Arenas 7 years ago
parent
commit
2ae6744c8e
2 changed files with 51 additions and 20 deletions
  1. +51
    -0
      src/Services/Marketing/Marketing.API/Model/RuleType.cs
  2. +0
    -20
      src/Services/Marketing/Marketing.API/Model/RuleTypeEnum.cs

+ 51
- 0
src/Services/Marketing/Marketing.API/Model/RuleType.cs 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;
}
}
}

+ 0
- 20
src/Services/Marketing/Marketing.API/Model/RuleTypeEnum.cs 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;
}
}
}

Loading…
Cancel
Save