hn-dotnet/Domain/Votable.cs
Julien Leicher 3cd5133f66 add-aspnet-identity (#26)
add exception filter when user not connected
default to needing authentication and apply anonymous to some actions
add user in get requests
add user relation in link, comment and vote
signup and in are ok now!
2020-12-11 17:59:35 +01:00

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);
}
}
}