| 1 | using Microsoft.AspNetCore.Mvc;
|
|---|
| 2 | using StockMaster.Data;
|
|---|
| 3 | using StockMaster.Models;
|
|---|
| 4 | using StockMaster.Services;
|
|---|
| 5 | using System.Linq;
|
|---|
| 6 | using System.Threading.Tasks;
|
|---|
| 7 |
|
|---|
| 8 | namespace StockMaster.Controllers
|
|---|
| 9 | {
|
|---|
| 10 | public class ProductController : BaseController
|
|---|
| 11 | {
|
|---|
| 12 | private readonly IProductService _productService;
|
|---|
| 13 | private readonly StockDbContext _context;
|
|---|
| 14 |
|
|---|
| 15 | public ProductController(IProductService productService, StockDbContext context)
|
|---|
| 16 | {
|
|---|
| 17 | _productService = productService;
|
|---|
| 18 | _context = context;
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | public async Task<IActionResult> Index()
|
|---|
| 22 | {
|
|---|
| 23 | var products = await _productService.GetAllProductsAsync();
|
|---|
| 24 | return View(products);
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | [HttpGet]
|
|---|
| 28 | public IActionResult Create()
|
|---|
| 29 | {
|
|---|
| 30 | ViewBag.Categories = _context.Categories.OrderBy(c => c.Name).ToList();
|
|---|
| 31 | ViewBag.Suppliers = _context.Suppliers.OrderBy(s => s.Name).ToList();
|
|---|
| 32 | return View();
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | [HttpPost]
|
|---|
| 36 | [ValidateAntiForgeryToken]
|
|---|
| 37 | public async Task<IActionResult> Create(Product product)
|
|---|
| 38 | {
|
|---|
| 39 |
|
|---|
| 40 | ModelState.Remove("Category");
|
|---|
| 41 | ModelState.Remove("Supplier");
|
|---|
| 42 |
|
|---|
| 43 | if (ModelState.IsValid)
|
|---|
| 44 | {
|
|---|
| 45 | try
|
|---|
| 46 | {
|
|---|
| 47 | var result = await _productService.CreateProductAsync(product);
|
|---|
| 48 | if (result)
|
|---|
| 49 | {
|
|---|
| 50 | TempData["Success"] = "Product created successfully";
|
|---|
| 51 | return RedirectToAction("Index");
|
|---|
| 52 | }
|
|---|
| 53 | ModelState.AddModelError("", "Failed to create product. SKU may already exist.");
|
|---|
| 54 | }
|
|---|
| 55 | catch (Exception ex)
|
|---|
| 56 | {
|
|---|
| 57 | ModelState.AddModelError("", $"Failed to create product: {ex.Message}");
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | ViewBag.Categories = _context.Categories.OrderBy(c => c.Name).ToList();
|
|---|
| 62 | ViewBag.Suppliers = _context.Suppliers.OrderBy(s => s.Name).ToList();
|
|---|
| 63 | return View(product);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | [HttpGet]
|
|---|
| 67 | public async Task<IActionResult> Edit(int id)
|
|---|
| 68 | {
|
|---|
| 69 | var product = await _productService.GetProductByIdAsync(id);
|
|---|
| 70 | if (product == null)
|
|---|
| 71 | return NotFound();
|
|---|
| 72 |
|
|---|
| 73 | ViewBag.Categories = _context.Categories.OrderBy(c => c.Name).ToList();
|
|---|
| 74 | ViewBag.Suppliers = _context.Suppliers.OrderBy(s => s.Name).ToList();
|
|---|
| 75 | return View(product);
|
|---|
| 76 | }
|
|---|
| 77 | [HttpPost]
|
|---|
| 78 | [ValidateAntiForgeryToken]
|
|---|
| 79 | public async Task<IActionResult> Edit(Product product)
|
|---|
| 80 | {
|
|---|
| 81 | ModelState.Remove("Category");
|
|---|
| 82 | ModelState.Remove("Supplier");
|
|---|
| 83 |
|
|---|
| 84 | if (ModelState.IsValid)
|
|---|
| 85 | {
|
|---|
| 86 | try
|
|---|
| 87 | {
|
|---|
| 88 | var currentUserName = User.Identity?.Name ?? "system";
|
|---|
| 89 |
|
|---|
| 90 | var result = await _productService.UpdateProductWithUserAsync(product, currentUserName);
|
|---|
| 91 |
|
|---|
| 92 | if (result)
|
|---|
| 93 | {
|
|---|
| 94 | TempData["Success"] = "Product updated successfully";
|
|---|
| 95 | return RedirectToAction("Index");
|
|---|
| 96 | }
|
|---|
| 97 | ModelState.AddModelError("", "Failed to update product");
|
|---|
| 98 | }
|
|---|
| 99 | catch (Exception ex)
|
|---|
| 100 | {
|
|---|
| 101 | ModelState.AddModelError("", $"Failed to update product: {ex.Message}");
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | ViewBag.Categories = _context.Categories.OrderBy(c => c.Name).ToList();
|
|---|
| 106 | ViewBag.Suppliers = _context.Suppliers.OrderBy(s => s.Name).ToList();
|
|---|
| 107 | return View(product);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | [HttpPost]
|
|---|
| 111 | [ValidateAntiForgeryToken]
|
|---|
| 112 | public async Task<IActionResult> Delete(int id)
|
|---|
| 113 | {
|
|---|
| 114 | try
|
|---|
| 115 | {
|
|---|
| 116 | var result = await _productService.DeleteProductAsync(id);
|
|---|
| 117 | if (result)
|
|---|
| 118 | {
|
|---|
| 119 | TempData["Success"] = "Product deleted successfully";
|
|---|
| 120 | }
|
|---|
| 121 | else
|
|---|
| 122 | {
|
|---|
| 123 | TempData["Error"] = "Failed to delete product";
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 | catch
|
|---|
| 127 | {
|
|---|
| 128 | TempData["Error"] = "Cannot delete product. It may be in use.";
|
|---|
| 129 | }
|
|---|
| 130 | return RedirectToAction("Index");
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 | } |
|---|