Skip to content
Browse files

POST update for /order/id moved to Nancy

  • Loading branch information...
1 parent 274dc9f commit f6bae07626d8ffb54c8a470e67e1b8dc05d145eb @horsdal committed Mar 15, 2012
View
84 RestBucks.Tests/Resources/WhenUserUpdateAnOrder.cs
@@ -1,34 +1,60 @@
-using System.Net;
-using NUnit.Framework;
-using RestBucks.Domain;
-using RestBucks.Infrastructure.Linking;
-using RestBucks.Resources.Orders;
-using RestBucks.Resources.Orders.Representations;
-using RestBucks.Tests.Util;
-using SharpTestsEx;
-
namespace RestBucks.Tests.Resources
{
- [TestFixture]
- public class WhenUserUpdateAnOrder
+ using NUnit.Framework;
+
+ using Nancy;
+
+ using RestBucks.Domain;
+
+ using Infrastructure.Linking;
+
+ using RestBucks.Resources.Orders;
+ using RestBucks.Resources.Orders.Representations;
+
+ using Util;
+
+ using SharpTestsEx;
+
+ [TestFixture]
+ public class WhenUserUpdateAnOrder : ResourceHandlerTestBase
+ {
+ private readonly IResourceLinker resourceLinker = new ResourceLinker();
+
+ [Test]
+ public void WhenOrderDoesNotExist_ThenReturn404()
{
- private readonly IResourceLinker resourceLinker = new ResourceLinker();
-
- [Test]
- public void WhenOrderDoesNotExist_ThenReturn404()
- {
- var handler = new OrderResourceHandler(new RepositoryStub<Order>(), resourceLinker);
- var response = handler.Update(123, new OrderRepresentation());
- response.StatusCode.Should().Be.EqualTo(HttpStatusCode.NotFound);
- }
-
- [Test]
- public void WhenOrderExist_ThenUpdateLocation()
- {
- var order = new Order {Id = 123, Location = Location.InShop};
- var handler = new OrderResourceHandler(new RepositoryStub<Order>(order), resourceLinker);
- handler.Update(123, new OrderRepresentation {Location = Location.TakeAway});
- order.Location.Should().Be.EqualTo(Location.TakeAway);
- }
+ // Arrange
+ var app = CreateAppProxy();
+
+ // Act
+ var response = app.Post("/order/123/",
+ with =>
+ {
+ with.HttpRequest();
+ with.Body(new OrderRepresentation().ToXmlString());
+ });
+ // Assert
+ response.StatusCode.Should().Be.EqualTo(HttpStatusCode.NotFound);
+ }
+
+ [Test]
+ public void WhenOrderExist_ThenUpdateLocation()
+ {
+ // Arrange
+ var order = new Order {Id = 123, Location = Location.InShop};
+ var app = CreateAppProxy(new RepositoryStub<Order>(order));
+
+ // Act
+ var response = app.Post("order/123/",
+ with =>
+ {
+ with.HttpRequest();
+ with.Body(new OrderRepresentation {Location = Location.TakeAway}.ToXmlString());
+ });
+
+ // Assert
+ response.StatusCode.Should().Be.EqualTo(HttpStatusCode.NoContent);
+ order.Location.Should().Be.EqualTo(Location.TakeAway);
}
+ }
}
View
14 RestBucks/Resources/Orders/OrderResourceHandler.cs
@@ -12,14 +12,15 @@
namespace RestBucks.Resources.Orders
{
using Nancy;
+ using Nancy.ModelBinding;
[ServiceContract, WithUriPrefix("order")]
public class OrderResourceHandler : NancyModule
{
private readonly IRepository<Order> orderRepository;
private readonly IResourceLinker linker;
- public static string BaseResoureUriTemplate = "/{orderId}/";
+ public static string SlashOrderId = "/{orderId}/";
public static string Path = "/order";
public OrderResourceHandler(IRepository<Order> orderRepository, IResourceLinker linker)
@@ -28,7 +29,8 @@ public OrderResourceHandler(IRepository<Order> orderRepository, IResourceLinker
this.orderRepository = orderRepository;
this.linker = linker;
- Get[BaseResoureUriTemplate] = parameters => GetHandler((int) parameters.orderId);
+ Get[SlashOrderId] = parameters => GetHandler((int) parameters.orderId);
+ Post[SlashOrderId] = parameter => Update((int)parameter.orderId, this.Bind<OrderRepresentation>());
}
[WebGet(UriTemplate = "{orderId}")]
@@ -53,12 +55,14 @@ public Response GetHandler(int orderId)
}
[WebInvoke(UriTemplate = "{orderId}", Method = "POST")]
- public HttpResponseMessage Update(int orderId, OrderRepresentation orderRepresentation)
+ public Response Update(int orderId, OrderRepresentation orderRepresentation)
{
var order = orderRepository.GetById(orderId);
- if (order == null) return Responses.NotFound();
+ if (order == null)
+ return Response.NotFound();
+
order.Location = orderRepresentation.Location;
- return Responses.NoContent();
+ return HttpStatusCode.NoContent;
}
[WebInvoke(UriTemplate = "{orderId}", Method = "DELETE")]
View
2 RestBucks/Resources/Orders/OrdersResourceHandler.cs
@@ -85,7 +85,7 @@ private Response Created(Order order)
{
var uri = resourceLinker.BuildUriString(
OrderResourceHandler.Path,
- OrderResourceHandler.BaseResoureUriTemplate,
+ OrderResourceHandler.SlashOrderId,
new {orderId = order.Id});
return Response.Created(uri);
View
2 RestBucks/Resources/Orders/Representations/OrderRepresentationMapper.cs
@@ -32,7 +32,7 @@ private static IEnumerable<Link> GetLinks(Order order)
var linker = new ResourceLinker();
var get = new Link(linker.BuildUriString(OrderResourceHandler.Path,
- OrderResourceHandler.BaseResoureUriTemplate,
+ OrderResourceHandler.SlashOrderId,
new {orderId = order.Id}),
baseUri + "docs/order-get.htm",
MediaTypes.Default);

0 comments on commit f6bae07

Please sign in to comment.
Something went wrong with that request. Please try again.