Cleaned up readme sample

This commit is contained in:
Koen Bekkenutte
2021-06-04 02:03:36 +08:00
parent 4db518896b
commit 7ba1caa133
3 changed files with 10 additions and 5 deletions

View File

@@ -12,6 +12,7 @@ namespace ReadmeSample.Entities
public int Id { get; set; }
public int UserId { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime? FulfilledDate { get; set; }
public decimal TaxRate { get; set; }
@@ -20,6 +21,6 @@ namespace ReadmeSample.Entities
[Projectable] public decimal Subtotal => Items.Sum(item => item.Product.ListPrice * item.Quantity);
[Projectable] public decimal Tax => Subtotal * TaxRate;
[Projectable] public decimal GrandTotal => Subtotal + Tax;
[Projectable] public decimal GrandTotal => Subtotal * TaxRate;
}
}

View File

@@ -11,7 +11,10 @@ namespace ReadmeSample.Extensions
public static class UserExtensions
{
[Projectable]
public static Order GetMostRecentOrderForUser(this User user, DateTime? cutoffDate)
=> user.Orders.Where(x => x.CreatedDate >= cutoffDate).OrderByDescending(x => x.CreatedDate).FirstOrDefault();
public static Order GetMostRecentOrderForUser(this User user, bool includeUnfulfilled) =>
user.Orders
.Where(x => !includeUnfulfilled ? x .FulfilledDate != null : true)
.OrderByDescending(x => x.CreatedDate)
.FirstOrDefault();
}
}

View File

@@ -23,7 +23,8 @@ namespace ReadmeSample
var sampleOrder = new Order {
User = sampleUser,
TaxRate = .19m,
CreatedDate = DateTime.UtcNow,
CreatedDate = DateTime.UtcNow.AddDays(-1),
FulfilledDate = DateTime.UtcNow,
Items = new List<OrderItem> {
new OrderItem { Product = sampleProduct, Quantity = 5 }
}
@@ -36,7 +37,7 @@ namespace ReadmeSample
var query = dbContext.Users
.Where(x => x.UserName == "Jon")
.Select(x => new {
x.GetMostRecentOrderForUser(DateTime.UtcNow.AddDays(-30)).GrandTotal
GrandTotal = x.GetMostRecentOrderForUser(/* includeUnfulfilled: */ false).GrandTotal
});
var result = query.First();