using System; using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.Mvc; namespace InstaVibe.Controllers { public class ThemeController : Controller { string conStr = ConfigurationManager.ConnectionStrings["InstaVibe"].ConnectionString; [HttpPost] public ActionResult SaveUserTheme(string fontSize, int? primaryHue) { int userId = Convert.ToInt32(Session["UserId"]); using (SqlConnection con = new SqlConnection(conStr)) { con.Open(); var cmd = new SqlCommand(@" UPDATE Users SET FontSize = CASE WHEN @FontSize IS NOT NULL AND LTRIM(RTRIM(@FontSize)) <> '' THEN @FontSize ELSE FontSize END, PrimaryHue = ISNULL(@PrimaryHue, PrimaryHue) WHERE user_id = @UserId", con); cmd.Parameters.AddWithValue("@FontSize", (object)fontSize ?? DBNull.Value); cmd.Parameters.AddWithValue("@PrimaryHue", (object)primaryHue ?? DBNull.Value); cmd.Parameters.AddWithValue("@UserId", userId); cmd.ExecuteNonQuery(); } return Json(new { success = true }); } [HttpGet] public ActionResult GetUserTheme() { int userId = Convert.ToInt32(Session["UserId"]); string fontSize = null; int? primaryHue = null; using (SqlConnection con = new SqlConnection(conStr)) { con.Open(); var cmd = new SqlCommand("SELECT FontSize, PrimaryHue FROM Users WHERE user_id = @UserId", con); cmd.Parameters.AddWithValue("@UserId", userId); using (var reader = cmd.ExecuteReader()) { if (reader.Read()) { fontSize = reader["FontSize"] as string; primaryHue = reader["PrimaryHue"] != DBNull.Value ? Convert.ToInt32(reader["PrimaryHue"]) : (int?)null; } } } return Json(new { fontSize = fontSize, primaryHue = primaryHue }, JsonRequestBehavior.AllowGet); } } }