using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; using ODCWeb.DataAccess.Data; using ODCWeb.Areas.Staff.Controllers; using ODCWeb.Models; using System; using MimeKit; using ODCWeb.DataAccess.Repository.IRepository; namespace ODCWeb.Areas.Admin.Controllers { [Area("Admin")] public class HomeController : Controller { private readonly UserManager userManager; private readonly ApplicationDbContext _db; private readonly IUnitOfWork _unitOfWork; private readonly SignInManager signInManager; public HomeController(UserManager userManager, ApplicationDbContext dbContext, SignInManager signInManager, IUnitOfWork unitOfWork) { this.userManager = userManager; _db = dbContext; _unitOfWork = unitOfWork; this.signInManager = signInManager; } public IActionResult LoginTransfer() { if (signInManager.IsSignedIn(User)) { /* TempData["success"] = "Loading! Please Wait";*/ return RedirectToAction("Index", "Home"); } else { /* TempData["success"] = "Loading! Please Wait";*/ return LocalRedirect("/Identity/Account/Login"); } } public IActionResult RemoveRemindersAll() { IEnumerable tasks = _unitOfWork.ScheduleTask.GetAll(); if (tasks != null) { foreach (var task in tasks) { _unitOfWork.ScheduleTask.Remove(task); _unitOfWork.save(); } } return RedirectToAction("Index", "Home"); } public IActionResult Index() { try { // Get all users from database var applicationUsers = _db.ApplicationUsers.ToList(); // Check if we have any users to process if (applicationUsers != null && applicationUsers.Any()) { foreach (ApplicationUser person in applicationUsers) { person.SubscriptionDaysLeft = ((int)(person.ExpiryDate - DateTime.Today).TotalDays); if (person.SubscriptionDaysLeft < 0) { person.SubscriptionStatus = false; } _db.Update(person); } _db.SaveChanges(); } // Pass processed users to view (if needed) ViewBag.Users = applicationUsers ?? new List(); return View(); } catch (Exception ex) { // Log the error System.Diagnostics.Debug.WriteLine($"Error in Home/Index: {ex.Message}"); // Provide empty data to prevent null reference ViewBag.Users = new List(); return View(); } } } }