-
Notifications
You must be signed in to change notification settings - Fork 95
/
Order.cs
73 lines (60 loc) · 2.43 KB
/
Order.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
67
68
69
70
71
72
73
// Copyright (c) 2021 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
// Licensed under MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using DataLayer.Dtos;
using StatusGeneric;
namespace DataLayer.EfClasses
{
public class Order
{
private HashSet<LineItem> _lineItems;
private Order() { }
public int OrderId { get; private set; }
public DateTime DateOrderedUtc { get; private set; }
public DateTime ExpectedDeliveryDate { get; private set; }
public bool HasBeenDelivered { get; private set; }
public string CustomerName { get; private set; }
// relationships
public IEnumerable<LineItem> LineItems => _lineItems?.ToList();
public string OrderNumber => $"SO{OrderId:D6}";
public static IStatusGeneric<Order> CreateOrder(
string customerName, DateTime expectedDeliveryDate,
IEnumerable<OrderBooksDto> bookOrders)
{
var status = new StatusGenericHandler<Order>();
var order = new Order
{
CustomerName = customerName,
ExpectedDeliveryDate = expectedDeliveryDate,
DateOrderedUtc = DateTime.UtcNow,
HasBeenDelivered = expectedDeliveryDate < DateTime.Today
};
byte lineNum = 1;
order._lineItems = new HashSet<LineItem>(bookOrders
.Select(x => new LineItem(x.numBooks, x.ChosenBook, lineNum++)));
if (!order._lineItems.Any())
status.AddError("No items in your basket.");
return status.SetResult(order); //don't worry, the Result will return default(T) if there are errors
}
public IStatusGeneric ChangeDeliveryDate(string userId, DateTime newDeliveryDate)
{
var status = new StatusGenericHandler();
if (CustomerName != userId)
{
status.AddError("I'm sorry, but there was a system error.");
//Log a security issue
return status;
}
if (HasBeenDelivered)
{
status.AddError("I'm sorry, but that order has been delivered.");
return status;
}
//All Ok
ExpectedDeliveryDate = newDeliveryDate;
return status;
}
}
}