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