Guest User

Untitled

a guest
Apr 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. public class OrderRecord
  2. {
  3. public virtual int Id { get; set; }
  4. public virtual int CustomerId { get; set; }
  5. public virtual DateTime CreatedAt { get; set; }
  6. public virtual decimal SubTotal { get; set; }
  7. public virtual decimal Vat { get; set; }
  8. public virtual OrderStatus Status { get; set; }
  9. public virtual IList<OrderDetailRecord> Details { get; private set; }
  10. public virtual string PaymentServiceProviderResponse { get; set; }
  11. public virtual string PaymentReference { get; set; }
  12. public virtual DateTime? PaidAt { get; set; }
  13. public virtual DateTime? CompletedAt { get; set; }
  14. public virtual DateTime? CancelledAt { get; set; }
  15.  
  16. public virtual decimal Total
  17. {
  18. get { return SubTotal + Vat; }
  19. private set { }
  20. }
  21.  
  22. public virtual string Number
  23. {
  24. get { return (Id + 1000).ToString(CultureInfo.InvariantCulture); }
  25. private set { }
  26. }
  27.  
  28. public OrderRecord()
  29. {
  30. Details = new List<OrderDetailRecord>();
  31. }
  32.  
  33. public virtual void UpdateTotals()
  34. {
  35. var subTotal = 0m;
  36. var vat = 0m;
  37.  
  38. foreach (var detail in Details)
  39. {
  40. subTotal += detail.SubTotal;
  41. vat += detail.Vat;
  42. }
  43.  
  44. SubTotal = subTotal;
  45. Vat = vat;
  46. }
  47.  
  48. SchemaBuilder.CreateTable("OrderRecord", t => t
  49. .Column<int>("Id", c => c.PrimaryKey().Identity())
  50. .Column<int>("CustomerId", c => c.NotNull())
  51. .Column<DateTime>("CreatedAt", c => c.NotNull())
  52. .Column<decimal>("SubTotal", c => c.NotNull())
  53. .Column<decimal>("Vat", c => c.NotNull())
  54. .Column<string>("Status", c => c.WithLength(50).NotNull())
  55. .Column<string>("PaymentServiceProviderResponse", c => c.WithLength(null))
  56. .Column<string>("PaymentReference", c => c.WithLength(50))
  57. .Column<DateTime>("PaidAt", c => c.Nullable())
  58. .Column<DateTime>("CompletedAt", c => c.Nullable())
  59. .Column<DateTime>("CancelledAt", c => c.Nullable())
  60.  
  61. );
  62.  
  63. public OrderRecord CreateOrder(int customerId, IEnumerable<ShoppingCartItem> items)
  64. {
  65.  
  66. if (items == null)
  67. throw new ArgumentNullException("items");
  68.  
  69. // Convert to an array to avoid re-running the enumerable
  70. var itemsArray = items.ToArray();
  71.  
  72. if (!itemsArray.Any())
  73. throw new ArgumentException("Creating an order with 0 items is not supported", "items");
  74.  
  75. var order = new OrderRecord
  76. {
  77. CreatedAt = _dateTimeService.Now,
  78. CustomerId = customerId,
  79. Status = OrderStatus.New
  80. };
  81.  
  82. _orderRepository.Create(order);
Add Comment
Please, Sign In to add comment