using ECommerce.Model.Entities; using ECommerce.Model.Repositories; using ECommerce.Presenter.ViewModels; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Ecommerce_web_project.Controllers.Admin; [Area("Admin")] [Authorize(Roles = "Admin")] public class CategoriesController : Controller { private readonly IRepository _repo; public CategoriesController(IRepository repo) => _repo = repo; public async Task Index() { var categories = await _repo.GetAllAsync(); var model = categories.Select(c => new CategoryViewModel { Id = c.Id, Name = c.Name, Description = c.Description, ImageUrl = c.ImageUrl }).ToList(); return View(model); } public IActionResult Create() => View(); [HttpPost] public async Task Create(CategoryViewModel model) { if (!ModelState.IsValid) return View(model); var category = new Category { Name = model.Name, Description = model.Description, ImageUrl = model.ImageUrl }; await _repo.AddAsync(category); return RedirectToAction(nameof(Index)); } public async Task Edit(Guid id) { var category = await _repo.GetByIdAsync(id); if (category == null) return NotFound(); return View(new CategoryViewModel { Id = category.Id, Name = category.Name, Description = category.Description, ImageUrl = category.ImageUrl }); } [HttpPost] public async Task Edit(CategoryViewModel model) { if (!ModelState.IsValid) return View(model); var category = await _repo.GetByIdAsync(model.Id); if (category == null) return NotFound(); category.Name = model.Name; category.Description = model.Description; category.ImageUrl = model.ImageUrl; category.UpdatedAt = DateTime.UtcNow; await _repo.UpdateAsync(category); return RedirectToAction(nameof(Index)); } [HttpPost] public async Task Delete(Guid id) { var category = await _repo.GetByIdAsync(id); if (category == null) return NotFound(); await _repo.DeleteAsync(category); return RedirectToAction(nameof(Index)); } }