Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions MyCraftShop/Controllers/InventoryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyCraftShop.Data;
using MyCraftShop.Models;

namespace MyCraftShop.Controllers
{
public class InventoryController : Controller
{
private readonly ApplicationDbContext _context;
private const int PageSize = 10;

public InventoryController(ApplicationDbContext context)
{
_context = context;
}

// GET: Inventory
public async Task<IActionResult> Index(int page = 1)
{
var totalItems = await _context.InventoryItems.CountAsync();
var totalPages = (int)Math.Ceiling(totalItems / (double)PageSize);
page = Math.Max(1, Math.Min(page, Math.Max(1, totalPages)));

var items = await _context.InventoryItems
.OrderBy(i => i.Name)
.Skip((page - 1) * PageSize)
.Take(PageSize)
.ToListAsync();

ViewBag.CurrentPage = page;
ViewBag.TotalPages = totalPages;

return View(items);
}

// GET: Inventory/Create
public IActionResult Create()
{
return View();
}

// POST: Inventory/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Name,Description,CostPerItem,SalesPrice,Quantity")] InventoryItem inventoryItem)
{
if (ModelState.IsValid)
{
_context.Add(inventoryItem);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(inventoryItem);
}

// GET: Inventory/Edit/5
[HttpGet]
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}

var inventoryItem = await _context.InventoryItems.FindAsync(id);
if (inventoryItem == null)
{
return NotFound();
}

return PartialView("_EditModal", inventoryItem);
}

// POST: Inventory/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Name,Description,CostPerItem,SalesPrice,Quantity")] InventoryItem inventoryItem)
{
if (id != inventoryItem.Id)
{
return NotFound();
}

if (ModelState.IsValid)
{
try
{
_context.Update(inventoryItem);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!InventoryItemExists(inventoryItem.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}

return PartialView("_EditModal", inventoryItem);
}

// POST: Inventory/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Delete(int id)
{
var inventoryItem = await _context.InventoryItems.FindAsync(id);
if (inventoryItem != null)
{
_context.InventoryItems.Remove(inventoryItem);
await _context.SaveChangesAsync();
}

return RedirectToAction(nameof(Index));
}

private bool InventoryItemExists(int id)
{
return _context.InventoryItems.Any(e => e.Id == id);
}
}
}
2 changes: 2 additions & 0 deletions MyCraftShop/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using MyCraftShop.Models;

namespace MyCraftShop.Data
{
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : IdentityDbContext(options)
{
public DbSet<InventoryItem> InventoryItems { get; set; }
}
}
Loading