public IHttpResponse RegisterPost(IHttpRequest request)
        {
            var userName        = request.FormData["username"].ToString().Trim();
            var password        = request.FormData["password"].ToString();
            var confirmPassword = request.FormData["confirmPassword"].ToString();
            var email           = HtmlDecoder.Decode(request.FormData["email"].ToString());


            if (string.IsNullOrWhiteSpace(userName) || userName.Length < 3)
            {
                return(this.BadRequestError("Username should be more than 2 characters"));
            }

            if (this.DbContext.Users.Any(x => x.Username == userName))
            {
                return(this.BadRequestError("This username already exists in the database!"));
            }

            if (string.IsNullOrWhiteSpace(password) || password.Length < 6)
            {
                return(this.BadRequestError("The password should be at least 6 characters!"));
            }

            if (password != confirmPassword)
            {
                return(this.BadRequestError("Password and Confirm password fields do not match!"));
            }

            //Hashing password
            string hashedPassword = this.passwordHasher.HashPassword(password);

            var user = new User
            {
                Username         = userName,
                Password         = hashedPassword,
                Email            = email,
                RegistrationDate = DateTime.UtcNow,
            };

            //Save data in the DB
            try
            {
                this.DbContext.Users.Add(user);
                this.DbContext.SaveChanges();
            }
            catch (Exception e)
            {
                return(this.ServerError(e.Message));
            }

            //Redirect to home page
            return(new RedirectResult("/"));
        }
        public IHttpResponse SetNewPasswordPost(IHttpRequest request)
        {
            var email = HtmlDecoder.Decode(request.FormData["email"].ToString());

            if (!this.DbContext.Users.Any(x => x.Email == email))
            {
                return(this.View("InvalidEmail"));
            }

            return(new RedirectResult("/"));
            //TODO rendom password generator for generating temp password
            //TODO find a way to sent the temp password to the user email
        }
        public IHttpResponse EditUsersDetailsPost(IHttpRequest request)
        {
            if (!this.IsAuthenticated(request))
            {
                return(new RedirectResult("/users/login"));
            }

            var username = request.Session.GetParameter("username").ToString();
            var user     = this.DbContext.Users.FirstOrDefault(x => x.Username == username);

            this.ViewBag["username"] = user.Username;

            var updatedEmail = HtmlDecoder.Decode(request.FormData["email"].ToString());

            if (this.DbContext.Users.Any(x => x.Email == updatedEmail))
            {
                this.ViewBag["email"]  = user.Email;
                this.ViewBag["allert"] = ExistingEmailAllert;
                return(this.View("EditProfile"));
            }

            user.Email = updatedEmail;

            try
            {
                this.DbContext.Users.Update(user);
                this.DbContext.SaveChanges();
            }
            catch (Exception e)
            {
                return(this.ServerError(e.Message));
            }

            this.ViewBag["email"]  = user.Email;
            this.ViewBag["allert"] = SuccessfullyUpdatedEmailAllert;
            return(this.View("EditProfile"));
        }