Skip to content

Commit

Permalink
Added PurchaseTicketCommand and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
crmckenzie committed Mar 28, 2013
1 parent 3a1d929 commit 412d381
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 10 deletions.
2 changes: 1 addition & 1 deletion 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).
77 changes: 77 additions & 0 deletions 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<IDatabase>();
this.Repository = Substitute.For<IRepository>();
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<Customer>
.CreateNew()
.Build()
;

var ticket = Builder<Ticket>
.CreateNew()
.Do(row=>
{
row.SoldTo = null;
row.Sold = false;
})
.Build()
;

this.Repository.Get<Customer>(customer.CustomerId.Value).Returns(customer);
this.Repository.Get<Ticket>(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();
}
}
}
Expand Up @@ -56,6 +56,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="PurchaseTicketCommandTests.cs" />
<Compile Include="QueryPerformanceCommandTests.cs" />
<Compile Include="QueryVenueCommandTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
1 change: 1 addition & 0 deletions ShowPlanner.Commands/ShowPlanner.Commands.csproj
Expand Up @@ -54,6 +54,7 @@
<Compile Include="Performances\IPerformanceCommands.cs" />
<Compile Include="Performances\QueryPerformanceCommand.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tickets\PurchaseTicketCommand.cs" />
<Compile Include="Venues\QueryVenueCommand.cs" />
<Compile Include="Venues\QueryVenueRequest.cs" />
<Compile Include="Venues\VenueDetail.cs" />
Expand Down
56 changes: 56 additions & 0 deletions 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<PurchaseTicketRequest, PurchaseTicketResponse>
{
private readonly IDatabase _database;

public PurchaseTicketCommand(IDatabase database)
{
_database = database;
}

public PurchaseTicketResponse Execute(PurchaseTicketRequest request)
{
using (var repository = _database.NewRepository())
{
var ticket = repository.Get<Ticket>(request.TicketId);
var customer = repository.Get<Customer>(request.CustomerId);
ticket.SoldTo = customer;
ticket.Sold = true;
repository.Commit();

return new PurchaseTicketResponse()
{
Success = true,
};
}
}

public void Dispose()
{
}
}


}
6 changes: 4 additions & 2 deletions ShowPlanner.Data.Models/Customer.cs
@@ -1,13 +1,15 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace ShowPlanner.Data.Models
{
public class Customer
{
public int? CustomerId { get; set; }

public string Name { get; set; }
public IList<Ticket> Tickets { get; set; }

public IList<Ticket> Tickets { get; set; }
[Required]
public string Name { get; set; }
}
}
4 changes: 4 additions & 0 deletions ShowPlanner.Data.Models/Performance.cs
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel.DataAnnotations;

namespace ShowPlanner.Data.Models
{
Expand All @@ -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; }
Expand Down
6 changes: 5 additions & 1 deletion 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<Performance> Performances { get; set; }

[Required]
public string Name { get; set; }

}
}
6 changes: 4 additions & 2 deletions ShowPlanner.Data.Models/Show.cs
@@ -1,13 +1,15 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace ShowPlanner.Data.Models
{
public class Show
{
public int? ShowId { get; set; }

public string Title { get; set; }

public IList<Ticket> Tickets { get; set; }

[Required]
public string Title { get; set; }
}
}
1 change: 1 addition & 0 deletions ShowPlanner.Data.Models/ShowPlanner.Data.Models.csproj
Expand Up @@ -34,6 +34,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down
8 changes: 6 additions & 2 deletions 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<Performance> Showings { get; set; }
public IList<Performance> Showings { get; set; }

[Required]
public string Name { get; set; }
}
}
5 changes: 4 additions & 1 deletion ShowPlanner.Data.Models/Ticket.cs
@@ -1,18 +1,21 @@
using System.ComponentModel.DataAnnotations;

namespace ShowPlanner.Data.Models
{
public class Ticket
{
public int? TicketId { get; set; }

public int? ShowId { get; set; }
[Required]
public Show Show { get; set; }

public int? CustomerId { get; set; }
public Customer SoldTo { get; set; }

public bool? Sold { get; set; }

[Required]
public decimal Price { get; set; }

}
}
5 changes: 4 additions & 1 deletion ShowPlanner.Data.Models/Venue.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;

Expand All @@ -8,10 +9,12 @@ public class Venue
{
public int? VenueId { get; set; }

public IList<Stage> Stages { get; set; }

[Required]
public string Name { get; set; }

public string Owner { get; set; }

public IList<Stage> Stages { get; set; }
}
}

0 comments on commit 412d381

Please sign in to comment.