public EfProductRepository(SportsStoreDbContext sportsStoreDbContext, ILogger <EfProductRepository> logger, IConfiguration configuration, IDistributedCache distributedCache) { _context = sportsStoreDbContext; _logger = logger; _configuration = configuration; _distributedCache = distributedCache; }
public IHttpActionResult PlaceOrder(PlaceOrderRequest request) { using (var db = new SportsStoreDbContext()) { var order = new Order { Name = request.Name, Street = request.Street, City = request.City, State = request.State, Zip = request.Zip, Country = request.Country, GiftWrap = request.GiftWrap }; foreach (var item in request.Products) { var product = db.Products.Find(item.Id); if (product == null) { return(BadRequest(string.Format("订单中商品[{0}]不存在", item.Id))); } order.OrderProducts.Add(new OrderProduct { ProductId = product.Id, Count = item.Count }); } db.Orders.Add(order); db.SaveChanges(); return(Ok(new { id = order.Id })); } }
public IHttpActionResult Orders() { using (var db = new SportsStoreDbContext()) { var orders = db.Orders .Include("OrderProducts.Product") .Select(o => new { id = o.Id, name = o.Name, street = o.Street, city = o.City, state = o.State, zip = o.Zip, country = o.Country, giftWrap = o.GiftWrap, products = o.OrderProducts .Select(p => new{ id = p.Product.Id, name = p.Product.Name, price = p.Product.Price, count = p.Count }).ToList() }).ToList(); return(Ok(orders)); } }
public IHttpActionResult PostProduct(ProductModel p) { using (var db = new SportsStoreDbContext()) { var product = db.Products.Find(p.Id); if (product == null) { product = new Product { Name = p.Name, Description = p.Description, Category = p.Category, Price = p.Price }; db.Products.Add(product); } else { product.Name = p.Name; product.Description = p.Description; product.Category = p.Category; product.Price = p.Price; } db.SaveChanges(); return(Ok(new { id = product.Id, name = product.Name, description = product.Description, price = product.Price })); } }
private void Dispose(bool disposing) { if (disposing) { if (_context != null) { _context.Dispose(); _context = null; } } }
protected virtual void Dispose(bool disposing) { if (disposing) { //Free managed resources if (_context != null) { _context.Dispose(); _context = null; } } }
public IHttpActionResult GetAllProducts() { using (var db = new SportsStoreDbContext()) { return(Ok(db.Products .Select(p => new { id = p.Id, name = p.Name, description = p.Description, category = p.Category, price = p.Price, }).ToList())); } }
public async Task InvokeAsync(HttpContext context, SportsStoreDbContext db, UserManager <IdentityUser> userManager) { if (!db.Users.Any()) { await SeedUser(userManager); } if (!db.Products.Any()) { await SeedProducts(db); } await _next(context); }
public IHttpActionResult DeleteProduct(int id) { using (var db = new SportsStoreDbContext()) { var p = db.Products.Find(id); if (p == null) { return(NotFound()); } db.Products.Remove(p); db.SaveChanges(); } return(Ok()); }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger <Startup> logger) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseStaticFiles(); app.UseSpaStaticFiles(); app.UseSwagger(); app.UseSwaggerUI(cfg => { cfg.SwaggerEndpoint("/swagger/v1/swagger.json", "SportsStore v1"); }); using (var scope = app.ApplicationServices.CreateScope()) { SportsStoreDbContext context = scope.ServiceProvider.GetRequiredService <SportsStoreDbContext>();// will get the SportsStoreDbContext object var createDatabase = context.Database.EnsureCreated(); if (createDatabase) { SportsStoreSeedData.PopulateSportsStore(context); logger.LogInformation($"***SportsStoreSeedData Called, '{context.Products.Count()}' - Products Added\n'{context.Orders.Count()}' - Orders Added\n'{context.OrderDetails.Count()}' - OrderDetails Added***"); } } //setup the CORS app.UseCors(cfg => { cfg.WithOrigins("https://localhost:4200/") .AllowAnyHeader() .AllowAnyOrigin() .AllowAnyMethod(); }); app.UseRouting(); app.UseEndpoints(ConfigureRoutes); app.UseSpa(cfg => { cfg.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { cfg.UseAngularCliServer(npmScript: "start"); } }); }
public BrowsingAppService(SportsStoreDbContext dbContext) { _DbContext = dbContext; }
private async Task SeedProducts(SportsStoreDbContext db) { Product[] products = { new Product { Name = "Kayak", Description = "A boat for one person", Category = "Watersports", Price = 275 }, new Product { Name = "Lifejacket", Description = "Protective and fashionable", Category = "Watersports", Price = 48.95m }, new Product { Name = "Soccer Ball", Description = "FIFA-approved size and weight", Category = "Soccer", Price = 19.50m }, new Product { Name = "Corner Flags", Description = "Give your playing field a professional touch", Category = "Soccer", Price = 34.95m }, new Product { Name = "Stadium", Description = "Flat-packed 35,000-seat stadium", Category = "Soccer", Price = 79500 }, new Product { Name = "Thinking Cap", Description = "Improve brain efficiency by 75%", Category = "Chess", Price = 16 }, new Product { Name = "Unsteady Chair", Description = "Secretly give your opponent a disadvantage", Category = "Chess", Price = 29.95m }, new Product { Name = "Human Chess Board", Description = "A fun game for the family", Category = "Chess", Price = 75 }, new Product { Name = "Bling-Bling King", Description = "Gold-plated, diamond-studded King", Category = "Chess", Price = 1200 } }; await db.Products.AddRangeAsync(products); db.SaveChanges(); }
public ProductService(SportsStoreDbContext db) { _db = db; }
public EFProductRepository(SportsStoreDbContext sportsStoreDbContext, ILogger <EFProductRepository> logger) { _context = sportsStoreDbContext; _logger = logger; }
public OrderService(SportsStoreDbContext db) { _db = db; }
public CheckOutAppService(SportsStoreDbContext dbContext) { _DbContext = dbContext; }
public CategoriesAndShoppingCartViewComponent(SportsStoreDbContext dbContext) { _dbcontex = dbContext; }
public EFProductRepository(SportsStoreDbContext dbContext) { _dbContext = dbContext; }
public EFOrderRepository(SportsStoreDbContext sportsStoreDbContext) { _context = sportsStoreDbContext; }