public static IBookOrderLine Add(this IBookOrder order, IBook book, int quantity) { var session = EntityHelper.GetSession(order); var line = session.NewEntity <IBookOrderLine>(); line.Order = order; line.Book = book; line.Quantity = quantity; line.Price = book.Price; order.Lines.Add(line); return(line); }
//Schedules update LINQ-based command that will recalculate totals at the end of SaveChanges transaction public static void ScheduleUpdateTotal(this IBookOrder order) { // only open orders; completed order might have coupon discount applied if (order.Status != OrderStatus.Open) { return; } var session = EntityHelper.GetSession(order); var orderTotalQuery = from bol in session.EntitySet <IBookOrderLine>() where bol.Order.Id == order.Id group bol by bol.Order.Id into orderUpdate select new { Id = orderUpdate.Key, Total = orderUpdate.Sum(line => line.Price * line.Quantity) }; session.ScheduleUpdate <IBookOrder>(orderTotalQuery, CommandSchedule.TransactionEnd); }
public static BookOrder ToModel(this IBookOrder order, bool details = false) { if (order == null) { return(null); } var ord = new BookOrder() { Id = order.Id, CreatedOn = order.CreatedOn, Total = order.Total, Status = order.Status, UserId = order.User.Id, UserName = order.User.DisplayName }; if (details) { ord.Items = order.Lines.Select(l => l.ToModel()).ToList(); } return(ord); }
public static void CompleteOrder(this IBookOrder order, string couponCode = null) { var session = EntityHelper.GetSession(order); order.Total = order.Lines.Sum(line => line.Price * line.Quantity); if (!string.IsNullOrWhiteSpace(couponCode)) { var entCoupon = LookupCoupon(session, couponCode); session.Context.ThrowIfNull(entCoupon, ClientFaultCodes.ObjectNotFound, "Coupon", "Coupon with code '{0}' not found.", couponCode); if (entCoupon != null && entCoupon.ExpiresOn >= DateTime.Now && entCoupon.AppliedOn == null) { entCoupon.AppliedOn = DateTime.Now; order.Total = (decimal)(((double)order.Total) * ((100 - entCoupon.DiscountPerc) / 100.0)); } } order.Status = OrderStatus.Completed; }
// Static method computing order summary public static string GetOrderSummary(IBookOrder order) { return order.CreatedOn.ToString("s") + " " + order.User.DisplayName + ", Total: " + order.Total.ToString("###.##"); }
public static string GetOrderDisplay(IBookOrder order) { return string.Format("{0}, {1} items.", order.User.DisplayName, order.Lines.Count); }
public static string GetOrderDisplay(IBookOrder order) { return(string.Format("{0}, {1} items.", order.User.DisplayName, order.Lines.Count)); }
// Static method computing order summary public static string GetOrderSummary(IBookOrder order) { return(order.CreatedOn.ToString("s") + " " + order.User.DisplayName + ", Total: " + order.Total.ToString("###.##")); }