Skip to content

Commit

Permalink
Implemented getting the receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymonPobiega committed Mar 3, 2011
1 parent 1f2aa6e commit 4affc59
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 6 deletions.
33 changes: 32 additions & 1 deletion src/Restbucks.Service.Tests/PaymentResourceTests.cs
Expand Up @@ -52,6 +52,37 @@ public void Pay_should_return_400_if_amount_does_not_match()
Assert.AreEqual(HttpStatusCode.BadRequest, responseMessage.StatusCode);
}

[Test]
public void Pay_should_return_400_if_payment_id_is_not_integer()
{
var order = CreateOrder();
var id = _repository.Store(order);
var representation = CreatePayment();

var responseMessage = new HttpResponseMessage();

_sut.Pay("aaa", representation,
new HttpRequestMessage(HttpMethod.Put, "http://restbucks.net/payment/" + id),
responseMessage);

Assert.AreEqual(HttpStatusCode.BadRequest, responseMessage.StatusCode);
}
[Test]
public void Pay_should_return_404_if_payment_id_is_invalid()
{
var order = CreateOrder();
var id = _repository.Store(order);
var representation = CreatePayment();

var responseMessage = new HttpResponseMessage();

_sut.Pay("13", representation,
new HttpRequestMessage(HttpMethod.Put, "http://restbucks.net/payment/" + id),
responseMessage);

Assert.AreEqual(HttpStatusCode.NotFound, responseMessage.StatusCode);
}

[Test]
public void Pay_should_return_200_if_unpaid()
{
Expand All @@ -67,7 +98,7 @@ public void Pay_should_return_200_if_unpaid()

Assert.AreEqual(HttpStatusCode.Created, responseMessage.StatusCode);
Assert.AreEqual("http://restbucks.net/order/1", responseBody.OrderLink);
//Assert.AreEqual("http://restbucks.net/order/1", responseBody.ReceiptLink);
Assert.AreEqual("http://restbucks.net/receipt/1", responseBody.ReceiptLink);
}

private static Order CreateOrder()
Expand Down
51 changes: 51 additions & 0 deletions src/Restbucks.Service.Tests/ReceiptResourceTests.cs
@@ -0,0 +1,51 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using NUnit.Framework;
using Restbucks.Service.Activities;
using Restbucks.Service.Domain;
using Restbucks.Service.Infrastructure;
using Restbucks.Service.Mappers;
using Restbucks.Service.Representations;
using Restbucks.Service.Resources;

namespace Restbucks.Service.Tests
{
[TestFixture]
public class ReceiptResourceTests
{
private ReceiptResource _sut;
private InMemoryOrderRepository _repository;

[Test]
public void Get_should_return_receipt_with_amount_paid()
{
var order = CreateOrder();
order.Pay(new PaymentInformation(1, "", "", 12, 12));
var id = _repository.Store(order);

var responseMessage = new HttpResponseMessage();

var responseBody = _sut.Get(id.ToString(),
new HttpRequestMessage(HttpMethod.Get, "http://restbucks.net/receipt/" + id),
responseMessage);

Assert.AreEqual(HttpStatusCode.OK, responseMessage.StatusCode);
Assert.AreEqual(1m, responseBody.AmountPaid);
}

private static Order CreateOrder()
{
return new Order(Location.InStore, new[] { new Item(Drink.Espresso, Size.Medium, Milk.Semi) });
}

[SetUp]
public void Initialize()
{
_repository = new InMemoryOrderRepository();
var mapper = new ReceiptRepresentationMapper();
_sut = new ReceiptResource(new ReadReceiptActivity(_repository, mapper));
}
}
}
1 change: 1 addition & 0 deletions src/Restbucks.Service.Tests/Restbucks.Service.Tests.csproj
Expand Up @@ -56,6 +56,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ReceiptResourceTests.cs" />
<Compile Include="PaymentResourceTests.cs" />
<Compile Include="OrderResourceTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
9 changes: 9 additions & 0 deletions src/Restbucks.Service/Activities/IReadReceiptActivity.cs
@@ -0,0 +1,9 @@
using Restbucks.Service.Representations;

namespace Restbucks.Service.Activities
{
public interface IReadReceiptActivity
{
ReceiptRepresentation Read(int orderId, string baseUri);
}
}
8 changes: 4 additions & 4 deletions src/Restbucks.Service/Activities/PaymentActivity.cs
Expand Up @@ -31,10 +31,10 @@ public PaymentRepresentation Pay(int orderId, PaymentRepresentation paymentRepre

var payment = _paymentMapper.GetDomainObject(paymentRepresentation);
order.Pay(payment);
var result = _paymentMapper.GetRepresentation(order.PaymentInfo);
result.OrderLink = RestbucksResources.GetResourceUri<OrderResource>(baseUri, order.Id.ToString());
//Add receipt link
return result;
var representation = _paymentMapper.GetRepresentation(order.PaymentInfo);
representation.OrderLink = RestbucksResources.GetResourceUri<OrderResource>(baseUri, order.Id.ToString());
representation.ReceiptLink = RestbucksResources.GetResourceUri<ReceiptResource>(baseUri, orderId.ToString());
return representation;
}
}
}
2 changes: 1 addition & 1 deletion src/Restbucks.Service/Activities/ReadOrderActivity.cs
Expand Up @@ -42,7 +42,7 @@ public OrderRepresentation Read(int orderId, string baseUri)
}
else if (order.Status == OrderStatus.Ready)
{
//receipt
representation.ReceiptLink = RestbucksResources.GetResourceUri<ReceiptResource>(baseUri,orderId.ToString());
}
return representation;
}
Expand Down
32 changes: 32 additions & 0 deletions src/Restbucks.Service/Activities/ReadReceiptActivity.cs
@@ -0,0 +1,32 @@
using System;
using Restbucks.Service.Domain;
using Restbucks.Service.Mappers;
using Restbucks.Service.Representations;
using Restbucks.Service.Resources;

namespace Restbucks.Service.Activities
{
public class ReadReceiptActivity : IReadReceiptActivity
{
private readonly IOrderRepository _repository;
private readonly ReceiptRepresentationMapper _receiptMapper;

public ReadReceiptActivity(IOrderRepository repository, ReceiptRepresentationMapper receiptMapper)
{
_repository = repository;
_receiptMapper = receiptMapper;
}

public ReceiptRepresentation Read(int orderId, string baseUri)
{
var order = _repository.FindById(orderId);
if (order == null)
{
throw new NoSuchOrderException(orderId);
}
var representation = _receiptMapper.GetRepresentation(order);
representation.OrderLink = RestbucksResources.GetResourceUri<OrderResource>(baseUri, orderId.ToString());
return representation;
}
}
}
3 changes: 3 additions & 0 deletions src/Restbucks.Service/Domain/Order.cs
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -16,6 +17,7 @@ public IEnumerable<Item> Items
public PaymentInformation PaymentInfo { get; private set; }
public Location Location { get; private set; }
public OrderStatus Status { get; private set; }
public DateTime PaymentDateUtc { get; private set; }

public Order(Location location, IEnumerable<Item> items)
{
Expand All @@ -30,6 +32,7 @@ public void Pay(PaymentInformation paymentInformation)
throw new InvalidPaymentException();
}
Status = OrderStatus.Preparing;
PaymentDateUtc = DateTime.UtcNow;
PaymentInfo = paymentInformation;
}

Expand Down
17 changes: 17 additions & 0 deletions src/Restbucks.Service/Mappers/ReceiptRepresentationMapper.cs
@@ -0,0 +1,17 @@
using Restbucks.Service.Domain;
using Restbucks.Service.Representations;

namespace Restbucks.Service.Mappers
{
public class ReceiptRepresentationMapper
{
public ReceiptRepresentation GetRepresentation(Order order)
{
return new ReceiptRepresentation
{
AmountPaid = order.PaymentInfo.Amount,
PaymentDate = order.PaymentDateUtc.ToLongDateString()
};
}
}
}
7 changes: 7 additions & 0 deletions src/Restbucks.Service/Representations/OrderRepresentation.cs
Expand Up @@ -40,6 +40,13 @@ public string PaymentLink
set { SetLink(RestbucksRelation.Payment, value); }
}

[XmlIgnore]
public string ReceiptLink
{
get { return GetLinkByRel(RestbucksRelation.Receipt).UnlessNull(x => x.Uri); }
set { SetLink(RestbucksRelation.Receipt, value); }
}

[XmlIgnore]
public string SelfLink
{
Expand Down
22 changes: 22 additions & 0 deletions src/Restbucks.Service/Representations/ReceiptRepresentation.cs
@@ -0,0 +1,22 @@
using System;
using System.Xml.Serialization;

namespace Restbucks.Service.Representations
{
[XmlRoot(Namespace = RestbucksNamespace, ElementName = "receipt")]
public class ReceiptRepresentation : RepresentationBase
{
[XmlElement(ElementName = "amount")]
public decimal AmountPaid { get; set; }

[XmlElement(ElementName = "paid")]
public string PaymentDate { get; set; }

[XmlIgnore]
public string OrderLink
{
get { return GetLinkByRel(RestbucksRelation.Order).UnlessNull(x => x.Uri); }
set { SetLink(RestbucksRelation.Order, value); }
}
}
}
47 changes: 47 additions & 0 deletions src/Restbucks.Service/Resources/ReceiptResource.cs
@@ -0,0 +1,47 @@
using System;
using System.Net;
using System.Net.Http;
using System.ServiceModel;
using System.ServiceModel.Web;
using Restbucks.Service.Activities;
using Restbucks.Service.Domain;
using Restbucks.Service.Representations;

namespace Restbucks.Service.Resources
{
[ServiceContract]
public class ReceiptResource
{
private readonly IReadReceiptActivity _readReceiptActivity;

public ReceiptResource(IReadReceiptActivity readReceiptActivity)
{
_readReceiptActivity = readReceiptActivity;
}

[WebGet(
UriTemplate = "/{orderId}",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml)]
public ReceiptRepresentation Get(string orderId, HttpRequestMessage requestMessage, HttpResponseMessage responseMessage)
{
int id;
if (int.TryParse(orderId, out id))
{
try
{
var baseUri = requestMessage.RequestUri.GetLeftPart(UriPartial.Authority);
var response = _readReceiptActivity.Read(id, baseUri);
return response;
}
catch (NoSuchOrderException)
{
responseMessage.StatusCode = HttpStatusCode.NotFound;
return null;
}
}
responseMessage.StatusCode = HttpStatusCode.BadRequest;
return null;
}
}
}
5 changes: 5 additions & 0 deletions src/Restbucks.Service/Restbucks.Service.csproj
Expand Up @@ -83,6 +83,9 @@
<ItemGroup>
<Compile Include="Activities\CreateOrderActivity.cs" />
<Compile Include="Activities\ICreateOrderActivity.cs" />
<Compile Include="Activities\IReadReceiptActivity.cs" />
<Compile Include="Activities\ReadReceiptActivity.cs" />
<Compile Include="Mappers\ReceiptRepresentationMapper.cs" />
<Compile Include="Domain\InvalidPaymentException.cs" />
<Compile Include="Activities\IPaymentActivity.cs" />
<Compile Include="Activities\IReadOrderActivity.cs" />
Expand All @@ -95,6 +98,8 @@
<Compile Include="Activities\UnexpectedOrderStateException.cs" />
<Compile Include="Domain\PaymentInformation.cs" />
<Compile Include="Representations\PaymentRepresentation.cs" />
<Compile Include="Representations\ReceiptRepresentation.cs" />
<Compile Include="Resources\ReceiptResource.cs" />
<Compile Include="Resources\PaymentResource.cs" />
<Compile Include="Domain\IOrderRepository.cs" />
<Compile Include="Domain\Location.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/Restbucks.Service/RestbucksResources.cs
Expand Up @@ -21,6 +21,7 @@ static RestbucksResources()
{
Bind<OrderResource>("order");
Bind<PaymentResource>("payment");
Bind<ReceiptResource>("receipt");
}

public static void RegisterRoutes(HttpHostConfiguration configuration)
Expand Down

0 comments on commit 4affc59

Please sign in to comment.