-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoard.cs
130 lines (85 loc) · 3.84 KB
/
Board.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using Tacta.EventStore.Domain;
using Tactify.Core.Boards.DomainEvents;
using Tactify.Core.Boards.Entities;
using Tactify.Core.Boards.Exceptions;
using Tactify.Core.Boards.ValueObjects;
namespace Tactify.Core.Boards
{
public sealed class Board : AggregateRoot<BoardId>
{
public override BoardId Id { get; protected set; }
public bool IsArchived { get; private set; } = false;
private List<Sprint> Sprints { get; set; } = new List<Sprint>();
private Sprint? ActiveSprint => Sprints.SingleOrDefault(x => x.Status == SprintStatus.Active);
private Sprint? NextSprintToStart => Sprints.OrderBy(x => x.Id.SprintNumber).FirstOrDefault(x => x.Status == SprintStatus.Created);
private int NewSprintNumber => Sprints.Any() ? Sprints.Max(x => x.Id.SprintNumber) + 1 : 1;
public Sprint SprintById(string sprintId) => Sprints.Single(x => x.Id.ToString() == sprintId);
private Board()
{
}
public Board(IEnumerable<IDomainEvent> events) : base(events)
{
}
public static Board CreateBoard(BoardInfo boardInfo)
{
var board = new Board();
var boardId = new BoardId(boardInfo.Name);
var @event = new BoardCreated(boardId.ToString(), boardInfo.Description, boardInfo.CreatedBy);
board.Apply(@event);
return board;
}
public void CreateNewSprint(string createdBy)
{
if (IsArchived) throw new CannotCreateNewSprintException($"Board {Id} is archived.");
var sprintId = new SprintId(NewSprintNumber);
var @event = new SprintCreated(Id.ToString(), sprintId.ToString(), createdBy);
Apply(@event);
}
public void StartNextSprint(string createdBy)
{
if (IsArchived) throw new CannotStartNextSprintException($"Board {Id} is archived.");
if (ActiveSprint != null) throw new CannotStartNextSprintException($"There is already active sprint {ActiveSprint.Id}.");
if (NextSprintToStart == null) throw new CannotStartNextSprintException($"There is no created sprint to start.");
var @event = new SprintStarted(Id.ToString(), NextSprintToStart.Id.ToString(), createdBy);
Apply(@event);
}
public void EndActiveSprint(string createdBy)
{
if (IsArchived) throw new CannotEndActiveSprintException($"Board {Id} is archived.");
if (ActiveSprint == null) throw new CannotEndActiveSprintException($"There is no active sprint to end.");
var @event = new SprintEnded(Id.ToString(), ActiveSprint.Id.ToString(), createdBy);
Apply(@event);
}
public void ArchiveBoard(string createdBy)
{
if (IsArchived) throw new CannotArchiveBoardException($"Board {Id} is already archived.");
if (Sprints.Any(x => x.Status != SprintStatus.Ended)) throw new CannotArchiveBoardException($"Not all sprints ended on the board {Id}.");
var @event = new BoardArchived(Id.ToString(), createdBy);
Apply(@event);
}
public void On(BoardCreated @event)
{
Id = BoardId.Identity(@event.AggregateId);
}
public void On(SprintCreated @event)
{
var sprintId = SprintId.Identity(@event.SprintId);
var sprint = new Sprint(sprintId);
Sprints.Add(sprint);
}
public void On(SprintStarted @event)
{
var sprint = SprintById(@event.SprintId);
sprint.SprintStarted();
}
public void On(SprintEnded @event)
{
var sprint = SprintById(@event.SprintId);
sprint.SprintEnded();
}
public void On(BoardArchived _)
{
IsArchived = true;
}
}
}