public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                User user = new User {
                    Email = model.Email, UserName = model.User_Name
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, false);

                    using (IDbConnection connection = new MySqlConnection(connectionString))
                    {
                        AccountControllerHelperMethods.CreateInstagramUser(connection, user.Id, model);
                    }

                    return(RedirectToAction("UserAccount", "Account"));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
            }
            return(View(model));
        }
        /// <summary>
        /// Check if current user if following the author of the given post and pass this info into the view.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="postId"></param>
        /// <returns></returns>
        public IActionResult GetAuthorGeneralInfo(string id, string postId)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("SignIn", "Account"));
            }

            UserViewModel user;

            using (IDbConnection connection = new MySqlConnection(connectionString))
            {
                user = AccountControllerHelperMethods.GetUser(id, connection);

                if (User.FindFirstValue(ClaimTypes.NameIdentifier) != id)
                {
                    bool following = AccountControllerHelperMethods.IsCurrentUserFollowing(connection, User.FindFirstValue(ClaimTypes.NameIdentifier), id);

                    user.Current_User_Following = following;
                }
                else
                {
                    user.Current_User_Following = false;
                }
            }

            ViewBag.PostId = postId;
            ViewBag.UserId = user.Id;

            return(PartialView(user));
        }
        /// <summary>
        /// Return the user account.
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult UserAccount(string Id)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("SignIn", "Account"));
            }


            using (IDbConnection connection = new MySqlConnection(connectionString))
            {
                if (Id == "null" || Id == null)
                {
                    Id = User.FindFirstValue(ClaimTypes.NameIdentifier);
                }

                UserViewModel  currUser  = AccountControllerHelperMethods.GetUser(Id, connection);
                UserStatsModel userStats = AccountControllerHelperMethods.GetUserStats(Id, connection);

                if (currUser == null)
                {
                    return(RedirectToAction("Error", "Home"));
                }

                posts = AccountControllerHelperMethods.GetAllUserPosts(Id, connection);

                ViewBag.CurrUser  = currUser;
                ViewBag.UserStats = userStats;
                ViewBag.Posts     = posts;
            }

            return(View(new ChangeProfileViewModel()));
        }
        public IActionResult ChangeProfile(ChangeProfileViewModel model)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("SignIn", "Account"));
            }

            if (ModelState.IsValid)
            {
                using (IDbConnection connection = new MySqlConnection(connectionString))
                {
                    byte[] imageData = null;

                    if (model.ProfilePicture != null)
                    {
                        using (var binaryReader = new BinaryReader(model.ProfilePicture.OpenReadStream()))
                        {
                            imageData = binaryReader.ReadBytes((int)model.ProfilePicture.Length);
                        }
                    }

                    AccountControllerHelperMethods.ChangeProfile(connection, model, User.FindFirstValue(ClaimTypes.NameIdentifier), imageData);
                }
            }
            return(RedirectToAction("UserAccount"));
        }
        public IActionResult AddPost(string caption, IFormFile img)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("SignIn", "Account"));
            }

            byte[] imageData = null;

            using (var binaryReader = new BinaryReader(img.OpenReadStream()))
            {
                imageData = binaryReader.ReadBytes((int)img.Length);
            }

            using (IDbConnection connection = new MySqlConnection(connectionString))
            {
                long postId = AccountControllerHelperMethods.AddPost(connection, User.FindFirstValue(ClaimTypes.NameIdentifier), imageData, caption);

                posts.Add(new PostViewModel()
                {
                    Id = postId, IUser_Id = User.FindFirstValue(ClaimTypes.NameIdentifier), Picture = imageData
                });
            }

            return(RedirectToAction("UserAccount", "Account", new { id = "null" }));
        }
        public IActionResult GetFollowers(string id)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("SignIn", "Account"));
            }

            List <UserViewModel> followers = new List <UserViewModel>();

            using (IDbConnection connection = new MySqlConnection(connectionString))
            {
                followers = AccountControllerHelperMethods.GetAllFollowers(connection, id);

                for (int i = 0; i < followers.Count; i++)
                {
                    bool isFollowing = AccountControllerHelperMethods.IsCurrentUserFollowing(connection, User.FindFirstValue(ClaimTypes.NameIdentifier), followers[i].Id);

                    followers[i].Current_User_Following = isFollowing;
                }
            }

            ViewBag.CurrUserId    = User.FindFirstValue(ClaimTypes.NameIdentifier);
            ViewBag.UserProfileId = id;

            return(PartialView(followers));
        }
        /// <summary>
        /// Get all users.
        /// </summary>
        /// <returns></returns>
        public IActionResult GetAllUsers()
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("SignIn", "Account"));
            }

            List <UserViewModel> users;

            using (IDbConnection connection = new MySqlConnection(connectionString))
            {
                users = AccountControllerHelperMethods.GetAllUsers(User.FindFirstValue(ClaimTypes.NameIdentifier), connection);

                foreach (var user in users)
                {
                    bool isFollowing = AccountControllerHelperMethods.IsCurrentUserFollowing(connection, User.FindFirstValue(ClaimTypes.NameIdentifier), user.Id);
                    user.Current_User_Following = isFollowing;
                }
            }

            return(View(users));
        }