From 412d381dde95390719330bbd9db960698921c810 Mon Sep 17 00:00:00 2001 From: Chris McKenzie Date: Wed, 27 Mar 2013 23:13:41 -0700 Subject: [PATCH] Added PurchaseTicketCommand and tests. --- README.md | 2 +- .../PurchaseTicketCommandTests.cs | 77 +++++++++++++++++++ .../ShowPlanner.Commands.Unit.Tests.csproj | 1 + .../ShowPlanner.Commands.csproj | 1 + .../Tickets/PurchaseTicketCommand.cs | 56 ++++++++++++++ ShowPlanner.Data.Models/Customer.cs | 6 +- ShowPlanner.Data.Models/Performance.cs | 4 + ShowPlanner.Data.Models/Performer.cs | 6 +- ShowPlanner.Data.Models/Show.cs | 6 +- .../ShowPlanner.Data.Models.csproj | 1 + ShowPlanner.Data.Models/Stage.cs | 8 +- ShowPlanner.Data.Models/Ticket.cs | 5 +- ShowPlanner.Data.Models/Venue.cs | 5 +- 13 files changed, 168 insertions(+), 10 deletions(-) create mode 100644 ShowPlanner.Commands.Unit.Tests/PurchaseTicketCommandTests.cs create mode 100644 ShowPlanner.Commands/Tickets/PurchaseTicketCommand.cs diff --git a/README.md b/README.md index 610af08..981f7af 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ShowPlanner =========== -A Demo Architecture Project +## A Demo Architecture Project ShowPlanner.Api is the public facing Api (currently not implemented because no site-level contracts have been created). diff --git a/ShowPlanner.Commands.Unit.Tests/PurchaseTicketCommandTests.cs b/ShowPlanner.Commands.Unit.Tests/PurchaseTicketCommandTests.cs new file mode 100644 index 0000000..f10bbbf --- /dev/null +++ b/ShowPlanner.Commands.Unit.Tests/PurchaseTicketCommandTests.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using FizzWare.NBuilder; +using NSubstitute; +using NUnit.Framework; +using ShowPlanner.Commands.Tickets; +using ShowPlanner.Data; +using ShowPlanner.Data.Models; + +namespace ShowPlanner.Commands.Unit.Tests +{ + [TestFixture] + public class PurchaseTicketCommandTests + { + [TestFixtureSetUp] + public void BeforeAnyTestRuns() + { + } + + [SetUp] + public void BeforeEachTestRuns() + { + this.Database = Substitute.For(); + this.Repository = Substitute.For(); + this.Database.NewRepository().Returns(this.Repository); + this.Command = new PurchaseTicketCommand(this.Database); + } + + protected IRepository Repository { get; set; } + + protected PurchaseTicketCommand Command { get; set; } + + protected IDatabase Database { get; set; } + + [Test] + public void Execute() + { + // Arrange + var customer = Builder + .CreateNew() + .Build() + ; + + var ticket = Builder + .CreateNew() + .Do(row=> + { + row.SoldTo = null; + row.Sold = false; + }) + .Build() + ; + + this.Repository.Get(customer.CustomerId.Value).Returns(customer); + this.Repository.Get(ticket.TicketId.Value).Returns(ticket); + + var request = new PurchaseTicketRequest() + { + CustomerId = customer.CustomerId.Value, + TicketId = ticket.TicketId.Value + }; + + // Act + var response = this.Command.Execute(request); + + // Assert + Assert.That(response.Success, Is.True); + + Assert.That(ticket.SoldTo, Is.SameAs(customer)); + Assert.That(ticket.Sold, Is.True); + + this.Repository.Received().Commit(); + } + } +} diff --git a/ShowPlanner.Commands.Unit.Tests/ShowPlanner.Commands.Unit.Tests.csproj b/ShowPlanner.Commands.Unit.Tests/ShowPlanner.Commands.Unit.Tests.csproj index b6a1199..ee167a3 100644 --- a/ShowPlanner.Commands.Unit.Tests/ShowPlanner.Commands.Unit.Tests.csproj +++ b/ShowPlanner.Commands.Unit.Tests/ShowPlanner.Commands.Unit.Tests.csproj @@ -56,6 +56,7 @@ + diff --git a/ShowPlanner.Commands/ShowPlanner.Commands.csproj b/ShowPlanner.Commands/ShowPlanner.Commands.csproj index 8ac4919..6cde16c 100644 --- a/ShowPlanner.Commands/ShowPlanner.Commands.csproj +++ b/ShowPlanner.Commands/ShowPlanner.Commands.csproj @@ -54,6 +54,7 @@ + diff --git a/ShowPlanner.Commands/Tickets/PurchaseTicketCommand.cs b/ShowPlanner.Commands/Tickets/PurchaseTicketCommand.cs new file mode 100644 index 0000000..c83b304 --- /dev/null +++ b/ShowPlanner.Commands/Tickets/PurchaseTicketCommand.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShowPlanner.Data; +using ShowPlanner.Data.Models; + +namespace ShowPlanner.Commands.Tickets +{ + + public class PurchaseTicketRequest + { + public int TicketId { get; set; } + + public int CustomerId { get; set; } + } + + public class PurchaseTicketResponse + { + public bool Success { get; set; } + } + + public class PurchaseTicketCommand: ICommand + { + private readonly IDatabase _database; + + public PurchaseTicketCommand(IDatabase database) + { + _database = database; + } + + public PurchaseTicketResponse Execute(PurchaseTicketRequest request) + { + using (var repository = _database.NewRepository()) + { + var ticket = repository.Get(request.TicketId); + var customer = repository.Get(request.CustomerId); + ticket.SoldTo = customer; + ticket.Sold = true; + repository.Commit(); + + return new PurchaseTicketResponse() + { + Success = true, + }; + } + } + + public void Dispose() + { + } + } + + +} diff --git a/ShowPlanner.Data.Models/Customer.cs b/ShowPlanner.Data.Models/Customer.cs index a90ae40..fa4065d 100644 --- a/ShowPlanner.Data.Models/Customer.cs +++ b/ShowPlanner.Data.Models/Customer.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace ShowPlanner.Data.Models { @@ -6,8 +7,9 @@ public class Customer { public int? CustomerId { get; set; } - public string Name { get; set; } + public IList Tickets { get; set; } - public IList Tickets { get; set; } + [Required] + public string Name { get; set; } } } \ No newline at end of file diff --git a/ShowPlanner.Data.Models/Performance.cs b/ShowPlanner.Data.Models/Performance.cs index d17be31..d86dac3 100644 --- a/ShowPlanner.Data.Models/Performance.cs +++ b/ShowPlanner.Data.Models/Performance.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel.DataAnnotations; namespace ShowPlanner.Data.Models { @@ -7,12 +8,15 @@ public class Performance public int? PerformanceId { get; set; } public int? PerformerId { get; set; } + [Required] public Performer Performer { get; set; } public int? ShowId { get; set; } + [Required] public Show Show { get; set; } public int? StageId { get; set; } + [Required] public Stage Stage { get; set; } public DateTime? StartTime { get; set; } diff --git a/ShowPlanner.Data.Models/Performer.cs b/ShowPlanner.Data.Models/Performer.cs index 6a7eca5..e0a8d3a 100644 --- a/ShowPlanner.Data.Models/Performer.cs +++ b/ShowPlanner.Data.Models/Performer.cs @@ -1,12 +1,16 @@ using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace ShowPlanner.Data.Models { public class Performer { public int? PerformerId { get; set; } - public string Name { get; set; } public IList Performances { get; set; } + + [Required] + public string Name { get; set; } + } } \ No newline at end of file diff --git a/ShowPlanner.Data.Models/Show.cs b/ShowPlanner.Data.Models/Show.cs index 25d144e..09a4be9 100644 --- a/ShowPlanner.Data.Models/Show.cs +++ b/ShowPlanner.Data.Models/Show.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace ShowPlanner.Data.Models { @@ -6,8 +7,9 @@ public class Show { public int? ShowId { get; set; } - public string Title { get; set; } - public IList Tickets { get; set; } + + [Required] + public string Title { get; set; } } } \ No newline at end of file diff --git a/ShowPlanner.Data.Models/ShowPlanner.Data.Models.csproj b/ShowPlanner.Data.Models/ShowPlanner.Data.Models.csproj index b6a754b..2e17fc9 100644 --- a/ShowPlanner.Data.Models/ShowPlanner.Data.Models.csproj +++ b/ShowPlanner.Data.Models/ShowPlanner.Data.Models.csproj @@ -34,6 +34,7 @@ + diff --git a/ShowPlanner.Data.Models/Stage.cs b/ShowPlanner.Data.Models/Stage.cs index 5951c99..9db3dd4 100644 --- a/ShowPlanner.Data.Models/Stage.cs +++ b/ShowPlanner.Data.Models/Stage.cs @@ -1,15 +1,19 @@ using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace ShowPlanner.Data.Models { public class Stage { public int? StageId { get; set; } - public string Name { get; set; } public int? VenueId { get; set; } + [Required] public Venue Venue { get; set; } - public IList Showings { get; set; } + public IList Showings { get; set; } + + [Required] + public string Name { get; set; } } } \ No newline at end of file diff --git a/ShowPlanner.Data.Models/Ticket.cs b/ShowPlanner.Data.Models/Ticket.cs index ef63769..7fe6e31 100644 --- a/ShowPlanner.Data.Models/Ticket.cs +++ b/ShowPlanner.Data.Models/Ticket.cs @@ -1,3 +1,5 @@ +using System.ComponentModel.DataAnnotations; + namespace ShowPlanner.Data.Models { public class Ticket @@ -5,6 +7,7 @@ public class Ticket public int? TicketId { get; set; } public int? ShowId { get; set; } + [Required] public Show Show { get; set; } public int? CustomerId { get; set; } @@ -12,7 +15,7 @@ public class Ticket public bool? Sold { get; set; } + [Required] public decimal Price { get; set; } - } } \ No newline at end of file diff --git a/ShowPlanner.Data.Models/Venue.cs b/ShowPlanner.Data.Models/Venue.cs index 158a1d3..468ad00 100644 --- a/ShowPlanner.Data.Models/Venue.cs +++ b/ShowPlanner.Data.Models/Venue.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; @@ -8,10 +9,12 @@ public class Venue { public int? VenueId { get; set; } + public IList Stages { get; set; } + + [Required] public string Name { get; set; } public string Owner { get; set; } - public IList Stages { get; set; } } }