Skip to content

Commit 8d170fe

Browse files
committed
Modify RuleType class
1 parent e5a59a1 commit 8d170fe

2 files changed

Lines changed: 51 additions & 20 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Model
2+
{
3+
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
8+
public sealed class RuleType
9+
{
10+
public static readonly RuleType UserProfileRule = new RuleType(1, nameof(UserProfileRule));
11+
public static readonly RuleType PurchaseHistoryRule = new RuleType(2, nameof(UserProfileRule));
12+
public static readonly RuleType UserLocationRule = new RuleType(3, nameof(UserProfileRule));
13+
14+
public readonly int Id;
15+
public readonly string Name;
16+
17+
private RuleType(int id, string name)
18+
{
19+
Id = id;
20+
Name = name;
21+
}
22+
23+
public static IEnumerable<RuleType> List() =>
24+
new[] { UserProfileRule, PurchaseHistoryRule, UserLocationRule };
25+
26+
public static RuleType FromName(string name)
27+
{
28+
var state = List()
29+
.SingleOrDefault(s => String.Equals(s.Name, name, StringComparison.CurrentCultureIgnoreCase));
30+
31+
if (state == null)
32+
{
33+
throw new MarketingDomainException($"Possible values for RuleType: {String.Join(",", List().Select(s => s.Name))}");
34+
}
35+
36+
return state;
37+
}
38+
39+
public static RuleType From(int id)
40+
{
41+
var state = List().SingleOrDefault(s => s.Id == id);
42+
43+
if (state == null)
44+
{
45+
throw new MarketingDomainException($"Possible values for RuleType: {String.Join(",", List().Select(s => s.Name))}");
46+
}
47+
48+
return state;
49+
}
50+
}
51+
}

src/Services/Marketing/Marketing.API/Model/RuleTypeEnum.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)