2011-02-09 00:05:32 +00:00
|
|
|
|
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>();
|
|
|
|
|
|
2011-02-09 03:41:35 +00:00
|
|
|
|
public Quote() {
|
|
|
|
|
Created = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-09 00:05:32 +00:00
|
|
|
|
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; } }
|
|
|
|
|
|
2011-02-09 19:28:33 +00:00
|
|
|
|
public virtual void VoteFor(User user, int value) {
|
2011-02-09 00:05:32 +00:00
|
|
|
|
var currentVote = votes.SingleOrDefault(vote => vote.Voter == user);
|
|
|
|
|
if (currentVote != null) {
|
|
|
|
|
votes.Remove(currentVote);
|
2011-02-09 19:28:33 +00:00
|
|
|
|
currentVote.Value = value;
|
|
|
|
|
currentVote.Modified = DateTime.UtcNow;
|
2011-02-09 00:05:32 +00:00
|
|
|
|
votes.Add(currentVote);
|
|
|
|
|
} else {
|
2011-02-09 19:28:33 +00:00
|
|
|
|
votes.Add(new Vote { Value = value, Quote = this, Voter = user });
|
2011-02-09 00:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-09 19:28:33 +00:00
|
|
|
|
}
|
2011-02-09 00:05:32 +00:00
|
|
|
|
|
2011-02-09 19:28:33 +00:00
|
|
|
|
public static class QuoteExtensions {
|
|
|
|
|
public static int GetTotalPoints(this Quote quote) {
|
|
|
|
|
return quote.Votes.Sum(vote => (int)vote);
|
|
|
|
|
}
|
2011-02-09 00:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|