play with storing valueobjects

This commit is contained in:
Claudiu Farcas
2022-10-18 00:03:51 +03:00
parent 80ccd22d9c
commit b31cbcc15d
36 changed files with 250 additions and 81 deletions
@@ -0,0 +1,43 @@
using Ardalis.GuardClauses;
using PracticeCalendar.Domain.Common;
namespace PracticeCalendar.Domain.Entities.PracticeEvent
{
/// <summary>
/// Attendee to an event
/// </summary>
public class Attendee : EntityBase
{
public Attendee(string name, string emailAddress)
{
Guard.Against.NullOrEmpty(name);
Guard.Against.NullOrEmpty(emailAddress);
Name = name;
EmailAddress = emailAddress;
}
public int PracticeEventId { get; private set; }
public string Name { get; set; } = string.Empty;
public string EmailAddress { get; set; } = string.Empty;
public bool IsAttending { get; private set; }
/// <summary>
/// Set if the Attendee is attending
/// </summary>
/// <param name="isAttending"></param>
public void SetIsAttending(bool isAttending)
{
this.IsAttending = isAttending;
//TODO - raise event
}
/// <summary>
/// Assign Attendee to the practice event
/// </summary>
/// <param name="practiceEventId"></param>
public void AssignToEvent(int practiceEventId)
{
this.PracticeEventId = practiceEventId;
}
}
}
@@ -0,0 +1,73 @@
using Ardalis.GuardClauses;
using PracticeCalendar.Domain.Common;
using PracticeCalendar.Domain.Common.Interfaces;
using PracticeCalendar.Domain.Events;
using PracticeCalendar.Domain.Exceptions;
namespace PracticeCalendar.Domain.Entities.PracticeEvent
{
/// <summary>
/// Practice event aggregate
/// </summary>
public class PracticeEvent : EntityBase, IAggregateRoot
{
public PracticeEvent(string title, string description, DateTime startTime, DateTime endTime)
{
Guard.Against.NullOrEmpty(title, nameof(title));
Guard.Against.NullOrEmpty(description, nameof(description));
this.Title = title;
this.Description = description;
this.StartTime = startTime;
this.EndTime = endTime;
}
public string Title { get; private set; } = string.Empty;
public string Description { get; private set; } = string.Empty;
public IList<Attendee> Attendees { get; private set; } = new List<Attendee>();
public DateTime StartTime { get; private set; }
public DateTime EndTime { get; private set; }
public void AddAttendee(Attendee attendee)
{
Guard.Against.Null(attendee, nameof(attendee));
attendee.AssignToEvent(this.Id);
Attendees.Add(attendee);
var attendeeAddedEvent = new AttendeeAddedEvent(this, attendee);
base.RegisterDomainEvent(attendeeAddedEvent);
}
public void UpdateTitleAndDescription(string title, string description)
{
this.Title = title;
this.Description = description;
var titleDescUpdatedEvent = new EventUpdateTitleAndDescriptionEvent(this);
base.RegisterDomainEvent(titleDescUpdatedEvent);
}
public void AttendeeAcceptEvent(int attendeeId)
{
var attendee = this.Attendees.FirstOrDefault(x => x.Id == attendeeId);
if (attendee == null)
throw new InvalidAttendeeException(attendeeId);
attendee.SetIsAttending(true);
var attendeeAcceptedEvent = new AttendeeAcceptedEvent(this, attendee);
base.RegisterDomainEvent(attendeeAcceptedEvent);
}
public void AttendeeDeclineEvent(int attendeeId)
{
var attendee = this.Attendees.FirstOrDefault(x => x.Id == attendeeId);
if (attendee == null)
throw new InvalidAttendeeException(attendeeId);
attendee.SetIsAttending(false);
var attendeeDeclinedEvent = new AttendeeDeclinedEvent(this, attendee);
base.RegisterDomainEvent(attendeeDeclinedEvent);
}
}
}
@@ -0,0 +1,13 @@
using Ardalis.Specification;
namespace PracticeCalendar.Domain.Entities.PracticeEvent.Specifications
{
public class PracticeEventByIdWithAttendeesSpecification : Specification<PracticeEvent>
{
public PracticeEventByIdWithAttendeesSpecification(int eventId)
{
Query.Where(x => x.Id == eventId)
.Include(x => x.Attendees);
}
}
}
@@ -0,0 +1,13 @@
using Ardalis.Specification;
namespace PracticeCalendar.Domain.Entities.PracticeEvent.Specifications
{
public class PracticeEventsWithAttendeesSpecification : Specification<PracticeEvent>
{
public PracticeEventsWithAttendeesSpecification()
{
Query.AsNoTracking()
.Include(x => x.Attendees);
}
}
}