public void RegisterUser() { using (var db = new WebstoreContext()) { Console.WriteLine("Type username: "******"Created new user with the ID: {0}", GetUserIdByName(newUsername)); var newUserQuery = from u in db.Users where newUsername == u.Username select u; foreach (var item in newUserQuery) { Console.WriteLine("New user created: {0}", item.Username); } } }
public void ApplyQuery(WebstoreContext db) { if (categoryId == 0) { products = db.Products.Where(product => product.Name.Contains(keyword) && product.ReleaseDate.CompareTo(DateTime.Now) <= 0).ToList(); } else { int[] prodIds = db.Catalogs.Where(catalog => catalog.CategoryId == categoryId).Select(catalog => catalog.ProductId).ToArray(); products = db.Products.Where(product => prodIds.Contains(product.Id) && product.Name.Contains(keyword) && product.ReleaseDate.CompareTo(DateTime.Now) <= 0).ToList(); } switch (orderby) { case "price": products.Sort((x, y) => order == "asc" ? x.Price.CompareTo(y.Price) : y.Price.CompareTo(x.Price)); break; case "date": products.Sort((x, y) => order == "asc" ? x.ReleaseDate.CompareTo(y.ReleaseDate) : y.ReleaseDate.CompareTo(x.ReleaseDate)); break; } products.ForEach(product => product.Images = db.Images.Where(image => image.ProductId == product.Id).OrderBy(image => image.Order).Take(1).ToList()); Initialize(); }
public void CreateProduct() { using (var db = new WebstoreContext()) { Console.WriteLine("Type product name"); var prodName = Console.ReadLine(); Console.WriteLine("Type product price"); var prodPrice = Double.Parse(Console.ReadLine()); var newProduct = new Product { Name = prodName, Price = prodPrice }; db.Products.Add(newProduct); db.SaveChanges(); Console.WriteLine("Created new product with the ID: {0}", GetProductIdByName(newProduct.Name)); var newProductQuery = from p in db.Products where prodName == p.Name select p; foreach (var product in newProductQuery) { Console.WriteLine("New product created: {0}", product.Name); } } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, WebstoreContext dbContext) { /*if (env.IsDevelopment()) * { * app.UseDeveloperExceptionPage(); * } * * app.UseRouting(); * * app.UseEndpoints(endpoints => * { * endpoints.MapGet("/", async context => * { * await context.Response.WriteAsync("Hello World!"); * }); * });*/ //可瀏覽靜態檔案 app.UseStaticFiles(); //使用session app.UseSession(); // 建立資料庫 dbContext.Database.EnsureCreated(); app.UseMvcWithDefaultRoute(); //route //app.UseMvc(); }
public void PrintAllUsers() { using (var db = new WebstoreContext()) { var users = from u in db.Users orderby u.Username select u; foreach (var user in users) { Console.WriteLine("User {0}: {1}", user.UserId, user.Username); } } }
public void PrintAllProducts() { using (var db = new WebstoreContext()) { var products = from p in db.Products orderby p.Name //where Ammount select p; foreach (var product in products) { Console.WriteLine("Product {0}: {1}", product.ProductId, product.Name); } } }
public int GetUserIdByName(string name) { using (var db = new WebstoreContext()) { var result = -1; var queryUserId = from u in db.Users where u.Username.Equals(name) select u.UserId; if (queryUserId.Count() == 1) { result = queryUserId.First(); } return(result); } }
public int GetProductIdByName(string name) { using (var db = new WebstoreContext()) { var result = -1; var queryProductId = from p in db.Products where p.Name.Equals(name) select p.ProductId; if (queryProductId.Count() == 1) { result = queryProductId.First(); } return(result); } }
public void Login() { using (var db = new WebstoreContext()) { Console.WriteLine("Enter username"); var name = Console.ReadLine(); Console.WriteLine("Enter password"); var pass = Console.ReadLine(); var user = (from u in db.Users where u.Username == name && u.Password == pass select u).Count(); Console.WriteLine(user.ToString()); Console.ReadKey(); var message = (user != 1) ? "Failed" : "Succeeded"; Console.WriteLine("Login attempt {0}", message); } }
public CartController(WebstoreContext db, IHttpContextAccessor http) { _db = db; _http = http; }
public Categories(WebstoreContext db) { categories = db.Categories.OrderBy(category => category.Id).ToList(); }
public ProductController(WebstoreContext db) { _db = db; }
public HomeController(WebstoreContext db) { _db = db; }