Пример #1
0
        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);
                }
            }
        }
Пример #2
0
        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();
        }
Пример #3
0
        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);
                }
            }
        }
Пример #4
0
        // 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();
        }
Пример #5
0
 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);
         }
     }
 }
Пример #6
0
 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);
         }
     }
 }
Пример #7
0
 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);
     }
 }
Пример #8
0
 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);
     }
 }
Пример #9
0
        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);
            }
        }
Пример #10
0
 public CartController(WebstoreContext db, IHttpContextAccessor http)
 {
     _db   = db;
     _http = http;
 }
Пример #11
0
 public Categories(WebstoreContext db)
 {
     categories = db.Categories.OrderBy(category => category.Id).ToList();
 }
Пример #12
0
 public ProductController(WebstoreContext db)
 {
     _db = db;
 }
Пример #13
0
 public HomeController(WebstoreContext db)
 {
     _db = db;
 }