-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDecreaseQuotaMutation.cs
More file actions
56 lines (45 loc) · 1.71 KB
/
Copy pathDecreaseQuotaMutation.cs
File metadata and controls
56 lines (45 loc) · 1.71 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
56
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 decreases the quota for specific user by specified amount.
/// </summary>
internal sealed class DecreaseQuotaMutation(
string userId,
int amount,
MutationContext context
) : MutationBase<QuotaState>(
CreateIntent(
operationName: "DecreaseQuota",
category: "Billing",
description: "Decrease user quota by given amount",
riskLevel: MutationRiskLevel.High),
context)
{
public string UserId { get; } = userId;
public int Amount { get; } = amount;
public override 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");
if (state.UserQuotas.GetValueOrDefault(UserId) < Amount)
result.AddError("Amount", "Cannot decrease below zero");
return result;
}
public override 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 };
return Success(
newState,
StateChange.Modified($"UserQuotas.{UserId}", null, quotas[UserId]));
}
}