Spaces:
Sleeping
Sleeping
File size: 2,280 Bytes
c2908d2 a19fd25 c2908d2 a19fd25 c2908d2 a19fd25 c2908d2 a19fd25 c2908d2 a19fd25 c2908d2 8ded86b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OilShopManagement.Data;
using OilShopManagement.Models;
namespace OilShopManagement.Controllers;
[Authorize(Roles = "Admin")]
public class CategoriesController : Controller
{
private readonly ApplicationDbContext _context;
public CategoriesController(ApplicationDbContext context) => _context = context;
public async Task<IActionResult> Index() =>
View(await _context.Categories.OrderBy(c => c.Name).ToListAsync());
public IActionResult Create() => View();
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Category category)
{
if (ModelState.IsValid)
{
category.CreatedAt = DateTime.UtcNow;
_context.Categories.Add(category);
await _context.SaveChangesAsync();
TempData["Success"] = "طھظ… ط¥ط¶ط§ظپط© ط§ظ„طھطµظ†ظٹظپ ط¨ظ†ط¬ط§ط";
return RedirectToAction(nameof(Index));
}
return View(category);
}
public async Task<IActionResult> Edit(int id)
{
var cat = await _context.Categories.FindAsync(id);
if (cat == null) return NotFound();
return View(cat);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, Category category)
{
if (id != category.Id) return BadRequest();
if (ModelState.IsValid)
{
_context.Categories.Update(category);
await _context.SaveChangesAsync();
TempData["Success"] = "طھظ… طھط¹ط¯ظٹظ„ ط§ظ„طھطµظ†ظٹظپ ط¨ظ†ط¬ط§ط";
return RedirectToAction(nameof(Index));
}
return View(category);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Delete(int id)
{
var cat = await _context.Categories.FindAsync(id);
if (cat == null) return NotFound();
cat.IsActive = false;
await _context.SaveChangesAsync();
TempData["Success"] = "طھظ… طط°ظپ ط§ظ„طھطµظ†ظٹظپ";
return RedirectToAction(nameof(Index));
}
}
|