-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIncreaseQuotaMutation.cs
More file actions
55 lines (44 loc) · 1.66 KB
/
Copy pathIncreaseQuotaMutation.cs
File metadata and controls
55 lines (44 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using BillingQuotas.State;
using ModularityKit.Mutator.Abstractions.Changes;
using ModularityKit.Mutator.Abstractions.Context;
using ModularityKit.Mutator.Abstractions.Engine;
using ModularityKit.Mutator.Abstractions.Intent;
using ModularityKit.Mutator.Abstractions.Results;
namespace BillingQuotas.Mutations;
/// <summary>
/// Mutation that increases the quota for specific user by given amount.
/// </summary>
internal sealed record IncreaseQuotaMutation(
string UserId,
int Amount,
MutationContext Context
) : IMutation<QuotaState>
{
public MutationIntent Intent { get; } = new()
{
OperationName = "IncreaseQuota",
Category = "Billing",
RiskLevel = MutationRiskLevel.Medium,
Description = "Increase user quota by given amount"
};
public ValidationResult Validate(QuotaState state)
{
var result = new ValidationResult();
if (string.IsNullOrEmpty(UserId))
result.AddError("UserId", "UserId cannot be empty");
if (Amount <= 0)
result.AddError("Amount", "Amount must be positive");
return result;
}
public MutationResult<QuotaState> Apply(QuotaState state)
{
var quotas = state.UserQuotas.ToDictionary(kv => kv.Key, kv => kv.Value);
quotas[UserId] = quotas.GetValueOrDefault(UserId) + Amount;
var newState = state with { UserQuotas = quotas };
var changes = ChangeSet.Single(
StateChange.Modified($"UserQuotas.{UserId}", null, quotas[UserId])
);
return MutationResult<QuotaState>.Success(newState, changes);
}
public MutationResult<QuotaState> Simulate(QuotaState state) => Apply(state);
}