Files
SkyArtShop/Sky_Art_shop/Controllers/AdminMenuController.cs

114 lines
4.7 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using SkyArtShop.Models;
using SkyArtShop.Services;
namespace SkyArtShop.Controllers
{
[Route("admin/menu")]
[Authorize(Roles = "Admin")]
public class AdminMenuController : Controller
{
private readonly MongoDBService _mongoService;
public AdminMenuController(MongoDBService mongoService)
{
_mongoService = mongoService;
}
[HttpGet("")]
public async Task<IActionResult> Index()
{
var menuItems = await _mongoService.GetAllAsync<MenuItem>("MenuItems");
return View(menuItems.OrderBy(m => m.DisplayOrder).ToList());
}
[HttpPost("reseed")]
public async Task<IActionResult> ReseedMenu()
{
// Delete all existing menu items
var existingItems = await _mongoService.GetAllAsync<MenuItem>("MenuItems");
foreach (var item in existingItems)
{
await _mongoService.DeleteAsync<MenuItem>("MenuItems", item.Id!);
}
// Add new menu items
var defaultMenuItems = new[]
{
new MenuItem { Label = "Home", Url = "/", DisplayOrder = 1, IsActive = true, ShowInNavbar = true, ShowInDropdown = true },
new MenuItem { Label = "Shop", Url = "/Shop", DisplayOrder = 2, IsActive = true, ShowInNavbar = true, ShowInDropdown = true },
new MenuItem { Label = "Top Sellers", Url = "/#top-sellers", DisplayOrder = 3, IsActive = true, ShowInNavbar = true, ShowInDropdown = true },
new MenuItem { Label = "Promotion", Url = "/#promotion", DisplayOrder = 4, IsActive = true, ShowInNavbar = true, ShowInDropdown = true },
new MenuItem { Label = "Portfolio", Url = "/Portfolio", DisplayOrder = 5, IsActive = true, ShowInNavbar = true, ShowInDropdown = true },
new MenuItem { Label = "Blog", Url = "/Blog", DisplayOrder = 6, IsActive = true, ShowInNavbar = true, ShowInDropdown = true },
new MenuItem { Label = "About", Url = "/About", DisplayOrder = 7, IsActive = true, ShowInNavbar = true, ShowInDropdown = true },
new MenuItem { Label = "Instagram", Url = "#instagram", DisplayOrder = 8, IsActive = true, ShowInNavbar = false, ShowInDropdown = true },
new MenuItem { Label = "Contact", Url = "/Contact", DisplayOrder = 9, IsActive = true, ShowInNavbar = true, ShowInDropdown = true },
new MenuItem { Label = "My Wishlist", Url = "#wishlist", DisplayOrder = 10, IsActive = true, ShowInNavbar = false, ShowInDropdown = true }
};
foreach (var item in defaultMenuItems)
{
await _mongoService.InsertAsync("MenuItems", item);
}
TempData["SuccessMessage"] = "Menu items reseeded successfully!";
return RedirectToAction("Index");
}
[HttpGet("create")]
public IActionResult Create()
{
return View(new MenuItem());
}
[HttpPost("create")]
public async Task<IActionResult> Create(MenuItem menuItem)
{
if (!ModelState.IsValid)
{
return View(menuItem);
}
menuItem.CreatedAt = DateTime.UtcNow;
await _mongoService.InsertAsync("MenuItems", menuItem);
TempData["SuccessMessage"] = "Menu item created successfully!";
return RedirectToAction("Index");
}
[HttpGet("edit/{id}")]
public async Task<IActionResult> Edit(string id)
{
var menuItem = await _mongoService.GetByIdAsync<MenuItem>("MenuItems", id);
if (menuItem == null)
{
return NotFound();
}
return View(menuItem);
}
[HttpPost("edit/{id}")]
public async Task<IActionResult> Edit(string id, MenuItem menuItem)
{
if (!ModelState.IsValid)
{
return View(menuItem);
}
menuItem.Id = id;
await _mongoService.UpdateAsync("MenuItems", id, menuItem);
TempData["SuccessMessage"] = "Menu item updated successfully!";
return RedirectToAction("Index");
}
[HttpPost("delete/{id}")]
public async Task<IActionResult> Delete(string id)
{
await _mongoService.DeleteAsync<MenuItem>("MenuItems", id);
TempData["SuccessMessage"] = "Menu item deleted successfully!";
return RedirectToAction("Index");
}
}
}