using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.Mvc; using InstaVibe.Models; namespace InstaVibe.Controllers { public class PublicProfileController : Controller { public string connectionString = ConfigurationManager.ConnectionStrings["InstaVibe"].ConnectionString.ToString(); // ====================== Public Profile Page (Index) ====================== public ActionResult Index(int id) { if (Session["UserId"] == null) { return RedirectToAction("Login", "Account"); } int loggedInUserId = Convert.ToInt32(Session["UserId"]); // ✅ Viewed user ka role DB se check karo string viewedUserType = GetUserRole(id); if (viewedUserType == "Alumni") return RedirectToAction("Public", "AlumniProfile", new { id = id }); SelfProfileModel model = new SelfProfileModel(); using (SqlConnection con = new SqlConnection(connectionString)) { con.Open(); // User info jisko profile dekhni hai string userQuery = "SELECT profile_picture, department, fullname FROM users WHERE user_id = @UserId"; using (SqlCommand userCmd = new SqlCommand(userQuery, con)) { userCmd.Parameters.AddWithValue("@UserId", id); using (SqlDataReader reader = userCmd.ExecuteReader()) { if (reader.Read()) { model.UserId = id; model.ProfileImage = reader["profile_picture"].ToString(); model.Department = reader["department"].ToString(); model.FullName = reader["fullname"].ToString(); } } } // Posts of the user being viewed 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", id); 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; // Follower/Following counts model.FollowersCount = GetFollowersCount(id, con); model.FollowingCount = GetFollowingCount(id, con); // Relationship checks model.IsFollowing = DatabaseHelper.IsFollowing(loggedInUserId, id); model.HasSentRequest = HasSentFriendRequest(loggedInUserId, id, con); model.IsCurrentUser = (id == loggedInUserId); } return View("Index", model); } private string GetUserRole(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"; } } private int GetFollowersCount(int userId, SqlConnection con) { string query = "SELECT COUNT(*) FROM followers WHERE following_user_id = @UserId"; using (SqlCommand cmd = new SqlCommand(query, con)) { cmd.Parameters.AddWithValue("@UserId", userId); return (int)cmd.ExecuteScalar(); } } private int GetFollowingCount(int userId, SqlConnection con) { string query = "SELECT COUNT(*) FROM followers WHERE follower_user_id = @UserId"; using (SqlCommand cmd = new SqlCommand(query, con)) { cmd.Parameters.AddWithValue("@UserId", userId); return (int)cmd.ExecuteScalar(); } } private bool HasSentFriendRequest(int fromUserId, int toUserId, SqlConnection con) { string query = @" SELECT COUNT(*) FROM FriendRequests WHERE from_user_id = @FromUserId AND to_user_id = @ToUserId AND status = 'Pending'"; using (SqlCommand cmd = new SqlCommand(query, con)) { cmd.Parameters.AddWithValue("@FromUserId", fromUserId); cmd.Parameters.AddWithValue("@ToUserId", toUserId); return (int)cmd.ExecuteScalar() > 0; } } // Followers popup partial public ActionResult GetFollowers(int userId) { var followers = DatabaseHelper.GetFollowers(userId); ViewBag.Type = "followers"; return PartialView("_FollowListPartial", followers); } // Following popup partial public ActionResult GetFollowing(int userId) { var following = DatabaseHelper.GetFollowing(userId); ViewBag.Type = "following"; return PartialView("_FollowListPartial", following); } // ====================== Pending Friend Requests ====================== public JsonResult GetPendingRequests() { int userId = Convert.ToInt32(Session["UserId"]); using (SqlConnection con = new SqlConnection(connectionString)) { con.Open(); string query = @" SELECT fr.request_id, u.username, u.profile_picture FROM FriendRequests fr INNER JOIN Users u ON u.user_id = fr.from_user_id WHERE fr.to_user_id = @UserId AND fr.status = 'Pending'"; SqlCommand cmd = new SqlCommand(query, con); cmd.Parameters.AddWithValue("@UserId", userId); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); var requests = dt.AsEnumerable().Select(row => new { RequestId = row["request_id"], Username = row["username"].ToString(), ProfilePic = row["profile_picture"].ToString() }); return Json(requests, JsonRequestBehavior.AllowGet); } } } }