public IActionResult Edit(ProductViewModel productViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index", "Home"));
            }

            int         id          = productViewModel.Id;
            string      name        = productViewModel.Name;
            string      description = productViewModel.Description;
            ProductType type        = Enum.Parse <ProductType>(productViewModel.Type);
            decimal     price       = productViewModel.Price;

            using (dbContext)
            {
                Product product = dbContext.Products.Find(id);
                product.Name        = name;
                product.Description = description;
                product.Type        = type;
                product.Price       = price;

                dbContext.SaveChanges();
            }

            return(RedirectToAction("Index", "Home"));
        }
예제 #2
0
        public static void Main()
        {
            var db = new ChushkaDbContext();

            using (db)
            {
                Console.WriteLine("Initialize/Update database...");
                db.Database.Migrate();

                if (!db.Roles.Any())
                {
                    Console.WriteLine("Seeding roles...");

                    db.Roles.Add(new Role()
                    {
                        Name = "Admin"
                    });
                    db.Roles.Add(new Role()
                    {
                        Name = "User"
                    });

                    db.SaveChanges();
                }
                if (!db.ProductTypes.Any())
                {
                    Console.WriteLine("Seeding ProductTypes...");

                    db.ProductTypes.Add(new ProductType()
                    {
                        Name = "Food"
                    });
                    db.ProductTypes.Add(new ProductType()
                    {
                        Name = "Domestic"
                    });
                    db.ProductTypes.Add(new ProductType()
                    {
                        Name = "Health"
                    });
                    db.ProductTypes.Add(new ProductType()
                    {
                        Name = "Cosmetic"
                    });
                    db.ProductTypes.Add(new ProductType()
                    {
                        Name = "Other"
                    });

                    db.SaveChanges();
                }
                Console.WriteLine("Database ready!");
            }
            var server = new WebServer(42420, new ControllerRouter(), new ResourceRouter());

            MvcEngine.Run(server);
        }
예제 #3
0
        public void AddProduct(string name, decimal price, string description, string type)
        {
            Product product = new Product()
            {
                Name        = name,
                Description = description,
                Price       = price,
                Type        = Enum.Parse <ProductType>(type)
            };

            context.Products.Add(product);
            context.SaveChanges();
        }
예제 #4
0
        public void AddProduct(string name, decimal price, string description, ProductType type)
        {
            Product product = new Product()
            {
                Name        = name.Trim(),
                Description = description?.Trim(),
                Price       = price,
                Type        = type
            };

            context.Products.Add(product);
            context.SaveChanges();
        }
예제 #5
0
        public void Create(string name, decimal price, string description, ProductType type)
        {
            var product = new Product()
            {
                Id          = Guid.NewGuid().ToString(),
                Name        = name,
                Price       = price,
                Description = description,
                Type        = type
            };

            chushkaDbContext.Products.Add(product);
            chushkaDbContext.SaveChanges();
        }
        public IActionResult Edit(int id, ProductsDetailsViewModel model)
        {
            var product = chushkaDbContext.Products.FirstOrDefault(p => p.Id == id);

            product.Name        = model.Name;
            product.Description = model.Description;
            product.Price       = model.Price;
            ProductType type;

            Enum.TryParse(model.Type, out type);
            product.Type = type;
            chushkaDbContext.Products.Update(product);
            chushkaDbContext.SaveChanges();
            return(this.RedirectToAction("Index", "Home"));
        }
예제 #7
0
        public void AddProduct(ProductViewModel model)
        {
            if (Enum.TryParse(model.SelectedFoodType, out ProductType productType))
            {
                Product product = new Product
                {
                    Name        = model.Name,
                    Description = model.Description,
                    Price       = model.Price,
                    Type        = productType
                };

                _db.Products.Add(product);
                _db.SaveChanges();
            }
        }
        public IActionResult Create(ProductInputModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.View());
            }

            var product = new Product
            {
                Name        = model.Name,
                Price       = model.Price,
                Description = model.Description,
                Type        = (ProductType)model.ProductType
            };

            dbContext.Products.Add(product);
            try
            {
                dbContext.SaveChanges();
                return(this.RedirectToAction("Details", new { Id = product.Id }));
            }
            catch (Exception e)
            {
                return(this.BadRequest(e.Message));
            }
        }
예제 #9
0
        public void AddUser(ChushkaUser user)
        {
            if (GetUser(user.UserName) == null)
            {
                user.CustomRole = !IsAnyUserInContext() ? CustomRole.Admin : CustomRole.User;

                context.Users.Add(user);
                context.SaveChanges();
            }
        }
예제 #10
0
        public void Edit(EditProductViewModel model)
        {
            var product = this.context.Products.SingleOrDefault(p => p.Id == model.Id);

            product.Name        = model.Name;
            product.Price       = decimal.Parse(model.Price);
            product.Description = model.Description;
            product.Type        = Enum.Parse <ProductType>(model.ProductType, true);

            context.SaveChanges();
        }
예제 #11
0
        public void CreateOrder(string userId, int productId)
        {
            Order order = new Order
            {
                ProductId = productId,
                ClientId  = userId,
                OrderOn   = DateTime.Now
            };

            _db.Orders.Add(order);
            _db.SaveChanges();
        }
예제 #12
0
        public IActionResult Register(RegisterModel model)
        {
            if (this.User.IsAuthenticated)
            {
                return(this.RedirectToHomePage());
            }

            if (!this.IsValidModel(model))
            {
                this.ShowError(this.GetValidationSummary());
                return(this.View());
            }

            Role role;
            User user;

            using (var db = new ChushkaDbContext())
            {
                role = db.Roles
                       .Where(r => r.Name == DefaultRole)
                       .FirstOrDefault();

                if (role == null)
                {
                    role = new Role()
                    {
                        Name = DefaultRole
                    };
                    db.Roles.Add(role);
                }

                user = new User()
                {
                    Username     = model.Username,
                    PasswordHash = PasswordUtilities.GetPasswordHash(model.Password),
                    FullName     = model.FullName,
                    Email        = model.Email,
                    Role         = role
                };

                db.Users.Add(user);
                db.SaveChanges();
            }

            var roles = new List <string>()
            {
                role.Name
            };

            this.SignIn(user.Username, user.Id, roles);
            return(this.RedirectToHomePage());
        }
        public void CreateOrder(int productId, string clientName)
        {
            Product product = context.Products.Find(productId);
            User    client  = context.Users
                              .SingleOrDefault(u => u.Username == clientName);
            Order order = new Order()
            {
                Product   = product,
                Client    = client,
                OrderedOn = DateTime.UtcNow
            };

            context.Orders.Add(order);
            context.SaveChanges();
        }
예제 #14
0
        public IActionResult Create(CreateProductModel model)
        {
            if (!this.IsAdmin)
            {
                return(this.RedirectToHomePage());
            }

            if (!this.IsValidModel(model))
            {
                this.ShowError(this.GetValidationSummary());
                return(this.View());
            }

            ProductType productType;
            Product     product;

            using (var db = new ChushkaDbContext())
            {
                productType = db
                              .ProductTypes
                              .FirstOrDefault(pt => pt.Name == model.ProductType);

                if (productType == null)
                {
                    productType = new ProductType()
                    {
                        Name = model.ProductType
                    };
                    db.ProductTypes.Add(productType);
                }

                product = new Product()
                {
                    Name        = model.Name,
                    Price       = model.Price,
                    Description = model.Description,
                    Type        = productType
                };

                db.Products.Add(product);
                db.SaveChanges();
            }

            return(this.RedirectToHomePage());
        }
예제 #15
0
        private static void SeedDatabase(ChushkaDbContext db)
        {
            var roles = new List <Role>()
            {
                new Role()
                {
                    Name = "User"
                },
                new Role()
                {
                    Name = "Admin"
                }
            };

            var productTypes = new List <ProductType>()
            {
                new ProductType()
                {
                    Name = "Food"
                },
                new ProductType()
                {
                    Name = "Domestic"
                },
                new ProductType()
                {
                    Name = "Health"
                },
                new ProductType()
                {
                    Name = "Cosmetic"
                },
                new ProductType()
                {
                    Name = "Other"
                },
            };

            db.Roles.AddRange(roles);
            db.ProductTypes.AddRange(productTypes);
            db.SaveChanges();

            Console.WriteLine("Database Seeded!");
        }
예제 #16
0
        public IActionResult Edit(EditProductModel model)
        {
            if (!IsAdmin)
            {
                return(this.RedirectToHomePage());
            }

            if (!this.IsValidModel(model))
            {
                this.ShowError(this.GetValidationSummary());
                this.ViewData["productId"]   = model.Id.ToString();
                this.ViewData["name"]        = model.Name;
                this.ViewData["price"]       = model.Price.ToString("0.##");
                this.ViewData["description"] = model.Description;
                return(this.View());
            }

            using (var db = new ChushkaDbContext())
            {
                var productType = db
                                  .ProductTypes
                                  .FirstOrDefault(pt => pt.Name == model.ProductType);

                if (productType == null)
                {
                    productType = new ProductType()
                    {
                        Name = model.ProductType
                    };
                    db.ProductTypes.Add(productType);
                }

                var product = db.Products.FirstOrDefault(p => p.Id == model.Id);
                product.Name        = model.Name;
                product.Price       = model.Price;
                product.Description = model.Description;
                product.Type        = productType;

                db.SaveChanges();
            }

            return(this.RedirectToHomePage());
        }
예제 #17
0
        public void AddUser(string username, string password, string email, string fullName)
        {
            UserRole role = UserRole.User;

            if (!context.Users.Any())
            {
                role = UserRole.Admin;
            }
            User user = new User()
            {
                Username = username,
                Password = password,
                Email    = email,
                FullName = fullName,
                Role     = role
            };

            context.Users.Add(user);
            context.SaveChanges();
        }
예제 #18
0
        public IActionResult Order(int id)
        {
            if (!this.User.IsAuthenticated)
            {
                return(this.RedirectToHomePage());
            }

            using (var db = new ChushkaDbContext())
            {
                db.Orders.Add(new Order()
                {
                    ClientId  = this.User.Id,
                    ProductId = id,
                    OrderedOn = DateTime.Now
                });

                db.SaveChanges();
            }

            return(this.IsAdmin ? this.RedirectToAction("/orders/all") : this.RedirectToHomePage());
        }
예제 #19
0
        public void AddUser(string username, string password, string fullName, string email)
        {
            using (var db = new ChushkaDbContext())
            {
                var user = new User
                {
                    Username = username,
                    Password = password,
                    FullName = fullName,
                    Email    = email,
                    Role     = Role.User
                };

                if (!db.Users.Any())
                {
                    user.Role = Role.Admin;
                }

                db.Users.Add(user);
                db.SaveChanges();
            }
        }
예제 #20
0
        public IActionResult Delete(int id)
        {
            if (!IsAdmin)
            {
                return(this.RedirectToHomePage());
            }

            Product product;

            using (var db = new ChushkaDbContext())
            {
                product = db.Products.Find(id);

                if (product == null)
                {
                    return(this.RedirectToHomePage());
                }

                db.Products.Remove(product);
                db.SaveChanges();
            }

            return(this.RedirectToHomePage());
        }