42 lines
1.5 KiB
C#
42 lines
1.5 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using Iesi.Collections.Generic;
|
|||
|
using Portoa.Persistence;
|
|||
|
|
|||
|
namespace VideoGameQuotes.Api {
|
|||
|
public class Quote : Entity<Quote, int> {
|
|||
|
private readonly Iesi.Collections.Generic.ISet<Vote> votes = new HashedSet<Vote>();
|
|||
|
private readonly Iesi.Collections.Generic.ISet<QuoteFlag> flags = new HashedSet<QuoteFlag>();
|
|||
|
|
|||
|
public virtual User Creator { get; set; }
|
|||
|
public virtual DateTime Created { get; set; }
|
|||
|
public virtual DateTime? Modified { get; set; }
|
|||
|
|
|||
|
public virtual string Text { get; set; }
|
|||
|
public virtual Game Game { get; set; }
|
|||
|
|
|||
|
public virtual IEnumerable<Vote> Votes { get { return votes; } }
|
|||
|
public virtual IEnumerable<QuoteFlag> Flags { get { return flags; } }
|
|||
|
|
|||
|
public virtual void VoteFor(User user, VoteDirection direction) {
|
|||
|
var currentVote = votes.SingleOrDefault(vote => vote.Voter == user);
|
|||
|
if (currentVote != null) {
|
|||
|
if (currentVote.Direction == direction) {
|
|||
|
throw new CannotVoteTwiceException();
|
|||
|
}
|
|||
|
|
|||
|
votes.Remove(currentVote);
|
|||
|
currentVote.Direction = direction;
|
|||
|
votes.Add(currentVote);
|
|||
|
} else {
|
|||
|
votes.Add(new Vote { Direction = direction, Quote = this, Voter = user });
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public virtual int UpVotes { get { return Votes.Count(vote => vote.Direction == VoteDirection.Up); } }
|
|||
|
public virtual int DownVotes { get { return Votes.Count(vote => vote.Direction == VoteDirection.Down); } }
|
|||
|
public virtual int NetVotes { get { return Votes.Sum(vote => (int)vote); } }
|
|||
|
}
|
|||
|
}
|