public CustomerService(MyShopOnLineDataContext context) { this.dbContext = context; context.SavingChanges += (sender, args) => { Console.WriteLine($"Saving changes for {((DbContext)sender).Database.GetConnectionString()}"); }; context.SavedChanges += (sender, args) => { Console.WriteLine($"Saved {args.EntitiesSavedCount} changes for {((DbContext)sender).Database.GetConnectionString()}"); }; }
internal static async Task <Order> GetEntity(this OrderRecord record, MyShopOnLineDataContext dbContext) { if (record == null) { return(null); } Customer customer = dbContext.Customers.FirstOrDefault(c => c.Email == record.CustomerEmail); if (customer == null) { throw new Exception("Specified customer is not valid."); } List <string> entryProductCodes = record.OrderEntries.Select(p => p.ProductCode).ToList(); Dictionary <string, Product> products = await(from prod in dbContext.Products where entryProductCodes.Contains(prod.Code) select prod).ToDictionaryAsync(x => x.Code, x => x); Order order = new Order() { Customer = customer, DeliveryDate = record.DeliveryDate, Delivered = record.Delivered, Number = string.IsNullOrEmpty(record.Number) ? DateTime.UtcNow.ToString("yyyyMMddHHmmssfff") : record.Number, OrderDate = record.OrderDate ?? DateTime.UtcNow, ReadyForShipping = record.ReadyForShipping, Shipped = record.Shipped, ShippingDate = record.ShppingDate, Total = record.OrderEntries.Sum(e => products[e.ProductCode].Price * e.EntryQuantity), Weight = record.OrderEntries.Sum(e => products[e.ProductCode].Weight * e.EntryQuantity), }; dbContext.OrderProducts.AddRange(record.OrderEntries.Select(oe => new OrderProduct() { Order = order, OrderNumber = order.Number, Product = products[oe.ProductCode], ProductCode = oe.ProductCode, Quantity = oe.EntryQuantity }).ToList()); return(order); }
internal static OrderRecord GetRecord(this Order order, MyShopOnLineDataContext dbContext) { if (order == null) { return(null); } var orderEntries = (from orderEntry in dbContext.OrderProducts where orderEntry.OrderNumber == order.Number select new { orderEntry.Product, orderEntry.Quantity }) .Select(oe => new OrderEntry(oe.Product.Code, oe.Quantity, oe.Product.Price, oe.Product.Weight, oe.Product.Description, oe.Product.Review)) .ToList(); return(new OrderRecord(order.Customer.Email, orderEntries, order.Number, order.OrderDate, order.Total, order.Weight, order.ReadyForShipping, order.Delivered, order.DeliveryDate, order.Shipped, order.ShippingDate)); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, MyShopOnLineDataContext dbContext) { dbContext.Database.Migrate(); //if (env.IsDevelopment()) //{ app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Backend Services API v1")); //} //app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }