First commit. Include junk from template to remove
This commit is contained in:
12
src/Domain/Common/BaseAuditableEntity.cs
Normal file
12
src/Domain/Common/BaseAuditableEntity.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace Hutopy.Domain.Common;
|
||||
|
||||
public abstract class BaseAuditableEntity : BaseEntity
|
||||
{
|
||||
public DateTimeOffset Created { get; set; }
|
||||
|
||||
public string? CreatedBy { get; set; }
|
||||
|
||||
public DateTimeOffset LastModified { get; set; }
|
||||
|
||||
public string? LastModifiedBy { get; set; }
|
||||
}
|
||||
30
src/Domain/Common/BaseEntity.cs
Normal file
30
src/Domain/Common/BaseEntity.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Hutopy.Domain.Common;
|
||||
|
||||
public abstract class BaseEntity
|
||||
{
|
||||
// This can easily be modified to be BaseEntity<T> and public T Id to support different key types.
|
||||
// Using non-generic integer types for simplicity
|
||||
public int Id { get; set; }
|
||||
|
||||
private readonly List<BaseEvent> _domainEvents = new();
|
||||
|
||||
[NotMapped]
|
||||
public IReadOnlyCollection<BaseEvent> DomainEvents => _domainEvents.AsReadOnly();
|
||||
|
||||
public void AddDomainEvent(BaseEvent domainEvent)
|
||||
{
|
||||
_domainEvents.Add(domainEvent);
|
||||
}
|
||||
|
||||
public void RemoveDomainEvent(BaseEvent domainEvent)
|
||||
{
|
||||
_domainEvents.Remove(domainEvent);
|
||||
}
|
||||
|
||||
public void ClearDomainEvents()
|
||||
{
|
||||
_domainEvents.Clear();
|
||||
}
|
||||
}
|
||||
7
src/Domain/Common/BaseEvent.cs
Normal file
7
src/Domain/Common/BaseEvent.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using MediatR;
|
||||
|
||||
namespace Hutopy.Domain.Common;
|
||||
|
||||
public abstract class BaseEvent : INotification
|
||||
{
|
||||
}
|
||||
45
src/Domain/Common/ValueObject.cs
Normal file
45
src/Domain/Common/ValueObject.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
namespace Hutopy.Domain.Common;
|
||||
|
||||
// Learn more: https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/microservice-ddd-cqrs-patterns/implement-value-objects
|
||||
public abstract class ValueObject
|
||||
{
|
||||
protected static bool EqualOperator(ValueObject left, ValueObject right)
|
||||
{
|
||||
if (left is null ^ right is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return left?.Equals(right!) != false;
|
||||
}
|
||||
|
||||
protected static bool NotEqualOperator(ValueObject left, ValueObject right)
|
||||
{
|
||||
return !(EqualOperator(left, right));
|
||||
}
|
||||
|
||||
protected abstract IEnumerable<object> GetEqualityComponents();
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (obj == null || obj.GetType() != GetType())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var other = (ValueObject)obj;
|
||||
return GetEqualityComponents().SequenceEqual(other.GetEqualityComponents());
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
var hash = new HashCode();
|
||||
|
||||
foreach (var component in GetEqualityComponents())
|
||||
{
|
||||
hash.Add(component);
|
||||
}
|
||||
|
||||
return hash.ToHashCode();
|
||||
}
|
||||
}
|
||||
7
src/Domain/Constants/Policies.cs
Normal file
7
src/Domain/Constants/Policies.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Hutopy.Domain.Constants;
|
||||
|
||||
public abstract class Policies
|
||||
{
|
||||
public const string CanPurge = nameof(CanPurge);
|
||||
public const string CanDelete = nameof(CanDelete);
|
||||
}
|
||||
6
src/Domain/Constants/Roles.cs
Normal file
6
src/Domain/Constants/Roles.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Hutopy.Domain.Constants;
|
||||
|
||||
public abstract class Roles
|
||||
{
|
||||
public const string Administrator = nameof(Administrator);
|
||||
}
|
||||
12
src/Domain/Domain.csproj
Normal file
12
src/Domain/Domain.csproj
Normal file
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<RootNamespace>Hutopy.Domain</RootNamespace>
|
||||
<AssemblyName>Hutopy.Domain</AssemblyName>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MediatR" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
31
src/Domain/Entities/TodoItem.cs
Normal file
31
src/Domain/Entities/TodoItem.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
namespace Hutopy.Domain.Entities;
|
||||
|
||||
public class TodoItem : BaseAuditableEntity
|
||||
{
|
||||
public int ListId { get; set; }
|
||||
|
||||
public string? Title { get; set; }
|
||||
|
||||
public string? Note { get; set; }
|
||||
|
||||
public PriorityLevel Priority { get; set; }
|
||||
|
||||
public DateTime? Reminder { get; set; }
|
||||
|
||||
private bool _done;
|
||||
public bool Done
|
||||
{
|
||||
get => _done;
|
||||
set
|
||||
{
|
||||
if (value && !_done)
|
||||
{
|
||||
AddDomainEvent(new TodoItemCompletedEvent(this));
|
||||
}
|
||||
|
||||
_done = value;
|
||||
}
|
||||
}
|
||||
|
||||
public TodoList List { get; set; } = null!;
|
||||
}
|
||||
10
src/Domain/Entities/TodoList.cs
Normal file
10
src/Domain/Entities/TodoList.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Hutopy.Domain.Entities;
|
||||
|
||||
public class TodoList : BaseAuditableEntity
|
||||
{
|
||||
public string? Title { get; set; }
|
||||
|
||||
public Colour Colour { get; set; } = Colour.White;
|
||||
|
||||
public IList<TodoItem> Items { get; private set; } = new List<TodoItem>();
|
||||
}
|
||||
9
src/Domain/Enums/PriorityLevel.cs
Normal file
9
src/Domain/Enums/PriorityLevel.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Hutopy.Domain.Enums;
|
||||
|
||||
public enum PriorityLevel
|
||||
{
|
||||
None = 0,
|
||||
Low = 1,
|
||||
Medium = 2,
|
||||
High = 3
|
||||
}
|
||||
11
src/Domain/Events/TodoItemCompletedEvent.cs
Normal file
11
src/Domain/Events/TodoItemCompletedEvent.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace Hutopy.Domain.Events;
|
||||
|
||||
public class TodoItemCompletedEvent : BaseEvent
|
||||
{
|
||||
public TodoItemCompletedEvent(TodoItem item)
|
||||
{
|
||||
Item = item;
|
||||
}
|
||||
|
||||
public TodoItem Item { get; }
|
||||
}
|
||||
11
src/Domain/Events/TodoItemCreatedEvent.cs
Normal file
11
src/Domain/Events/TodoItemCreatedEvent.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace Hutopy.Domain.Events;
|
||||
|
||||
public class TodoItemCreatedEvent : BaseEvent
|
||||
{
|
||||
public TodoItemCreatedEvent(TodoItem item)
|
||||
{
|
||||
Item = item;
|
||||
}
|
||||
|
||||
public TodoItem Item { get; }
|
||||
}
|
||||
11
src/Domain/Events/TodoItemDeletedEvent.cs
Normal file
11
src/Domain/Events/TodoItemDeletedEvent.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace Hutopy.Domain.Events;
|
||||
|
||||
public class TodoItemDeletedEvent : BaseEvent
|
||||
{
|
||||
public TodoItemDeletedEvent(TodoItem item)
|
||||
{
|
||||
Item = item;
|
||||
}
|
||||
|
||||
public TodoItem Item { get; }
|
||||
}
|
||||
9
src/Domain/Exceptions/UnsupportedColourException.cs
Normal file
9
src/Domain/Exceptions/UnsupportedColourException.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Hutopy.Domain.Exceptions;
|
||||
|
||||
public class UnsupportedColourException : Exception
|
||||
{
|
||||
public UnsupportedColourException(string code)
|
||||
: base($"Colour \"{code}\" is unsupported.")
|
||||
{
|
||||
}
|
||||
}
|
||||
6
src/Domain/GlobalUsings.cs
Normal file
6
src/Domain/GlobalUsings.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
global using Hutopy.Domain.Common;
|
||||
global using Hutopy.Domain.Entities;
|
||||
global using Hutopy.Domain.Enums;
|
||||
global using Hutopy.Domain.Events;
|
||||
global using Hutopy.Domain.Exceptions;
|
||||
global using Hutopy.Domain.ValueObjects;
|
||||
69
src/Domain/ValueObjects/Colour.cs
Normal file
69
src/Domain/ValueObjects/Colour.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
namespace Hutopy.Domain.ValueObjects;
|
||||
|
||||
public class Colour(string code) : ValueObject
|
||||
{
|
||||
public static Colour From(string code)
|
||||
{
|
||||
var colour = new Colour(code);
|
||||
|
||||
if (!SupportedColours.Contains(colour))
|
||||
{
|
||||
throw new UnsupportedColourException(code);
|
||||
}
|
||||
|
||||
return colour;
|
||||
}
|
||||
|
||||
public static Colour White => new("#FFFFFF");
|
||||
|
||||
public static Colour Red => new("#FF5733");
|
||||
|
||||
public static Colour Orange => new("#FFC300");
|
||||
|
||||
public static Colour Yellow => new("#FFFF66");
|
||||
|
||||
public static Colour Green => new("#CCFF99");
|
||||
|
||||
public static Colour Blue => new("#6666FF");
|
||||
|
||||
public static Colour Purple => new("#9966CC");
|
||||
|
||||
public static Colour Grey => new("#999999");
|
||||
|
||||
public string Code { get; private set; } = string.IsNullOrWhiteSpace(code)?"#000000":code;
|
||||
|
||||
public static implicit operator string(Colour colour)
|
||||
{
|
||||
return colour.ToString();
|
||||
}
|
||||
|
||||
public static explicit operator Colour(string code)
|
||||
{
|
||||
return From(code);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Code;
|
||||
}
|
||||
|
||||
protected static IEnumerable<Colour> SupportedColours
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return White;
|
||||
yield return Red;
|
||||
yield return Orange;
|
||||
yield return Yellow;
|
||||
yield return Green;
|
||||
yield return Blue;
|
||||
yield return Purple;
|
||||
yield return Grey;
|
||||
}
|
||||
}
|
||||
|
||||
protected override IEnumerable<object> GetEqualityComponents()
|
||||
{
|
||||
yield return Code;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user