2011-02-09 00:05:32 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2011-02-14 07:39:45 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2011-02-09 00:05:32 +00:00
|
|
|
|
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-12 23:53:43 +00:00
|
|
|
|
private readonly Iesi.Collections.Generic.ISet<Category> categories = new HashedSet<Category>();
|
2011-02-09 00:05:32 +00:00
|
|
|
|
|
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-12 23:53:43 +00:00
|
|
|
|
public virtual IEnumerable<Category> Categories { get { return categories; } }
|
2011-02-09 00:05:32 +00:00
|
|
|
|
|
2011-02-12 23:53:43 +00:00
|
|
|
|
#region adding and removing stuff
|
|
|
|
|
public virtual Quote AddCategory(Category category) {
|
|
|
|
|
categories.Add(category);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void ClearCategories() {
|
|
|
|
|
categories.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual Quote AddFlag(QuoteFlag flag) {
|
|
|
|
|
flags.Add(flag);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void ClearFlags() {
|
|
|
|
|
flags.Clear();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public virtual Quote VoteFor(User user, VoteDirection direction) {
|
2011-02-09 00:05:32 +00:00
|
|
|
|
var currentVote = votes.SingleOrDefault(vote => vote.Voter == user);
|
|
|
|
|
if (currentVote != null) {
|
2011-02-10 20:34:55 +00:00
|
|
|
|
if (currentVote.Direction == direction) {
|
|
|
|
|
throw new CannotVoteTwiceException();
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-09 00:05:32 +00:00
|
|
|
|
votes.Remove(currentVote);
|
2011-02-10 20:34:55 +00:00
|
|
|
|
currentVote.Direction = direction;
|
|
|
|
|
currentVote.Created = DateTime.UtcNow;
|
2011-02-09 00:05:32 +00:00
|
|
|
|
votes.Add(currentVote);
|
|
|
|
|
} else {
|
2011-02-10 20:34:55 +00:00
|
|
|
|
votes.Add(new Vote { Direction = direction, Quote = this, Voter = user });
|
2011-02-09 00:05:32 +00:00
|
|
|
|
}
|
2011-02-12 23:53:43 +00:00
|
|
|
|
|
|
|
|
|
return this;
|
2011-02-09 00:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-10 20:34:55 +00:00
|
|
|
|
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); } }
|
2011-02-09 00:05:32 +00:00
|
|
|
|
}
|
2011-02-12 23:53:43 +00:00
|
|
|
|
|
|
|
|
|
public static class QuoteExtensions {
|
2011-02-14 07:39:45 +00:00
|
|
|
|
private static readonly Regex urlFriendlyRegex = new Regex(@"[^A-Za-z0-9_-]");
|
|
|
|
|
|
|
|
|
|
public static string GetUrlFriendlyText(this Quote quote) {
|
|
|
|
|
var text = urlFriendlyRegex.Replace(quote.Text.Replace(" ", "_").Replace("\n", ""), "");
|
|
|
|
|
return text.Substring(0, Math.Min(text.Length, 50));
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-12 23:53:43 +00:00
|
|
|
|
public static string GetHumanReadableTimeSinceCreated(this Quote quote) {
|
|
|
|
|
var timespan = DateTime.UtcNow.Subtract(quote.Created);
|
|
|
|
|
if (timespan.TotalDays >= 365) {
|
|
|
|
|
var years = (int)Math.Round(timespan.TotalDays / 365);
|
|
|
|
|
return years == 1 ? "1 year ago" : years + " years ago";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (timespan.TotalDays >= 50) {
|
|
|
|
|
var months = (int)Math.Round(timespan.TotalDays / 30);
|
|
|
|
|
return months == 1 ? "1 month ago" : months + " months ago";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (timespan.TotalDays >= 10) {
|
|
|
|
|
var weeks = (int)Math.Round(timespan.TotalDays / 7);
|
|
|
|
|
return weeks == 1 ? "1 week ago" : weeks + " weeks ago";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (timespan.TotalDays >= 1) {
|
|
|
|
|
return (int)timespan.TotalDays == 1 ? "1 day ago" : (int)timespan.TotalDays + " days ago";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (timespan.TotalMinutes >= 90) {
|
|
|
|
|
return (int)timespan.TotalHours == 1 ? "1 hour ago" : (int)timespan.TotalHours + " hours ago";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (timespan.TotalSeconds >= 90) {
|
|
|
|
|
return (int)timespan.TotalMinutes == 1 ? "1 minute ago" : (int)timespan.TotalMinutes + " minutes ago";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (int)timespan.TotalSeconds == 1 ? "1 second ago" : (int)timespan.TotalSeconds + " seconds ago";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-09 00:05:32 +00:00
|
|
|
|
}
|