using Asp.Versioning;
using Core.Adapters.Lib;
using Core.Inventory.External.Clients.Inventory;
using Core.Inventory.External.Clients.Inventory.Requests.Base;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
namespace Core.Inventory.BFF.API.Controllers
{
///
/// Handles all requests for furniture base operations.
///
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
[Consumes("application/json")]
[Produces("application/json")]
public class FurnitureBaseController(IInventoryServiceClient inventoryClient, ILogger logger) : BaseController(logger)
{
///
/// Gets all furniture base records.
///
[HttpGet("GetAll")]
[ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)]
public async Task GetAllBaseAsync(CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(GetAllBaseAsync)} - Request received.");
return await Handle(() => inventoryClient.GetAllBaseAsync(TrackingId.ToString(), cancellationToken));
}
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(GetAllBaseAsync)} - An error occurred.");
throw;
}
}
///
/// Gets a furniture base by its identifier.
///
[HttpPost("GetById")]
[ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status200OK)]
public async Task GetByIdAsync([FromBody] GetFurnitureBaseByIdRequest request, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(GetByIdAsync)} - Request received. Payload: {JsonSerializer.Serialize(request)}");
if (string.IsNullOrWhiteSpace(request.MongoId))
return BadRequest("FurnitureBase MongoId is required.");
return await Handle(() => inventoryClient.GetByIdAsync(TrackingId.ToString(), request, cancellationToken));
}
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(GetByIdAsync)} - An error occurred. Payload: {JsonSerializer.Serialize(request)}");
throw;
}
}
///
/// Creates a new furniture base record.
///
[HttpPost("Create")]
[ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status201Created)]
public async Task CreateAsync([FromBody] CreateFurnitureBaseRequest newFurniture, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(CreateAsync)} - Request received. Payload: {JsonSerializer.Serialize(newFurniture)}");
if (newFurniture == null) return BadRequest("Invalid furniture object");
if (string.IsNullOrEmpty(newFurniture.ModelName)) return BadRequest("Invalid furniture name");
if (string.IsNullOrEmpty(newFurniture.Material)) return BadRequest("Invalid furniture material");
if (string.IsNullOrEmpty(newFurniture.Condition)) return BadRequest("Invalid furniture condition");
return await Handle(() => inventoryClient.CreateAsync(TrackingId.ToString(), newFurniture, cancellationToken));
}
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(CreateAsync)} - An error occurred. Payload: {JsonSerializer.Serialize(newFurniture)}");
throw;
}
}
///
/// Updates a furniture base record.
///
[HttpPut("Update")]
[ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status200OK)]
public async Task UpdateAsync([FromBody] UpdateFurnitureBaseRequest newFurniture, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(UpdateAsync)} - Request received. Payload: {JsonSerializer.Serialize(newFurniture)}");
if (newFurniture == null) return BadRequest("Invalid furniture object");
if (string.IsNullOrEmpty(newFurniture.ModelName)) return BadRequest("Invalid furniture model name");
if (string.IsNullOrEmpty(newFurniture.Material)) return BadRequest("Invalid furniture material");
if (string.IsNullOrEmpty(newFurniture.Condition)) return BadRequest("Invalid furniture condition");
if (string.IsNullOrWhiteSpace(newFurniture.Id)) return BadRequest("Id is required.");
return await Handle(() => inventoryClient.UpdateFurnitureBaseAsync(TrackingId.ToString(), newFurniture, cancellationToken));
}
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(UpdateAsync)} - An error occurred. Payload: {JsonSerializer.Serialize(newFurniture)}");
throw;
}
}
///
/// Changes the status of a furniture base.
///
[HttpPatch("ChangeStatus")]
[ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status200OK)]
public async Task ChangeStatusAsync([FromBody] ChangeFurnitureBaseStatusRequest request, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(ChangeStatusAsync)} - Request received. Payload: {JsonSerializer.Serialize(request)}");
if (string.IsNullOrWhiteSpace(request.MongoId)) return BadRequest("Id is required.");
return await Handle(() => inventoryClient.ChangeFurnitureBaseStatusAsync(TrackingId.ToString(), request, cancellationToken));
}
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(ChangeStatusAsync)} - An error occurred. Payload: {JsonSerializer.Serialize(request)}");
throw;
}
}
}
}