From 215d3440b3f94cf5e9d9d9528bef0d733c7788e0 Mon Sep 17 00:00:00 2001 From: tmont Date: Sat, 12 Feb 2011 23:53:43 +0000 Subject: [PATCH] submit page, added categories, initial quotes/categories --- Src/VideoGameQuotes.Api/Game.cs | 28 +++- .../{System.cs => GamingSystem.cs} | 4 +- .../Mappings/Category.hbm.xml | 13 ++ Src/VideoGameQuotes.Api/Mappings/Game.hbm.xml | 13 +- .../{System.hbm.xml => GamingSystem.hbm.xml} | 4 +- .../Mappings/Quote.hbm.xml | 9 +- Src/VideoGameQuotes.Api/Publisher.cs | 2 + Src/VideoGameQuotes.Api/Quote.cs | 61 ++++++- Src/VideoGameQuotes.Api/Region.cs | 14 +- .../VideoGameQuotes.Api.csproj | 9 +- .../Controllers/QuoteController.cs | 132 +++++++++++++++ Src/VideoGameQuotes.Web/Global.asax.cs | 7 + .../Models/QuoteSubmitModel.cs | 95 +++++++++++ .../Security/IsValidUserAttribute.cs | 31 ++++ .../Services/QuoteService.cs | 75 +++++++++ .../VideoGameQuotes.Web.csproj | 9 + .../Views/Quote/Quote.aspx | 24 +++ .../Views/Quote/Submit.aspx | 155 ++++++++++++++++++ Src/VideoGameQuotes.Web/media/css/global.css | 29 +++- .../NHibernate/SchemaExporter.cs | 132 ++++++++++++--- .../NHibernate/schema.sql | 108 +++++++++--- 21 files changed, 876 insertions(+), 78 deletions(-) rename Src/VideoGameQuotes.Api/{System.cs => GamingSystem.cs} (75%) create mode 100644 Src/VideoGameQuotes.Api/Mappings/Category.hbm.xml rename Src/VideoGameQuotes.Api/Mappings/{System.hbm.xml => GamingSystem.hbm.xml} (86%) create mode 100644 Src/VideoGameQuotes.Web/Controllers/QuoteController.cs create mode 100644 Src/VideoGameQuotes.Web/Models/QuoteSubmitModel.cs create mode 100644 Src/VideoGameQuotes.Web/Security/IsValidUserAttribute.cs create mode 100644 Src/VideoGameQuotes.Web/Services/QuoteService.cs create mode 100644 Src/VideoGameQuotes.Web/Views/Quote/Quote.aspx create mode 100644 Src/VideoGameQuotes.Web/Views/Quote/Submit.aspx diff --git a/Src/VideoGameQuotes.Api/Game.cs b/Src/VideoGameQuotes.Api/Game.cs index c1b84a8..e1471cd 100644 --- a/Src/VideoGameQuotes.Api/Game.cs +++ b/Src/VideoGameQuotes.Api/Game.cs @@ -1,30 +1,46 @@ using System; using System.Collections.Generic; using Iesi.Collections.Generic; +using JetBrains.Annotations; using Portoa.Persistence; namespace VideoGameQuotes.Api { public class Game : Entity { - private readonly Iesi.Collections.Generic.ISet systems = new HashedSet(); + private readonly Iesi.Collections.Generic.ISet systems = new HashedSet(); + private readonly Iesi.Collections.Generic.ISet publishers = new HashedSet(); public Game() { Created = DateTime.UtcNow; } + public virtual string Website { get; set; } public virtual User Creator { get; set; } public virtual DateTime Created { get; set; } - public virtual IEnumerable Systems { get { return systems; } } + public virtual IEnumerable Systems { get { return systems; } } public virtual string Name { get; set; } - public virtual Publisher Publisher { get; set; } + public virtual IEnumerable Publishers { get { return publishers; } } + [CanBeNull] public virtual byte[] Screenshot { get; set; } public virtual Region Region { get; set; } - public virtual void AddSystem(System system) { + #region adding and removing stuff + public virtual Game AddSystem(GamingSystem system) { systems.Add(system); + return this; } - public virtual void RemoveSystem(System system) { - systems.Remove(system); + public virtual void ClearSystems() { + systems.Clear(); } + + public virtual Game AddPublisher(Publisher publisher) { + publishers.Add(publisher); + return this; + } + + public virtual void ClearPublishers() { + publishers.Clear(); + } + #endregion } } \ No newline at end of file diff --git a/Src/VideoGameQuotes.Api/System.cs b/Src/VideoGameQuotes.Api/GamingSystem.cs similarity index 75% rename from Src/VideoGameQuotes.Api/System.cs rename to Src/VideoGameQuotes.Api/GamingSystem.cs index 9539304..c2127de 100644 --- a/Src/VideoGameQuotes.Api/System.cs +++ b/Src/VideoGameQuotes.Api/GamingSystem.cs @@ -2,8 +2,8 @@ using Portoa.Persistence; namespace VideoGameQuotes.Api { - public class System : Entity { - public System() { + public class GamingSystem : Entity { + public GamingSystem() { Created = DateTime.UtcNow; } diff --git a/Src/VideoGameQuotes.Api/Mappings/Category.hbm.xml b/Src/VideoGameQuotes.Api/Mappings/Category.hbm.xml new file mode 100644 index 0000000..b4190bf --- /dev/null +++ b/Src/VideoGameQuotes.Api/Mappings/Category.hbm.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/Src/VideoGameQuotes.Api/Mappings/Game.hbm.xml b/Src/VideoGameQuotes.Api/Mappings/Game.hbm.xml index 65d52c3..7aa98ac 100644 --- a/Src/VideoGameQuotes.Api/Mappings/Game.hbm.xml +++ b/Src/VideoGameQuotes.Api/Mappings/Game.hbm.xml @@ -1,23 +1,26 @@  - - + + - - + + + + + + - \ No newline at end of file diff --git a/Src/VideoGameQuotes.Api/Mappings/System.hbm.xml b/Src/VideoGameQuotes.Api/Mappings/GamingSystem.hbm.xml similarity index 86% rename from Src/VideoGameQuotes.Api/Mappings/System.hbm.xml rename to Src/VideoGameQuotes.Api/Mappings/GamingSystem.hbm.xml index 3b270ce..68b6388 100644 --- a/Src/VideoGameQuotes.Api/Mappings/System.hbm.xml +++ b/Src/VideoGameQuotes.Api/Mappings/GamingSystem.hbm.xml @@ -1,12 +1,12 @@  - + - + diff --git a/Src/VideoGameQuotes.Api/Mappings/Quote.hbm.xml b/Src/VideoGameQuotes.Api/Mappings/Quote.hbm.xml index bceed6c..4e8f8f8 100644 --- a/Src/VideoGameQuotes.Api/Mappings/Quote.hbm.xml +++ b/Src/VideoGameQuotes.Api/Mappings/Quote.hbm.xml @@ -13,14 +13,19 @@ + + + + + - + - + diff --git a/Src/VideoGameQuotes.Api/Publisher.cs b/Src/VideoGameQuotes.Api/Publisher.cs index 3c7b4e8..4331123 100644 --- a/Src/VideoGameQuotes.Api/Publisher.cs +++ b/Src/VideoGameQuotes.Api/Publisher.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel.DataAnnotations; using Portoa.Persistence; namespace VideoGameQuotes.Api { @@ -7,6 +8,7 @@ namespace VideoGameQuotes.Api { Created = DateTime.UtcNow; } + [Required] public virtual string Name { get; set; } public virtual string Website { get; set; } public virtual DateTime Created { get; set; } diff --git a/Src/VideoGameQuotes.Api/Quote.cs b/Src/VideoGameQuotes.Api/Quote.cs index a19576f..18ed095 100644 --- a/Src/VideoGameQuotes.Api/Quote.cs +++ b/Src/VideoGameQuotes.Api/Quote.cs @@ -8,6 +8,7 @@ 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(); + private readonly Iesi.Collections.Generic.ISet categories = new HashedSet(); public Quote() { Created = DateTime.UtcNow; @@ -22,8 +23,29 @@ namespace VideoGameQuotes.Api { public virtual IEnumerable Votes { get { return votes; } } public virtual IEnumerable Flags { get { return flags; } } + public virtual IEnumerable Categories { get { return categories; } } - public virtual void VoteFor(User user, VoteDirection direction) { + #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) { var currentVote = votes.SingleOrDefault(vote => vote.Voter == user); if (currentVote != null) { if (currentVote.Direction == direction) { @@ -37,10 +59,47 @@ namespace VideoGameQuotes.Api { } else { votes.Add(new Vote { Direction = direction, Quote = this, Voter = user }); } + + return this; } 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); } } } + + public static class QuoteExtensions { + 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"; + } + } + } diff --git a/Src/VideoGameQuotes.Api/Region.cs b/Src/VideoGameQuotes.Api/Region.cs index b57f5fc..a4657b6 100644 --- a/Src/VideoGameQuotes.Api/Region.cs +++ b/Src/VideoGameQuotes.Api/Region.cs @@ -1,12 +1,18 @@ -namespace VideoGameQuotes.Api { +using System; + +namespace VideoGameQuotes.Api { /// /// The region for which a game is associated /// + [Flags] public enum Region { Unknown = 0, - UnitedStates = 1, + NorthAmerica = 1, Japan = 2, - Europe = 3, - Other = 4 + Europe = 4, + PAL = 8, + Australia = 16, + + All = NorthAmerica | Japan | Europe | PAL | Australia } } \ No newline at end of file diff --git a/Src/VideoGameQuotes.Api/VideoGameQuotes.Api.csproj b/Src/VideoGameQuotes.Api/VideoGameQuotes.Api.csproj index dec9e22..52a8676 100644 --- a/Src/VideoGameQuotes.Api/VideoGameQuotes.Api.csproj +++ b/Src/VideoGameQuotes.Api/VideoGameQuotes.Api.csproj @@ -56,6 +56,7 @@ ..\..\Lib\Portoa.NHibernate.dll + @@ -65,6 +66,7 @@ + @@ -75,7 +77,7 @@ - + @@ -84,7 +86,7 @@ - + @@ -94,6 +96,9 @@ + + +