File size: 2,435 Bytes
de8b914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using SCM.Shared.Exceptions;

namespace SCM.Domain.Projects;

/// <summary>
/// Obra. Entidade-raiz do domínio de projetos.
/// Invariantes são garantidas pelo factory <see cref="Create"/> e por <see cref="UpdateDetails"/>.
/// As propriedades têm setter público apenas para permitir a hidratação pelo Dapper.
/// </summary>
public sealed class Project
{
    public Guid Id { get; set; }
    public string Code { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public string? Description { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public ProjectStatus Status { get; set; } = ProjectStatus.Planned;
    public DateTime CreatedAt { get; set; }
    public DateTime? UpdatedAt { get; set; }

    public static Project Create(
        string code,
        string name,
        string? description,
        DateTime startDate,
        DateTime? endDate,
        DateTime nowUtc)
    {
        Validate(code, name, startDate, endDate);

        return new Project
        {
            Id = Guid.NewGuid(),
            Code = code.Trim(),
            Name = name.Trim(),
            Description = string.IsNullOrWhiteSpace(description) ? null : description.Trim(),
            StartDate = startDate,
            EndDate = endDate,
            Status = ProjectStatus.Planned,
            CreatedAt = nowUtc
        };
    }

    public void UpdateDetails(
        string name,
        string? description,
        DateTime startDate,
        DateTime? endDate,
        ProjectStatus status,
        DateTime nowUtc)
    {
        Validate(Code, name, startDate, endDate);

        Name = name.Trim();
        Description = string.IsNullOrWhiteSpace(description) ? null : description.Trim();
        StartDate = startDate;
        EndDate = endDate;
        Status = status;
        UpdatedAt = nowUtc;
    }

    private static void Validate(string code, string name, DateTime startDate, DateTime? endDate)
    {
        if (string.IsNullOrWhiteSpace(code))
            throw new DomainException("O código da obra é obrigatório.");
        if (string.IsNullOrWhiteSpace(name))
            throw new DomainException("O nome da obra é obrigatório.");
        if (endDate is not null && endDate.Value.Date < startDate.Date)
            throw new DomainException("A data de término não pode ser anterior à data de início.");
    }
}