This repository was archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Expand file tree
/
Copy pathOrderItem.cs
More file actions
83 lines (67 loc) · 2.24 KB
/
OrderItem.cs
File metadata and controls
83 lines (67 loc) · 2.24 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork;
using Ordering.Domain.Exceptions;
using System;
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate
{
public class OrderItem
: Entity
{
// DDD Patterns comment
// Using private fields, allowed since EF Core 1.1, is a much better encapsulation
// aligned with DDD Aggregates and Domain Entities (Instead of properties and property collections)
private string _productName;
private string _pictureUrl;
private decimal _unitPrice;
private decimal _discount;
private int _units;
public int ProductId { get; private set; }
protected OrderItem() { }
public OrderItem(int productId, string productName, decimal unitPrice, decimal discount, string PictureUrl, int units = 1)
{
if (units <= 0)
{
throw new OrderingDomainException("Invalid number of units");
}
if ((unitPrice * units) < discount)
{
throw new OrderingDomainException("The total of order item is lower than applied discount");
}
ProductId = productId;
_productName = productName;
_unitPrice = unitPrice;
_discount = discount;
_units = units;
_pictureUrl = PictureUrl;
}
public string GetPictureUri() => _pictureUrl;
public decimal GetDiscount()
{
return _discount;
}
public int GetUnits()
{
return _units;
}
public decimal GetUnitPrice()
{
return _unitPrice;
}
public string GetOrderItemProductName() => _productName;
public void SetNewDiscount(decimal discount)
{
if (discount < 0)
{
throw new OrderingDomainException("Discount is not valid");
}
_discount = discount;
}
public void AddUnits(int units)
{
if (units < 0)
{
throw new OrderingDomainException("Invalid units");
}
_units += units;
}
}
}