138 lines
4.5 KiB
C#
138 lines
4.5 KiB
C#
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using LingAdmin.API.Data;
|
||
|
|
using LingAdmin.API.Models;
|
||
|
|
using LingAdmin.API.DTOs;
|
||
|
|
using Dapr.Client;
|
||
|
|
|
||
|
|
namespace LingAdmin.API.Controllers;
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/[controller]")]
|
||
|
|
public class InventoryController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly AppDbContext _context;
|
||
|
|
private readonly DaprClient _daprClient;
|
||
|
|
private readonly ILogger<InventoryController> _logger;
|
||
|
|
|
||
|
|
public InventoryController(AppDbContext context, DaprClient daprClient, ILogger<InventoryController> logger)
|
||
|
|
{
|
||
|
|
_context = context;
|
||
|
|
_daprClient = daprClient;
|
||
|
|
_logger = logger;
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet]
|
||
|
|
public async Task<ActionResult<ApiResponse<List<InventoryItem>>>> GetInventory()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var items = await _context.InventoryItems.ToListAsync();
|
||
|
|
return Ok(ApiResponse<List<InventoryItem>>.Success(items));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
_logger.LogError(ex, "Error retrieving inventory items");
|
||
|
|
return StatusCode(500, ApiResponse<List<InventoryItem>>.Error("Internal server error", 500));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet("{id}")]
|
||
|
|
public async Task<ActionResult<ApiResponse<InventoryItem>>> GetInventoryItem(int id)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var item = await _context.InventoryItems.FindAsync(id);
|
||
|
|
if (item == null)
|
||
|
|
{
|
||
|
|
return NotFound(ApiResponse<InventoryItem>.Error("Item not found", 404));
|
||
|
|
}
|
||
|
|
return Ok(ApiResponse<InventoryItem>.Success(item));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
_logger.LogError(ex, "Error retrieving inventory item {Id}", id);
|
||
|
|
return StatusCode(500, ApiResponse<InventoryItem>.Error("Internal server error", 500));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost]
|
||
|
|
public async Task<ActionResult<ApiResponse<InventoryItem>>> CreateInventoryItem(InventoryItem item)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
_context.InventoryItems.Add(item);
|
||
|
|
await _context.SaveChangesAsync();
|
||
|
|
|
||
|
|
// Publish event via Dapr
|
||
|
|
await _daprClient.PublishEventAsync("pubsub", "inventory-created", item);
|
||
|
|
|
||
|
|
return CreatedAtAction(nameof(GetInventoryItem), new { id = item.Id }, ApiResponse<InventoryItem>.Success(item));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
_logger.LogError(ex, "Error creating inventory item");
|
||
|
|
return StatusCode(500, ApiResponse<InventoryItem>.Error("Internal server error", 500));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPut("{id}")]
|
||
|
|
public async Task<ActionResult<ApiResponse<InventoryItem>>> UpdateInventoryItem(int id, InventoryItem item)
|
||
|
|
{
|
||
|
|
if (id != item.Id)
|
||
|
|
{
|
||
|
|
return BadRequest(ApiResponse<InventoryItem>.Error("ID mismatch", 400));
|
||
|
|
}
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
_context.Entry(item).State = EntityState.Modified;
|
||
|
|
await _context.SaveChangesAsync();
|
||
|
|
|
||
|
|
// Publish event via Dapr
|
||
|
|
await _daprClient.PublishEventAsync("pubsub", "inventory-updated", item);
|
||
|
|
|
||
|
|
return Ok(ApiResponse<InventoryItem>.Success(item));
|
||
|
|
}
|
||
|
|
catch (DbUpdateConcurrencyException)
|
||
|
|
{
|
||
|
|
if (!await _context.InventoryItems.AnyAsync(e => e.Id == id))
|
||
|
|
{
|
||
|
|
return NotFound(ApiResponse<InventoryItem>.Error("Item not found", 404));
|
||
|
|
}
|
||
|
|
throw;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
_logger.LogError(ex, "Error updating inventory item {Id}", id);
|
||
|
|
return StatusCode(500, ApiResponse<InventoryItem>.Error("Internal server error", 500));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpDelete("{id}")]
|
||
|
|
public async Task<ActionResult<ApiResponse<object>>> DeleteInventoryItem(int id)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var item = await _context.InventoryItems.FindAsync(id);
|
||
|
|
if (item == null)
|
||
|
|
{
|
||
|
|
return NotFound(ApiResponse<object>.Error("Item not found", 404));
|
||
|
|
}
|
||
|
|
|
||
|
|
_context.InventoryItems.Remove(item);
|
||
|
|
await _context.SaveChangesAsync();
|
||
|
|
|
||
|
|
// Publish event via Dapr
|
||
|
|
await _daprClient.PublishEventAsync("pubsub", "inventory-deleted", new { id });
|
||
|
|
|
||
|
|
return Ok(ApiResponse<object>.Success(new { message = "Item deleted successfully" }));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
_logger.LogError(ex, "Error deleting inventory item {Id}", id);
|
||
|
|
return StatusCode(500, ApiResponse<object>.Error("Internal server error", 500));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|