using System;
using System.Collections.Generic;
using System.Linq;
namespace HN.Domain
{
///
/// Représente une entité sur laquelle on peut voter.
///
public abstract class Votable
{
private List _votes;
public IReadOnlyList Votes => _votes;
protected Votable()
{
_votes = new List();
}
public void Upvote(Guid userId)
{
UpsertUserVote(userId, VoteType.Up);
}
public void Downvote(Guid userId)
{
UpsertUserVote(userId, VoteType.Down);
}
private void UpsertUserVote(Guid userId, VoteType type)
{
var vote = _votes.SingleOrDefault(v => v.CreatedBy == userId);
if (vote == null)
{
_votes.Add(new Vote(userId, type));
return;
}
vote.HasType(type);
}
}
}