source: StockMaster/Controllers/WarehouseController.cs

main
Last change on this file was dfe03b8, checked in by Ceyda <ceyda.huseini@…>, 3 days ago

Initialize StockMaster project

  • Property mode set to 100644
File size: 3.1 KB
Line 
1using Microsoft.AspNetCore.Mvc;
2using StockMaster.Data;
3using StockMaster.Models;
4using StockMaster.Services;
5using Microsoft.EntityFrameworkCore;
6using System.Threading.Tasks;
7using Microsoft.AspNetCore.Authorization;
8
9namespace StockMaster.Controllers
10{
11 [Authorize]
12 public class WarehouseController : Controller
13 {
14 private readonly IWarehouseService _warehouseService;
15 private readonly StockDbContext _context;
16
17 public WarehouseController(IWarehouseService warehouseService, StockDbContext context)
18 {
19 _warehouseService = warehouseService;
20 _context = context;
21 }
22
23 public async Task<IActionResult> Index()
24 {
25 var warehouses = await _context.Warehouses.OrderBy(w => w.Name).ToListAsync();
26 return View(warehouses);
27 }
28
29 public async Task<IActionResult> Stock(int id)
30 {
31 var warehouse = await _context.Warehouses.FindAsync(id);
32 if (warehouse == null) return NotFound();
33
34 var stock = await _warehouseService.GetWarehouseStockAsync(id);
35 ViewBag.Warehouse = warehouse;
36 return View(stock);
37 }
38
39 [HttpGet]
40 public IActionResult Create()
41 {
42 return View();
43 }
44
45 [HttpPost]
46 [ValidateAntiForgeryToken]
47 public async Task<IActionResult> Create(Warehouse warehouse)
48 {
49 if (ModelState.IsValid)
50 {
51 _context.Warehouses.Add(warehouse);
52 await _context.SaveChangesAsync();
53 TempData["Success"] = "Warehouse created successfully.";
54 return RedirectToAction("Index");
55 }
56 return View(warehouse);
57 }
58
59 [HttpGet]
60 public async Task<IActionResult> Edit(int id)
61 {
62 var warehouse = await _context.Warehouses.FindAsync(id);
63 if (warehouse == null) return NotFound();
64 return View(warehouse);
65 }
66
67 [HttpPost]
68 [ValidateAntiForgeryToken]
69 public async Task<IActionResult> Edit(Warehouse warehouse)
70 {
71 if (ModelState.IsValid)
72 {
73 _context.Warehouses.Update(warehouse);
74 await _context.SaveChangesAsync();
75 TempData["Success"] = "Warehouse updated successfully.";
76 return RedirectToAction("Index");
77 }
78 return View(warehouse);
79 }
80
81 [HttpPost]
82 [ValidateAntiForgeryToken]
83 public async Task<IActionResult> Delete(int id)
84 {
85 try
86 {
87 var warehouse = await _context.Warehouses.FindAsync(id);
88 if (warehouse != null)
89 {
90 _context.Warehouses.Remove(warehouse);
91 await _context.SaveChangesAsync();
92 TempData["Success"] = "Warehouse deleted successfully.";
93 }
94 }
95 catch
96 {
97 TempData["Error"] = "Cannot delete warehouse. It contains stock or historical data.";
98 }
99 return RedirectToAction("Index");
100 }
101 }
102}
Note: See TracBrowser for help on using the repository browser.