-
Notifications
You must be signed in to change notification settings - Fork 271
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
Showing
1 changed file
with
38 additions
and
50 deletions.
There are no files selected for viewing
88 changes: 38 additions & 50 deletions
88
src/ServiceStack.Northwind/ServiceStack.Northwind.ServiceInterface/OrdersService.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 |
---|---|---|
@@ -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<Order> orders; | ||
|
||
if (request.CustomerId.IsNullOrEmpty()) | ||
{ | ||
orders = dbCmd.Select<Order>(order => order.OrderByDescending(o => o.OrderDate)) | ||
.Skip((request.Page.GetValueOrDefault(1) - 1)*PageCount) | ||
.Take(PageCount) | ||
.ToList(); | ||
} | ||
else orders = dbCmd.Select<Order>(order => order.Where(o => o.CustomerId == request.CustomerId)); | ||
|
||
if (orders.Count == 0) | ||
return new OrdersResponse(); | ||
|
||
var orderDetails = dbCmd.Select<OrderDetail>(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}; | ||
} | ||
} | ||
} | ||
} | ||
public class OrdersService : Service | ||
{ | ||
private const int PageCount = 20; | ||
|
||
public object Get(Orders request) | ||
{ | ||
var orders = request.CustomerId.IsNullOrEmpty() | ||
? Db.Select<Order>(order => order.OrderByDescending(o => o.OrderDate)) | ||
.Skip((request.Page.GetValueOrDefault(1) - 1)*PageCount) | ||
.Take(PageCount) | ||
.ToList() | ||
: Db.Select<Order>(order => order.Where(o => o.CustomerId == request.CustomerId)); | ||
|
||
if (orders.Count == 0) | ||
return new OrdersResponse(); | ||
|
||
var orderDetails = Db.Select<OrderDetail>(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 }; | ||
} | ||
} | ||
} |