hn-dotnet/Domain/Votable.cs
2020-12-15 13:58:27 +01:00

43 lines
833 B
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace HN.Domain
{
/// <summary>
/// Représente une entité sur laquelle on peut voter.
/// </summary>
public abstract class Votable
{
private List<Vote> _votes;
public IReadOnlyList<Vote> Votes => _votes;
protected Votable()
{
_votes = new List<Vote>();
}
public void UpvoteBy(Guid userId)
{
UpsertUserVote(userId, VoteType.Up);
}
public void DownvoteBy(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);
}
}
}