#nullable disable
using BLL.Controllers.Bases;
using BLL.DAL;
using BLL.Models;
using BLL.Services.Bases;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
// Generated from Custom Template.
namespace MVC.Controllers
{
public class CategoriesController : MvcController
{
// Service injections:
private readonly IService<Category, CategoryModel> _categoryService;
/* Can be uncommented and used for many to many relationships. {Entity} may be replaced with the related entiy name in the controller and views. */
//private readonly IService<{Entity}, {Entity}Model> _{Entity}Service;
public CategoriesController(
IService<Category, CategoryModel> categoryService
/* Can be uncommented and used for many to many relationships. {Entity} may be replaced with the related entiy name in the controller and views. */
//, IService<{Entity}, {Entity}Model> {Entity}Service
)
{
_categoryService = categoryService;
/* Can be uncommented and used for many to many relationships. {Entity} may be replaced with the related entiy name in the controller and views. */
//_{Entity}Service = {Entity}Service;
}
// GET: Categories
[Authorize(Roles = "Admin")]
public IActionResult Index()
{
// Get collection service logic:
var list = _categoryService.Query().ToList();
return View(list);
}
// GET: Categories/Details/5
[Authorize(Roles = "Admin")]
public IActionResult Details(int id)
{
// Get item service logic:
var item = _categoryService.Query().SingleOrDefault(q => q.Record.Id == id);
return View(item);
}
protected void SetViewData()
{
// Related items service logic to set ViewData (Record.Id and Name parameters may need to be changed in the SelectList constructor according to the model):
/* Can be uncommented and used for many to many relationships. {Entity} may be replaced with the related entiy name in the controller and views. */
//ViewBag.{Entity}Ids = new MultiSelectList(_{Entity}Service.Query().ToList(), "Record.Id", "Name");
}
// GET: Categories/Create
[Authorize(Roles = "Admin")]
public IActionResult Create()
{
SetViewData();
return View();
}
// POST: Categories/Create
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public IActionResult Create(CategoryModel category)
{
if (ModelState.IsValid)
{
// Insert item service logic:
var result = _categoryService.Create(category.Record);
if (result.IsSuccessful)
{
TempData["Message"] = result.Message;
return RedirectToAction(nameof(Details), new { id = category.Record.Id });
}
ModelState.AddModelError("", result.Message);
}
SetViewData();
return View(category);
}
// GET: Categories/Edit/5
[Authorize(Roles = "Admin")]
public IActionResult Edit(int id)
{
// Get item to edit service logic:
var item = _categoryService.Query().SingleOrDefault(q => q.Record.Id == id);
SetViewData();
return View(item);
}
// POST: Categories/Edit
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public IActionResult Edit(CategoryModel category)
{
if (ModelState.IsValid)
{
// Update item service logic:
var result = _categoryService.Update(category.Record);
if (result.IsSuccessful)
{
TempData["Message"] = result.Message;
return RedirectToAction(nameof(Details), new { id = category.Record.Id });
}
ModelState.AddModelError("", result.Message);
}
SetViewData();
return View(category);
}
// GET: Categories/Delete/5
[Authorize(Roles = "Admin")]
public IActionResult Delete(int id)
{
// Get item to delete service logic:
var item = _categoryService.Query().SingleOrDefault(q => q.Record.Id == id);
return View(item);
}
// POST: Categories/Delete
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public IActionResult DeleteConfirmed(int id)
{
// Delete item service logic:
var result = _categoryService.Delete(id);
TempData["Message"] = result.Message;
return RedirectToAction(nameof(Index));
}
}
}