using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using SkyArtShop.Models; using SkyArtShop.Services; namespace SkyArtShop.Controllers { [Route("admin")] [Authorize(Roles = "Admin")] public class AdminController : Controller { private readonly MongoDBService _mongoService; private readonly SignInManager _signInManager; private readonly UserManager _userManager; public AdminController(MongoDBService mongoService, SignInManager signInManager, UserManager userManager) { _mongoService = mongoService; _signInManager = signInManager; _userManager = userManager; } [HttpGet("login")] [AllowAnonymous] public IActionResult Login() { if (User.Identity?.IsAuthenticated == true) { return RedirectToAction("Dashboard"); } return View(); } [HttpPost("login")] [AllowAnonymous] public async Task Login(string email, string password) { var user = await _userManager.FindByEmailAsync(email); if (user == null) { ViewBag.Error = "Invalid email or password"; return View(); } var result = await _signInManager.PasswordSignInAsync(user, password, true, false); if (!result.Succeeded) { ViewBag.Error = "Invalid email or password"; return View(); } return RedirectToAction("Dashboard"); } [HttpGet("logout")] public async Task Logout() { await _signInManager.SignOutAsync(); return RedirectToAction("Login"); } [HttpGet("dashboard")] public async Task Dashboard() { var products = await _mongoService.GetAllAsync("Products"); var projects = await _mongoService.GetAllAsync("PortfolioProjects"); var blogPosts = await _mongoService.GetAllAsync("BlogPosts"); var pages = await _mongoService.GetAllAsync("Pages"); var settings = (await _mongoService.GetAllAsync("SiteSettings")).FirstOrDefault(); ViewBag.ProductCount = products.Count; ViewBag.ProjectCount = projects.Count; ViewBag.BlogCount = blogPosts.Count; ViewBag.PageCount = pages.Count; ViewBag.SiteName = settings?.SiteName ?? "Sky Art Shop"; ViewBag.AdminEmail = User.Identity?.Name; return View(); } [HttpGet("")] public IActionResult Index() => RedirectToAction("Dashboard"); } }