From 3293776a3ea789985438a671160698d27ddbdf17 Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Wed, 11 Sep 2013 12:46:33 -0400 Subject: [PATCH] Update OrdersService.cs --- .../OrdersService.cs | 88 ++++++++----------- 1 file changed, 38 insertions(+), 50 deletions(-) diff --git a/src/ServiceStack.Northwind/ServiceStack.Northwind.ServiceInterface/OrdersService.cs b/src/ServiceStack.Northwind/ServiceStack.Northwind.ServiceInterface/OrdersService.cs index be03d606..d346439c 100644 --- a/src/ServiceStack.Northwind/ServiceStack.Northwind.ServiceInterface/OrdersService.cs +++ b/src/ServiceStack.Northwind/ServiceStack.Northwind.ServiceInterface/OrdersService.cs @@ -1,51 +1,39 @@ -namespace ServiceStack.Northwind.ServiceInterface +using System.Collections.Generic; +using System.Linq; +using ServiceStack.Common.Extensions; +using ServiceStack.Northwind.ServiceModel.Operations; +using ServiceStack.Northwind.ServiceModel.Types; +using ServiceStack.OrmLite; +using ServiceStack.ServiceInterface; + +namespace ServiceStack.Northwind.ServiceInterface { - using System.Collections.Generic; - using System.Linq; - using ServiceStack.Common.Extensions; - using ServiceStack.Northwind.ServiceModel.Operations; - using ServiceStack.Northwind.ServiceModel.Types; - using ServiceStack.OrmLite; - using ServiceStack.ServiceInterface; - - public class OrdersService : Service - { - private const int PageCount = 20; - - public IDbConnectionFactory DbFactory { get; set; } - - public object Get(Orders request) - { - using (var dbConn = DbFactory.OpenDbConnection()) - using (var dbCmd = dbConn.CreateCommand()) - { - List orders; - - if (request.CustomerId.IsNullOrEmpty()) - { - orders = dbCmd.Select(order => order.OrderByDescending(o => o.OrderDate)) - .Skip((request.Page.GetValueOrDefault(1) - 1)*PageCount) - .Take(PageCount) - .ToList(); - } - else orders = dbCmd.Select(order => order.Where(o => o.CustomerId == request.CustomerId)); - - if (orders.Count == 0) - return new OrdersResponse(); - - var orderDetails = dbCmd.Select(detail => Sql.In(detail.OrderId, orders.ConvertAll(x => x.Id))); - - var orderDetailsLookup = orderDetails.ToLookup(o => o.OrderId); - - var customerOrders = orders.ConvertAll(o => - new CustomerOrder - { - Order = o, - OrderDetails = orderDetailsLookup[o.Id].ToList() - }); - - return new OrdersResponse {Results = customerOrders}; - } - } - } -} \ No newline at end of file + public class OrdersService : Service + { + private const int PageCount = 20; + + public object Get(Orders request) + { + var orders = request.CustomerId.IsNullOrEmpty() + ? Db.Select(order => order.OrderByDescending(o => o.OrderDate)) + .Skip((request.Page.GetValueOrDefault(1) - 1)*PageCount) + .Take(PageCount) + .ToList() + : Db.Select(order => order.Where(o => o.CustomerId == request.CustomerId)); + + if (orders.Count == 0) + return new OrdersResponse(); + + var orderDetails = Db.Select(detail => Sql.In(detail.OrderId, orders.ConvertAll(x => x.Id))); + + var orderDetailsLookup = orderDetails.ToLookup(o => o.OrderId); + + var customerOrders = orders.ConvertAll(o => new CustomerOrder { + Order = o, + OrderDetails = orderDetailsLookup[o.Id].ToList() + }); + + return new OrdersResponse { Results = customerOrders }; + } + } +}