Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f2aa6e
commit 4affc59
Showing
14 changed files
with
232 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Restbucks.Service.Representations; | ||
|
||
namespace Restbucks.Service.Activities | ||
{ | ||
public interface IReadReceiptActivity | ||
{ | ||
ReceiptRepresentation Read(int orderId, string baseUri); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/Restbucks.Service/Mappers/ReceiptRepresentationMapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/Restbucks.Service/Representations/ReceiptRepresentation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters