Inventory controller, model, viewmodel (before relation to product)
Fri Aug 09 2024 12:16:37 GMT+0000 (Coordinated Universal Time)
Saved by @iamkatmakhafola
//controller
namespace av_motion_api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class InventoriesController : ControllerBase
{
private readonly AppDbContext _appContext;
public readonly IRepository _repository;
public InventoriesController(AppDbContext _context, IRepository repository)
{
_appContext = _context;
_repository = repository;
}
// GET: api/<InventoriesController>
[HttpGet]
public async Task<ActionResult<IEnumerable<InventoryViewModel>>> GetInventories()
{
var inventories = await _repository.GetInventoryDetails();
return Ok(inventories);
}
// GET api/<InventoriesController>/5
[HttpGet("{id}")]
public async Task<ActionResult<InventoryViewModel>> GetInventoryItem(int id)
{
if (_appContext.Inventory == null)
{
return NotFound();
}
var inventoryItem = await _repository.GetInventoryItem(id);
if (inventoryItem == null)
{
return NotFound();
}
return Ok(inventoryItem);
}
// POST api/<InventoriesController>
[HttpPost]
public async Task<ActionResult<Inventory>> PostInventoryItem([FromBody] InventoryViewModel inventoryItem)
{
var item = new Inventory
{
Inventory_Item_Category = inventoryItem.category,
Inventory_Item_Name = inventoryItem.itemName,
Inventory_Item_Quantity = inventoryItem.quantity,
Inventory_Item_Photo = inventoryItem.photo,
Supplier_ID = inventoryItem.supplierID,
Received_Supplier_Order_ID = inventoryItem.received_supplier_order_id
};
if (_appContext.Inventory == null)
{
return Problem("Entity set 'AppDbContext.Inventory' is null.");
}
_appContext.Inventory.Add(item);
await _appContext.SaveChangesAsync();
return Ok(item);
}
// PUT api/<InventoriesController>/5
[HttpPut("{id}")]
public async Task<IActionResult> PutInventoryItem(int id, [FromBody] InventoryViewModel inventoryItem)
{
if (id != inventoryItem.inventoryID)
{
return BadRequest();
}
var existingitem = await _appContext.Inventory.FindAsync(id);
if (existingitem == null)
{
return NotFound();
}
existingitem.Inventory_Item_Name = inventoryItem.itemName;
existingitem.Inventory_Item_Category = inventoryItem.category;
existingitem.Inventory_Item_Quantity = inventoryItem.quantity;
existingitem.Supplier_ID = inventoryItem.supplierID;
existingitem.Inventory_Item_Photo = inventoryItem.photo;
// _appContext.Entry(inventoryItem).State = EntityState.Modified;
try
{
_appContext.Inventory.Update(existingitem);
await _appContext.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!InventoryStatusExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// DELETE api/<InventoriesController>/5
//[HttpDelete("{id}")]
//public void Delete(int id)
//{
//}
private bool InventoryStatusExists(int id)
{
return (_appContext.Inventory?.Any(e => e.Inventory_ID == id)).GetValueOrDefault();
}
}
}
//model
namespace av_motion_api.Models
{
public class Inventory
{
[Key]
public int Inventory_ID { get; set; }
[Required]
[StringLength(50)]
public string Inventory_Item_Category { get; set;}
[Required]
[StringLength(50)]
public string Inventory_Item_Name { get; set; }
[Required]
public int Inventory_Item_Quantity { get; set; }
[Required]
public string Inventory_Item_Photo { get; set; }
public int Supplier_ID { get; set; }
[ForeignKey(nameof(Supplier_ID))]
public Supplier Supplier { get; set; }
public int Received_Supplier_Order_ID { get; set; }
[ForeignKey(nameof(Received_Supplier_Order_ID))]
public Received_Supplier_Order Received_Supplier_Order { get; set; }
}
}
//viewmodel
namespace av_motion_api.ViewModels
{
public class InventoryViewModel
{
public int inventoryID { get; set; }
public string category { get; set; }
public string itemName { get; set; }
public int quantity { get; set; }
public string photo { get; set; }
public int supplierID { get; set; }
public string supplierName { get; set; }
public int received_supplier_order_id { get; set; }
}
}



Comments