43 lines
829 B
C#
43 lines
829 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 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);
|
|
}
|
|
}
|
|
} |