using System; using System.Collections.Generic; using System.Linq; using Iesi.Collections.Generic; using Portoa.Persistence; namespace VideoGameQuotes.Api { public class Quote : Entity { private readonly Iesi.Collections.Generic.ISet votes = new HashedSet(); private readonly Iesi.Collections.Generic.ISet flags = new HashedSet(); public Quote() { Created = DateTime.UtcNow; } 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 Votes { get { return votes; } } public virtual IEnumerable Flags { get { return flags; } } public virtual void VoteFor(User user, int value) { var currentVote = votes.SingleOrDefault(vote => vote.Voter == user); if (currentVote != null) { votes.Remove(currentVote); currentVote.Value = value; currentVote.Modified = DateTime.UtcNow; votes.Add(currentVote); } else { votes.Add(new Vote { Value = value, Quote = this, Voter = user }); } } } public static class QuoteExtensions { public static int GetTotalPoints(this Quote quote) { return quote.Votes.Sum(vote => (int)vote); } } }