changed up/downvotes to points, added initial system data

This commit is contained in:
tmont 2011-02-09 19:28:33 +00:00
parent e88aa8fb1c
commit 6302f9e995
8 changed files with 69 additions and 55 deletions

View File

@ -1,7 +0,0 @@
using System;
namespace VideoGameQuotes.Api {
public class CannotVoteTwiceException : Exception {
public CannotVoteTwiceException(string message = null, Exception innerException = null) : base(message, innerException) { }
}
}

View File

@ -7,7 +7,7 @@
</id> </id>
<property name="Created" column="created" not-null="true" /> <property name="Created" column="created" not-null="true" />
<property name="Direction" column="direction" not-null="true" type="int" length="1" /> <property name="Value" column="point_value" not-null="true" />
<many-to-one name="Voter" column="voter_id" not-null="true" foreign-key="fk_vote_user"/> <many-to-one name="Voter" column="voter_id" not-null="true" foreign-key="fk_vote_user"/>
<many-to-one name="Quote" column="quote_id" not-null="true" foreign-key="fk_vote_quote" /> <many-to-one name="Quote" column="quote_id" not-null="true" foreign-key="fk_vote_quote" />

View File

@ -23,24 +23,22 @@ namespace VideoGameQuotes.Api {
public virtual IEnumerable<Vote> Votes { get { return votes; } } public virtual IEnumerable<Vote> Votes { get { return votes; } }
public virtual IEnumerable<QuoteFlag> Flags { get { return flags; } } public virtual IEnumerable<QuoteFlag> Flags { get { return flags; } }
public virtual void VoteFor(User user, VoteDirection direction) { public virtual void VoteFor(User user, int value) {
var currentVote = votes.SingleOrDefault(vote => vote.Voter == user); var currentVote = votes.SingleOrDefault(vote => vote.Voter == user);
if (currentVote != null) { if (currentVote != null) {
if (currentVote.Direction == direction) {
throw new CannotVoteTwiceException();
}
votes.Remove(currentVote); votes.Remove(currentVote);
currentVote.Direction = direction; currentVote.Value = value;
currentVote.Created = DateTime.UtcNow; currentVote.Modified = DateTime.UtcNow;
votes.Add(currentVote); votes.Add(currentVote);
} else { } else {
votes.Add(new Vote { Direction = direction, Quote = this, Voter = user }); votes.Add(new Vote { Value = value, Quote = this, Voter = user });
}
} }
} }
public virtual int UpVotes { get { return Votes.Count(vote => vote.Direction == VoteDirection.Up); } } public static class QuoteExtensions {
public virtual int DownVotes { get { return Votes.Count(vote => vote.Direction == VoteDirection.Down); } } public static int GetTotalPoints(this Quote quote) {
public virtual int NetVotes { get { return Votes.Sum(vote => (int)vote); } } return quote.Votes.Sum(vote => (int)vote);
}
} }
} }

View File

@ -64,7 +64,6 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="CannotVoteTwiceException.cs" />
<Compile Include="Game.cs" /> <Compile Include="Game.cs" />
<Compile Include="QuoteFlag.cs" /> <Compile Include="QuoteFlag.cs" />
<Compile Include="Publisher.cs" /> <Compile Include="Publisher.cs" />
@ -76,7 +75,6 @@
<Compile Include="User.cs" /> <Compile Include="User.cs" />
<Compile Include="UserGroup.cs" /> <Compile Include="UserGroup.cs" />
<Compile Include="Vote.cs" /> <Compile Include="Vote.cs" />
<Compile Include="VoteDirection.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Mappings\User.hbm.xml" /> <EmbeddedResource Include="Mappings\User.hbm.xml" />

View File

@ -3,13 +3,18 @@ using Portoa.Persistence;
namespace VideoGameQuotes.Api { namespace VideoGameQuotes.Api {
public class Vote : Entity<Vote, int> { public class Vote : Entity<Vote, int> {
public Vote() {
Created = DateTime.UtcNow;
}
public virtual User Voter { get; set; } public virtual User Voter { get; set; }
public virtual Quote Quote { get; set; } public virtual Quote Quote { get; set; }
public virtual DateTime Created { get; set; } public virtual DateTime Created { get; set; }
public virtual VoteDirection Direction { get; set; } public virtual DateTime? Modified { get; set; }
public virtual int Value { get; set; }
public static explicit operator int(Vote vote) { public static explicit operator int(Vote vote) {
return vote.Direction == VoteDirection.Up ? 1 : -1; return vote.Value;
} }
} }
} }

View File

@ -1,6 +0,0 @@
namespace VideoGameQuotes.Api {
public enum VoteDirection {
Up = 1,
Down = 0
}
}

View File

@ -1,4 +1,5 @@
using NHibernate.Cfg; using System;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl; using NHibernate.Tool.hbm2ddl;
using NUnit.Framework; using NUnit.Framework;
using Portoa.NHibernate; using Portoa.NHibernate;
@ -20,7 +21,7 @@ namespace VideoGameQuotes.Api.Tests.NHibernate {
} }
[Test] [Test]
public void Create_admin() { public void Create_initial_data() {
var config = GetConfig(); var config = GetConfig();
var admin = new User { var admin = new User {
@ -35,6 +36,49 @@ namespace VideoGameQuotes.Api.Tests.NHibernate {
new NHibernateRepository<User>(session).Save(admin); new NHibernateRepository<User>(session).Save(admin);
tx.Commit(); tx.Commit();
} }
var nes = new System { Abbreviation = "NES", Name = "Nintendo Entertainment System", ReleaseDate = new DateTime(1985, 10, 18) };
var snes = new System { Abbreviation = "SNES", Name = "Super Nintendo Entertainment System", ReleaseDate = new DateTime(1991, 8, 23) };
var genny = new System { Abbreviation = "Genesis", Name = "Sega Genesis", ReleaseDate = new DateTime(1989, 10, 14) };
var ps = new System { Abbreviation = "PS", Name = "PlayStation", ReleaseDate = new DateTime(1995, 9, 9) };
var ps2 = new System { Abbreviation = "PS2", Name = "PlayStation 2", ReleaseDate = new DateTime(2000, 3, 4) };
var xbox = new System { Abbreviation = "Xbox", Name = "Xbox", ReleaseDate = new DateTime(2002, 2, 22) };
var n64 = new System { Abbreviation = "N64", Name = "Nintendo 64", ReleaseDate = new DateTime(1996, 9, 29) };
var gcn = new System { Abbreviation = "GCN", Name = "Nintendo GameCube", ReleaseDate = new DateTime(2001, 11, 18) };
var dc = new System { Abbreviation = "DC", Name = "Dreamcast", ReleaseDate = new DateTime(1999, 9, 9) };
var wii = new System { Abbreviation = "Wii", Name = "Wii", ReleaseDate = new DateTime(2006, 11, 19) };
var xbox360 = new System { Abbreviation = "360", Name = "Xbox 360", ReleaseDate = new DateTime(2005, 11, 16) };
var ps3 = new System { Abbreviation = "PS3", Name = "PlayStation 3", ReleaseDate = new DateTime(2006, 11, 17) };
var pc = new System { Abbreviation = "PC", Name = "Personal Computer", ReleaseDate = new DateTime(1980, 1, 1) };
var gb = new System { Abbreviation = "GB", Name = "Game Boy", ReleaseDate = new DateTime(1989, 7, 31) };
var gbc = new System { Abbreviation = "GBC", Name = "Game Boy Color", ReleaseDate = new DateTime(1998, 11, 18) };
var gba = new System { Abbreviation = "GBA", Name = "Game Boy Advance", ReleaseDate = new DateTime(2001, 6, 11) };
var nds = new System { Abbreviation = "NES", Name = "Nintendo Entertainment System", ReleaseDate = new DateTime(1985, 10, 18) };
var psp = new System { Abbreviation = "PSP", Name = "PlayStation Portable", ReleaseDate = new DateTime(2005, 3, 24) };
using (var tx = session.BeginTransaction()) {
var repo = new NHibernateRepository<System>(session);
repo.Save(nes);
repo.Save(snes);
repo.Save(genny);
repo.Save(ps);
repo.Save(ps2);
repo.Save(xbox);
repo.Save(n64);
repo.Save(gcn);
repo.Save(dc);
repo.Save(wii);
repo.Save(xbox360);
repo.Save(ps3);
repo.Save(pc);
repo.Save(gb);
repo.Save(gbc);
repo.Save(gba);
repo.Save(nds);
repo.Save(psp);
tx.Commit();
}
} }
} }

View File

@ -3,34 +3,16 @@
namespace VideoGameQuotes.Api.Tests { namespace VideoGameQuotes.Api.Tests {
[TestFixture] [TestFixture]
public class QuoteTests { public class QuoteTests {
[Test] [Test]
[ExpectedException(typeof(CannotVoteTwiceException))] public void Should_remove_old_vote_if_voting_for_same_quote() {
public void Should_not_allow_voting_in_the_same_direction_twice() {
var quote = new Quote(); var quote = new Quote();
var user = new User(); var user = new User();
quote.VoteFor(user, VoteDirection.Up); quote.VoteFor(user, 5);
quote.VoteFor(user, VoteDirection.Up); Assert.That(quote.GetTotalPoints(), Is.EqualTo(5));
}
[Test]
public void Should_remove_old_vote_if_voting_in_opposite_direction() {
var quote = new Quote();
var user = new User();
quote.VoteFor(user, VoteDirection.Up);
Assert.That(quote.UpVotes, Is.EqualTo(1));
Assert.That(quote.DownVotes, Is.EqualTo(0));
Assert.That(quote.NetVotes, Is.EqualTo(1));
quote.VoteFor(user, VoteDirection.Down);
Assert.That(quote.UpVotes, Is.EqualTo(0));
Assert.That(quote.DownVotes, Is.EqualTo(1));
Assert.That(quote.NetVotes, Is.EqualTo(-1));
}
quote.VoteFor(user, 7);
Assert.That(quote.GetTotalPoints(), Is.EqualTo(7));
}
} }
} }