Example #1
0
        public IActionResult Login(LoginUserBindingModel model)
        {
            if (!this.IsValidModel(model))
            {
                this.Model["message"] = GetErrors();

                return(this.View());
            }

            using (var db = new KittenWebDbContext())
            {
                string passwordHash = PasswordUtilities.GetPasswordHash(model.Password);

                User user = db.Users.FirstOrDefault(u => u.Username == model.Username &&
                                                    u.PasswordHash == passwordHash);

                if (user == null)
                {
                    this.Model["message"] = "<p>Invalid user or password</p>";

                    return(this.View());
                }

                this.SignIn(user.Username, user.Id);
            }

            return(new RedirectResult("/home/welcome"));
        }
Example #2
0
        public IActionResult All()
        {
            if (!this.User.IsAuthenticated)
            {
                return(new RedirectResult("/home/index"));
            }

            StringBuilder result = new StringBuilder();

            using (var db = new KittenWebDbContext())
            {
                var kittens = db.Kittens
                              .Select(k => new{ k.Name, k.Breed, k.Birthdate, k.Breed.ImageUrl })
                              .ToList();

                var kittenViewTemplate = File.ReadAllText(Paths.PartialViewsPath + "KittenView.html");

                foreach (var kitten in kittens)
                {
                    var kittenView = String.Format(kittenViewTemplate, kitten.ImageUrl, kitten.Name, kitten.Birthdate, kitten.Breed);

                    result.AppendLine(kittenView);
                }

                this.Model["allKittens"] = result.ToString();
            }

            return(this.View());
        }
Example #3
0
        public IActionResult Register(RegisterUserBindingModel model)
        {
            if (!this.IsValidModel(model))
            {
                this.Model["message"] = this.GetErrors();

                return(this.View());
            }

            using (var db = new KittenWebDbContext())
            {
                string passwordHash = PasswordUtilities.GetPasswordHash(model.Password);

                User user = new User()
                {
                    Username     = model.Username,
                    PasswordHash = passwordHash,
                    Email        = model.Email
                };

                db.Users.Add(user);

                db.SaveChanges();

                this.Model["message"] = "<p>Successful registration!</p>";
            }

            return(this.View());
        }
Example #4
0
        public static void Main()
        {
            int port = 55000;

            var controllerRouter = new ControllerRouter();

            var resourceRouter = new ResourceRouter();

            var webServer = new WebServer(port, controllerRouter, resourceRouter);

            var dbContext = new KittenWebDbContext();

            MvcEngine.Run(webServer, dbContext);
        }
Example #5
0
        public IActionResult Add(AddKittenBindingModel model)
        {
            if (!this.User.IsAuthenticated)
            {
                return(new RedirectResult("/home/index"));
            }

            if (!this.IsValidModel(model))
            {
                this.Model["message"] = this.GetErrors();

                return(this.View());
            }

            using (var db = new KittenWebDbContext())
            {
                var breed = db.Breeds.FirstOrDefault(b => b.Type == model.BreedType);

                if (breed == null)
                {
                    this.Model["message"] = "<p>Unsupported breed!</p>";

                    return(this.View());
                }

                DateTime birthdate = DateTime.Now;

                if (model.Birthdate == null || !DateTime.TryParse(model.Birthdate, out birthdate))
                {
                    this.Model["message"] = "<p>Unsupported date format!</p>";

                    return(this.View());
                }

                Kitten kitten = new Kitten()
                {
                    Name      = model.Name,
                    BreedId   = breed.Id,
                    Birthdate = birthdate
                };

                db.Kittens.Add(kitten);

                db.SaveChanges();
            }

            return(this.View());
        }