Files
SkyArtShop/Controllers/AdminPortfolioController.cs
Local Server 703ab57984 Fix admin route access and backend configuration
- Added /admin redirect to login page in nginx config
- Fixed backend server.js route ordering for proper admin handling
- Updated authentication middleware and routes
- Added user management routes
- Configured PostgreSQL integration
- Updated environment configuration
2025-12-13 22:34:11 -06:00

166 lines
5.5 KiB
C#

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<IActionResult> Categories()
{
return View((await _pgService.GetAllAsync<PortfolioCategory>(_categoriesCollection)).OrderBy((PortfolioCategory c) => c.DisplayOrder).ToList());
}
[HttpGet("category/create")]
public IActionResult CreateCategory()
{
return View(new PortfolioCategory());
}
[HttpPost("category/create")]
public async Task<IActionResult> 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<IActionResult> EditCategory(string id)
{
PortfolioCategory portfolioCategory = await _pgService.GetByIdAsync<PortfolioCategory>(_categoriesCollection, id);
if (portfolioCategory == null)
{
return NotFound();
}
return View(portfolioCategory);
}
[HttpPost("category/edit/{id}")]
public async Task<IActionResult> 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<IActionResult> DeleteCategory(string id)
{
await _pgService.DeleteAsync<PortfolioCategory>(_categoriesCollection, id);
base.TempData["SuccessMessage"] = "Category deleted successfully!";
return RedirectToAction("Categories");
}
[HttpGet("projects")]
public async Task<IActionResult> Projects(string? categoryId)
{
List<PortfolioProject> projects = await _pgService.GetAllAsync<PortfolioProject>(_projectsCollection);
List<PortfolioCategory> source = await _pgService.GetAllAsync<PortfolioCategory>(_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<IActionResult> CreateProject()
{
List<PortfolioCategory> source = await _pgService.GetAllAsync<PortfolioCategory>(_categoriesCollection);
base.ViewBag.Categories = source.Where((PortfolioCategory c) => c.IsActive).ToList();
return View(new PortfolioProject());
}
[HttpPost("project/create")]
public async Task<IActionResult> CreateProject(PortfolioProject project)
{
if (!base.ModelState.IsValid)
{
List<PortfolioCategory> source = await _pgService.GetAllAsync<PortfolioCategory>(_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<IActionResult> EditProject(string id)
{
PortfolioProject project = await _pgService.GetByIdAsync<PortfolioProject>(_projectsCollection, id);
if (project == null)
{
return NotFound();
}
List<PortfolioCategory> source = await _pgService.GetAllAsync<PortfolioCategory>(_categoriesCollection);
base.ViewBag.Categories = source.Where((PortfolioCategory c) => c.IsActive).ToList();
return View(project);
}
[HttpPost("project/edit/{id}")]
public async Task<IActionResult> EditProject(string id, PortfolioProject project)
{
if (!base.ModelState.IsValid)
{
List<PortfolioCategory> source = await _pgService.GetAllAsync<PortfolioCategory>(_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<IActionResult> DeleteProject(string id)
{
await _pgService.DeleteAsync<PortfolioProject>(_projectsCollection, id);
base.TempData["SuccessMessage"] = "Project deleted successfully!";
return RedirectToAction("Projects");
}
}