Skip to content

Instantly share code, notes, and snippets.

@ahmetkucukoglu
Created December 17, 2024 12:12
Show Gist options
  • Save ahmetkucukoglu/27d3faf45d403a0b7888967aa62ade11 to your computer and use it in GitHub Desktop.
Save ahmetkucukoglu/27d3faf45d403a0b7888967aa62ade11 to your computer and use it in GitHub Desktop.
Persistence Ignorance - Voting Domain Model
using PersistenceIgnorance.Domain.Voting.Events;
namespace PersistenceIgnorance.Domain.Voting;
public class Voting(List<Voter> voters)
{
private readonly List<Voter> _voters = voters;
private readonly List<IEvent> _events = [];
private bool IsStarted { get; set; }
private bool IsClosed { get; set; }
public Voting(List<Voter> voters, bool isStarted, bool isClosed) : this(voters)
{
_voters = voters;
IsStarted = isStarted;
IsClosed = isClosed;
}
public void AddVoter(int identityNumber)
{
if (IsStarted)
throw new InvalidOperationException("The voting has started.");
if (IsClosed)
throw new InvalidOperationException("The voting has closed.");
if (_voters.Any(v => v.IdentityNumber == identityNumber))
throw new InvalidOperationException("The voter has already exists.");
_voters.Add(new Voter(identityNumber));
_events.Add(new VoterAdded(identityNumber));
}
public void RemoveVoter(int identityNumber)
{
if (IsStarted)
throw new InvalidOperationException("The voting has started.");
if (IsClosed)
throw new InvalidOperationException("The voting has closed.");
var voter = _voters.FirstOrDefault(v => v.IdentityNumber == identityNumber);
if (voter == null)
throw new InvalidOperationException("The voter has not found.");
_voters.Remove(voter);
_events.Add(new VoterRemoved(identityNumber));
}
public void Start()
{
if (IsStarted)
throw new InvalidOperationException("The voting has started.");
if (IsClosed)
throw new InvalidOperationException("The voting has closed.");
_events.Add(new VotingStarted(DateTimeOffset.Now));
}
public void Close()
{
if (IsClosed)
throw new InvalidOperationException("The voting has closed.");
if (!IsStarted)
throw new InvalidOperationException("The voting has not started.");
if (_voters.Any(v => v.Status == VotingStatus.Waiting))
throw new InvalidOperationException("There are those who don't vote.");
_events.Add(new VotingClosed(DateTimeOffset.Now));
}
public void Approve(int identityNumber)
{
if (IsClosed)
throw new InvalidOperationException("The voting has closed.");
if (!IsStarted)
throw new InvalidOperationException("The voting has not started.");
var voter = _voters.FirstOrDefault(v => v.IdentityNumber == identityNumber);
if (voter == null)
throw new InvalidOperationException("The voter has not found.");
if (voter.Status != VotingStatus.Waiting)
throw new InvalidOperationException("The voter has already made his choice.");
voter.ChangeStatus(VotingStatus.Approved);
_events.Add(new VoterApproved(identityNumber, DateTimeOffset.Now));
}
public void Reject(int identityNumber)
{
if (IsClosed)
throw new InvalidOperationException("The voting has closed.");
if (!IsStarted)
throw new InvalidOperationException("The voting has not started.");
var voter = _voters.FirstOrDefault(v => v.IdentityNumber == identityNumber);
if (voter == null)
throw new InvalidOperationException("The voter has not found.");
if (voter.Status != VotingStatus.Waiting)
throw new InvalidOperationException("The voter has already made his choice.");
voter.ChangeStatus(VotingStatus.Rejected);
_events.Add(new VoterRejected(identityNumber, DateTimeOffset.Now));
}
public IReadOnlyCollection<IEvent> GetEvents => _events.AsReadOnly();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment