using System; using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.Mvc; using InstaVibe.Models; namespace InstaVibe.Controllers { public class SelfProfileController : Controller { public string connectionString = ConfigurationManager.ConnectionStrings["InstaVibe"].ConnectionString.ToString(); // ---------------- MAIN PROFILE PAGE ---------------- public ActionResult Index() { if (Session["UserId"] == null) { // User not logged in — redirect to login page return RedirectToAction("Login", "Account"); } int userId = Convert.ToInt32(Session["UserId"]); // ✅ Session pe bharosa nahi — directly DB se role lo string userRole = GetUserRoleFromDB(userId); if (userRole == "Alumni") return RedirectToAction("Index", "AlumniProfile"); SelfProfileModel model = new SelfProfileModel(); model.FollowersCount = GetFollowersCount(userId); model.FollowingCount = GetFollowingCount(userId); using (SqlConnection con = new SqlConnection(connectionString)) { con.Open(); // Get user profile info string userQuery = "SELECT profile_picture, department, fullname FROM users WHERE user_id = @UserId"; using (SqlCommand userCmd = new SqlCommand(userQuery, con)) { userCmd.Parameters.AddWithValue("@UserId", userId); using (SqlDataReader reader = userCmd.ExecuteReader()) { if (reader.Read()) { model.ProfileImage = reader["profile_picture"].ToString(); model.Department = reader["department"].ToString(); model.FullName = reader["fullname"].ToString(); } } } // Get user posts model.Posts = new List(); string postsQuery = @" SELECT p.media_url, p.caption, p.created_at, u.username, u.profile_picture FROM Posts p INNER JOIN users u ON p.user_id = u.user_id WHERE p.user_id = @UserId ORDER BY p.created_at DESC"; using (SqlCommand postCmd = new SqlCommand(postsQuery, con)) { postCmd.Parameters.AddWithValue("@UserId", userId); using (SqlDataReader reader = postCmd.ExecuteReader()) { while (reader.Read()) { PostModel post = new PostModel() { MediaUrl = reader["media_url"].ToString(), Caption = reader["caption"].ToString(), PostDate = Convert.ToDateTime(reader["created_at"]), Username = reader["username"].ToString(), ProfileImage = reader["profile_picture"].ToString() }; model.Posts.Add(post); } } } model.PostsCount = model.Posts.Count; } model.IsCurrentUser = true; return View(model); } // ✅ Yeh private method add karo SelfProfileController mein private string GetUserRoleFromDB(int userId) { using (SqlConnection con = new SqlConnection(connectionString)) { con.Open(); SqlCommand cmd = new SqlCommand( "SELECT user_type FROM users WHERE user_id = @Id", con); // role → user_type cmd.Parameters.AddWithValue("@Id", userId); object result = cmd.ExecuteScalar(); return result?.ToString() ?? "Student"; } } // ---------------- FOLLOWERS COUNT ---------------- private int GetFollowersCount(int userId) { using (SqlConnection con = new SqlConnection(connectionString)) { string query = "SELECT COUNT(*) FROM followers WHERE following_user_id = @UserId"; SqlCommand cmd = new SqlCommand(query, con); cmd.Parameters.AddWithValue("@UserId", userId); con.Open(); return (int)cmd.ExecuteScalar(); } } // ---------------- FOLLOWING COUNT ---------------- private int GetFollowingCount(int userId) { using (SqlConnection con = new SqlConnection(connectionString)) { string query = "SELECT COUNT(*) FROM followers WHERE follower_user_id = @UserId"; SqlCommand cmd = new SqlCommand(query, con); cmd.Parameters.AddWithValue("@UserId", userId); con.Open(); return (int)cmd.ExecuteScalar(); } } // ---------------- FOLLOWERS & FOLLOWING LIST PARTIAL VIEWS ---------------- public ActionResult GetFollowers() { int userId = Convert.ToInt32(Session["UserId"]); List followers = DatabaseHelper.GetFollowers(userId); return PartialView("~/Views/Shared/_FollowListPartial.cshtml", followers); } public ActionResult GetFollowing() { int userId = Convert.ToInt32(Session["UserId"]); List following = DatabaseHelper.GetFollowing(userId); return PartialView("~/Views/Shared/_FollowListPartial.cshtml", following); } } }