|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.EntityFrameworkCore; |
| 6 | + |
| 7 | +namespace PostgresEFCoreImpl; |
| 8 | + |
| 9 | +public class Queries |
| 10 | +{ |
| 11 | + private readonly SalesDbContext _dbContext; |
| 12 | + |
| 13 | + public Queries(SalesDbContext dbContext) |
| 14 | + { |
| 15 | + _dbContext = dbContext; |
| 16 | + } |
| 17 | + |
| 18 | + public DbContext DbContext => _dbContext; |
| 19 | + |
| 20 | + // Result type for GetCustomerOrders matching SqlC output |
| 21 | + public record GetCustomerOrdersRow( |
| 22 | + Guid OrderId, |
| 23 | + DateTime OrderedAt, |
| 24 | + string OrderState, |
| 25 | + decimal TotalAmount, |
| 26 | + Guid OrderItemId, |
| 27 | + int Quantity, |
| 28 | + decimal UnitPrice, |
| 29 | + int ProductId, |
| 30 | + string ProductName, |
| 31 | + string ProductCategory |
| 32 | + ); |
| 33 | + |
| 34 | + public record GetCustomerOrdersArgs(int CustomerId, int Offset, int Limit); |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Get customer orders with all order items and product details, ordered by date descending with pagination |
| 38 | + /// </summary> |
| 39 | + public async Task<List<GetCustomerOrdersRow>> GetCustomerOrders(GetCustomerOrdersArgs args) |
| 40 | + { |
| 41 | + var results = await _dbContext.Orders |
| 42 | + .Where(o => o.CustomerId == args.CustomerId) |
| 43 | + .OrderByDescending(o => o.OrderedAt) |
| 44 | + .Skip(args.Offset) |
| 45 | + .Take(args.Limit) |
| 46 | + .SelectMany(o => o.OrderItems.Select(i => new |
| 47 | + { |
| 48 | + Order = o, |
| 49 | + OrderItem = i, |
| 50 | + Product = i.Product |
| 51 | + })) |
| 52 | + .Select(x => new GetCustomerOrdersRow( |
| 53 | + x.Order.OrderId, |
| 54 | + x.Order.OrderedAt, |
| 55 | + x.Order.OrderState, |
| 56 | + x.Order.TotalAmount, |
| 57 | + x.OrderItem.OrderItemId, |
| 58 | + x.OrderItem.Quantity, |
| 59 | + x.OrderItem.UnitPrice, |
| 60 | + x.Product.ProductId, |
| 61 | + x.Product.Name, |
| 62 | + x.Product.Category |
| 63 | + )) |
| 64 | + .ToListAsync(); |
| 65 | + |
| 66 | + return results; |
| 67 | + } |
| 68 | + |
| 69 | + public record AddProductsArgs(string Name, string Category, decimal UnitPrice, int StockQuantity, string? Description); |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Bulk insert products |
| 73 | + /// </summary> |
| 74 | + public async Task AddProducts(List<AddProductsArgs> args) |
| 75 | + { |
| 76 | + var products = args.Select(a => new Product |
| 77 | + { |
| 78 | + Name = a.Name, |
| 79 | + Category = a.Category, |
| 80 | + UnitPrice = a.UnitPrice, |
| 81 | + StockQuantity = a.StockQuantity, |
| 82 | + Description = a.Description, |
| 83 | + AddedAt = DateTime.UtcNow |
| 84 | + }).ToList(); |
| 85 | + |
| 86 | + await _dbContext.Products.AddRangeAsync(products); |
| 87 | + await _dbContext.SaveChangesAsync(); |
| 88 | + } |
| 89 | + |
| 90 | + public record AddOrdersArgs(int CustomerId, string OrderState, decimal TotalAmount); |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Bulk insert orders |
| 94 | + /// </summary> |
| 95 | + public async Task AddOrders(List<AddOrdersArgs> args) |
| 96 | + { |
| 97 | + var orders = args.Select(a => new Order |
| 98 | + { |
| 99 | + OrderId = Guid.NewGuid(), |
| 100 | + CustomerId = a.CustomerId, |
| 101 | + OrderState = a.OrderState, |
| 102 | + TotalAmount = a.TotalAmount, |
| 103 | + OrderedAt = DateTime.UtcNow |
| 104 | + }).ToList(); |
| 105 | + |
| 106 | + await _dbContext.Orders.AddRangeAsync(orders); |
| 107 | + await _dbContext.SaveChangesAsync(); |
| 108 | + } |
| 109 | + |
| 110 | + public record AddOrderItemsArgs(Guid OrderId, int ProductId, int Quantity, decimal UnitPrice); |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Bulk insert order items |
| 114 | + /// </summary> |
| 115 | + public async Task AddOrderItems(List<AddOrderItemsArgs> args) |
| 116 | + { |
| 117 | + var orderItems = args.Select(a => new OrderItem |
| 118 | + { |
| 119 | + OrderItemId = Guid.NewGuid(), |
| 120 | + OrderId = a.OrderId, |
| 121 | + ProductId = a.ProductId, |
| 122 | + Quantity = a.Quantity, |
| 123 | + UnitPrice = a.UnitPrice |
| 124 | + }).ToList(); |
| 125 | + |
| 126 | + await _dbContext.OrderItems.AddRangeAsync(orderItems); |
| 127 | + await _dbContext.SaveChangesAsync(); |
| 128 | + } |
| 129 | +} |
0 commit comments