public string Create(int id, int quantity) { if (!(db.Products.Where(x => x.ProductID == id).Any()) || db.Products.Where(x => x.ProductID == id).First().Discontinued) { return("Error"); } try { if (!(db.ShopCart.Any(x => x.ProductID == id && x.UserName == User.Identity.Name))) { ShopCarts cart = new ShopCarts() { UserName = User.Identity.Name, ProductID = id, Quantity = quantity, Products = db.Products.Find(id) }; db.ShopCart.Add(cart); db.SaveChanges(); } else { Update(id, quantity); } return("{}"); } catch (Exception e) { //logger.Error(e.ToString()); return("Error"); } }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddDbContext <AppDbContext>(); services.AddTransient <IAllCars, CarRepository>(); services.AddTransient <ICarsCategory, CategoryRepository>(); services.AddTransient <IAllOrders, OrdersRepository>(); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddScoped(sp => ShopCarts.GetShopCart(sp)); services.AddMvc(); services.AddMemoryCache(); services.AddSession(); }
public ShopCartController(IAllCars carRepository, ShopCarts shopCart) { this._carRepository = carRepository; this._shopCart = shopCart; }
public OrderController(IAllOrders allOrders, ShopCarts shopCart) { this._allOrders = allOrders; this._shopCart = shopCart; }
public OrdersRepository(AppDbContext context, ShopCarts shopCarts) { this._context = context; this._shopCarts = shopCarts; }