reverted back to up/downvotes, configured log4net a little more
This commit is contained in:
parent
4f0dd9a6e8
commit
7e042d3b93
7
Src/VideoGameQuotes.Api/CannotVoteTwiceException.cs
Normal file
7
Src/VideoGameQuotes.Api/CannotVoteTwiceException.cs
Normal file
@ -0,0 +1,7 @@
|
||||
using System;
|
||||
|
||||
namespace VideoGameQuotes.Api {
|
||||
public class CannotVoteTwiceException : Exception {
|
||||
public CannotVoteTwiceException(string message = null, Exception innerException = null) : base(message, innerException) { }
|
||||
}
|
||||
}
|
@ -23,22 +23,24 @@ namespace VideoGameQuotes.Api {
|
||||
public virtual IEnumerable<Vote> Votes { get { return votes; } }
|
||||
public virtual IEnumerable<QuoteFlag> Flags { get { return flags; } }
|
||||
|
||||
public virtual void VoteFor(User user, int value) {
|
||||
public virtual void VoteFor(User user, VoteDirection direction) {
|
||||
var currentVote = votes.SingleOrDefault(vote => vote.Voter == user);
|
||||
if (currentVote != null) {
|
||||
if (currentVote.Direction == direction) {
|
||||
throw new CannotVoteTwiceException();
|
||||
}
|
||||
|
||||
votes.Remove(currentVote);
|
||||
currentVote.Value = value;
|
||||
currentVote.Modified = DateTime.UtcNow;
|
||||
currentVote.Direction = direction;
|
||||
currentVote.Created = DateTime.UtcNow;
|
||||
votes.Add(currentVote);
|
||||
} else {
|
||||
votes.Add(new Vote { Value = value, Quote = this, Voter = user });
|
||||
votes.Add(new Vote { Direction = direction, Quote = this, Voter = user });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class QuoteExtensions {
|
||||
public static int GetTotalPoints(this Quote quote) {
|
||||
return quote.Votes.Sum(vote => (int)vote);
|
||||
}
|
||||
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); } }
|
||||
}
|
||||
}
|
||||
|
@ -64,6 +64,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="CannotVoteTwiceException.cs" />
|
||||
<Compile Include="Game.cs" />
|
||||
<Compile Include="ICurrentUserProvider.cs" />
|
||||
<Compile Include="Persistence\IUserRepository.cs" />
|
||||
@ -78,6 +79,7 @@
|
||||
<Compile Include="User.cs" />
|
||||
<Compile Include="UserGroup.cs" />
|
||||
<Compile Include="Vote.cs" />
|
||||
<Compile Include="VoteDirection.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Mappings\User.hbm.xml" />
|
||||
|
@ -3,18 +3,13 @@ using Portoa.Persistence;
|
||||
|
||||
namespace VideoGameQuotes.Api {
|
||||
public class Vote : Entity<Vote, int> {
|
||||
public Vote() {
|
||||
Created = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
public virtual User Voter { get; set; }
|
||||
public virtual Quote Quote { get; set; }
|
||||
public virtual DateTime Created { get; set; }
|
||||
public virtual DateTime? Modified { get; set; }
|
||||
public virtual int Value { get; set; }
|
||||
public virtual VoteDirection Direction { get; set; }
|
||||
|
||||
public static explicit operator int(Vote vote) {
|
||||
return vote.Value;
|
||||
return vote.Direction == VoteDirection.Up ? 1 : -1;
|
||||
}
|
||||
}
|
||||
}
|
6
Src/VideoGameQuotes.Api/VoteDirection.cs
Normal file
6
Src/VideoGameQuotes.Api/VoteDirection.cs
Normal file
@ -0,0 +1,6 @@
|
||||
namespace VideoGameQuotes.Api {
|
||||
public enum VoteDirection {
|
||||
Up = 1,
|
||||
Down = 0
|
||||
}
|
||||
}
|
@ -13,7 +13,8 @@ namespace VideoGameQuotes.Web {
|
||||
Container
|
||||
.AddNewExtension<ConfigureLog4Net>()
|
||||
.Configure<ILog4NetConfigurator>()
|
||||
.SetName("VideoGameQuotes.Web");
|
||||
.SetName("VideoGameQuotes.Web")
|
||||
.UseXml();
|
||||
|
||||
Container
|
||||
.AddNewExtension<LogAllMethodCalls>()
|
||||
|
@ -3,16 +3,34 @@
|
||||
namespace VideoGameQuotes.Api.Tests {
|
||||
[TestFixture]
|
||||
public class QuoteTests {
|
||||
|
||||
[Test]
|
||||
public void Should_remove_old_vote_if_voting_for_same_quote() {
|
||||
[ExpectedException(typeof(CannotVoteTwiceException))]
|
||||
public void Should_not_allow_voting_in_the_same_direction_twice() {
|
||||
var quote = new Quote();
|
||||
var user = new User();
|
||||
|
||||
quote.VoteFor(user, 5);
|
||||
Assert.That(quote.GetTotalPoints(), Is.EqualTo(5));
|
||||
|
||||
quote.VoteFor(user, 7);
|
||||
Assert.That(quote.GetTotalPoints(), Is.EqualTo(7));
|
||||
quote.VoteFor(user, VoteDirection.Up);
|
||||
quote.VoteFor(user, VoteDirection.Up);
|
||||
}
|
||||
|
||||
[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));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user