using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SkyArtShop.Models; using SkyArtShop.Services; namespace SkyArtShop.Controllers; [Route("admin/portfolio")] [Authorize(Roles = "Admin,MasterAdmin")] public class AdminPortfolioController : Controller { private readonly PostgreSQLService _pgService; private readonly SlugService _slugService; private readonly string _categoriesCollection = "PortfolioCategories"; private readonly string _projectsCollection = "PortfolioProjects"; public AdminPortfolioController(PostgreSQLService pgService, SlugService slugService) { _pgService = pgService; _slugService = slugService; } [HttpGet("categories")] public async Task Categories() { return View((await _pgService.GetAllAsync(_categoriesCollection)).OrderBy((PortfolioCategory c) => c.DisplayOrder).ToList()); } [HttpGet("category/create")] public IActionResult CreateCategory() { return View(new PortfolioCategory()); } [HttpPost("category/create")] public async Task CreateCategory(PortfolioCategory category) { if (!base.ModelState.IsValid) { return View(category); } category.CreatedAt = DateTime.UtcNow; category.UpdatedAt = DateTime.UtcNow; category.Slug = _slugService.GenerateSlug(category.Name); await _pgService.InsertAsync(_categoriesCollection, category); base.TempData["SuccessMessage"] = "Category created successfully!"; return RedirectToAction("Categories"); } [HttpGet("category/edit/{id}")] public async Task EditCategory(string id) { PortfolioCategory portfolioCategory = await _pgService.GetByIdAsync(_categoriesCollection, id); if (portfolioCategory == null) { return NotFound(); } return View(portfolioCategory); } [HttpPost("category/edit/{id}")] public async Task EditCategory(string id, PortfolioCategory category) { if (!base.ModelState.IsValid) { return View(category); } category.Id = id; category.UpdatedAt = DateTime.UtcNow; category.Slug = _slugService.GenerateSlug(category.Name); await _pgService.UpdateAsync(_categoriesCollection, id, category); base.TempData["SuccessMessage"] = "Category updated successfully!"; return RedirectToAction("Categories"); } [HttpPost("category/delete/{id}")] public async Task DeleteCategory(string id) { await _pgService.DeleteAsync(_categoriesCollection, id); base.TempData["SuccessMessage"] = "Category deleted successfully!"; return RedirectToAction("Categories"); } [HttpGet("projects")] public async Task Projects(string? categoryId) { List projects = await _pgService.GetAllAsync(_projectsCollection); List source = await _pgService.GetAllAsync(_categoriesCollection); if (!string.IsNullOrEmpty(categoryId)) { projects = projects.Where((PortfolioProject p) => p.CategoryId == categoryId).ToList(); } base.ViewBag.Categories = source.Where((PortfolioCategory c) => c.IsActive).ToList(); base.ViewBag.SelectedCategory = categoryId; return View(projects.OrderBy((PortfolioProject p) => p.DisplayOrder).ToList()); } [HttpGet("project/create")] public async Task CreateProject() { List source = await _pgService.GetAllAsync(_categoriesCollection); base.ViewBag.Categories = source.Where((PortfolioCategory c) => c.IsActive).ToList(); return View(new PortfolioProject()); } [HttpPost("project/create")] public async Task CreateProject(PortfolioProject project) { if (!base.ModelState.IsValid) { List source = await _pgService.GetAllAsync(_categoriesCollection); base.ViewBag.Categories = source.Where((PortfolioCategory c) => c.IsActive).ToList(); return View(project); } project.CreatedAt = DateTime.UtcNow; project.UpdatedAt = DateTime.UtcNow; await _pgService.InsertAsync(_projectsCollection, project); base.TempData["SuccessMessage"] = "Project created successfully!"; return RedirectToAction("Projects"); } [HttpGet("project/edit/{id}")] public async Task EditProject(string id) { PortfolioProject project = await _pgService.GetByIdAsync(_projectsCollection, id); if (project == null) { return NotFound(); } List source = await _pgService.GetAllAsync(_categoriesCollection); base.ViewBag.Categories = source.Where((PortfolioCategory c) => c.IsActive).ToList(); return View(project); } [HttpPost("project/edit/{id}")] public async Task EditProject(string id, PortfolioProject project) { if (!base.ModelState.IsValid) { List source = await _pgService.GetAllAsync(_categoriesCollection); base.ViewBag.Categories = source.Where((PortfolioCategory c) => c.IsActive).ToList(); return View(project); } project.Id = id; project.UpdatedAt = DateTime.UtcNow; await _pgService.UpdateAsync(_projectsCollection, id, project); base.TempData["SuccessMessage"] = "Project updated successfully!"; return RedirectToAction("Projects"); } [HttpPost("project/delete/{id}")] public async Task DeleteProject(string id) { await _pgService.DeleteAsync(_projectsCollection, id); base.TempData["SuccessMessage"] = "Project deleted successfully!"; return RedirectToAction("Projects"); } }