scm-api / src /SCM.Domain /Projects /Project.cs
rafaeldavila's picture
Deploy SCM API (.NET 8) - Docker Space
de8b914 verified
Raw History Blame Contribute Delete
2.44 kB
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.");
}
}