-
Notifications
You must be signed in to change notification settings - Fork 30
/
Order.PriceAgreement.cs
executable file
·66 lines (54 loc) · 2.4 KB
/
Order.PriceAgreement.cs
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
57
58
59
60
61
62
63
64
65
66
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using MyCompany.ECommerce.Sales.Commons;
using MyCompany.ECommerce.TechnicalStuff;
using P3Model.Annotations.Domain.DDD;
namespace MyCompany.ECommerce.Sales.Orders;
public partial class Order
{
[DddValueObject]
public class PriceAgreement : IEquatable<PriceAgreement>
{
public PriceAgreementType Type { get; }
public Money? Price { get; }
public DateTime? ExpiresOn { get; }
public static PriceAgreement Non() => new(PriceAgreementType.Non, null, default);
public static PriceAgreement Temporary(Money price, DateTime expiresOn) =>
new(PriceAgreementType.Temporary, price, expiresOn);
public static PriceAgreement Final(Money price) => new(PriceAgreementType.Final, price, default);
[JsonConstructor]
//https://github.com/dotnet/runtime/issues/44428
public PriceAgreement(PriceAgreementType type, Money? price, DateTime? expiresOn)
{
Type = type;
Price = price;
ExpiresOn = expiresOn;
}
[SuppressMessage("ReSharper", "UnusedMember.Local", Justification = "EF")]
private PriceAgreement() { }
public bool CanChangePrice() => Type switch
{
PriceAgreementType.Non => true,
PriceAgreementType.Temporary => true,
PriceAgreementType.Final => false,
_ => throw new ArgumentOutOfRangeException(nameof(Type), Type, null)
};
public bool IsValidOn(DateTime date) => Type switch
{
PriceAgreementType.Non => false,
PriceAgreementType.Temporary => ExpiresOn >= date,
PriceAgreementType.Final => true,
_ => throw new ArgumentOutOfRangeException(nameof(Type), Type, null)
};
public bool Equals(PriceAgreement? other) => other is not null &&
Type == other.Type &&
Price == other.Price &&
ExpiresOn == other.ExpiresOn;
public override bool Equals(object? obj) => obj is PriceAgreement other && Equals(other);
public override int GetHashCode() => new HashCode()
.CombineWith(Type)
.CombineWith(Price)
.CombineWith(ExpiresOn)
.ToHashCode();
}
}