143 lines
6.1 KiB
C#
143 lines
6.1 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// Handles all requests for furniture base operations.
|
|
/// </summary>
|
|
[ApiVersion("1.0")]
|
|
[Route("api/v{version:apiVersion}/[controller]")]
|
|
[ApiController]
|
|
[Consumes("application/json")]
|
|
[Produces("application/json")]
|
|
public class FurnitureBaseController(IInventoryServiceClient inventoryClient, ILogger<FurnitureBaseController> logger) : BaseController(logger)
|
|
{
|
|
/// <summary>
|
|
/// Gets all furniture base records.
|
|
/// </summary>
|
|
[HttpGet("GetAll")]
|
|
[ProducesResponseType(typeof(IEnumerable<FurnitureBase>), StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> GetAllAsync(CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation($"{nameof(GetAllAsync)} - Request received.");
|
|
return await Handle(() => inventoryClient.GetAllAsync(TrackingId.ToString(), cancellationToken));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, $"{nameof(GetAllAsync)} - An error occurred.");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a furniture base by its identifier.
|
|
/// </summary>
|
|
[HttpPost("GetById")]
|
|
[ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> 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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new furniture base record.
|
|
/// </summary>
|
|
[HttpPost("Create")]
|
|
[ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status201Created)]
|
|
public async Task<IActionResult> 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.Name)) 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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates a furniture base record.
|
|
/// </summary>
|
|
[HttpPut("Update")]
|
|
[ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> 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.Name)) 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");
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Changes the status of a furniture base.
|
|
/// </summary>
|
|
[HttpPatch("ChangeStatus")]
|
|
[ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> 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;
|
|
}
|
|
}
|
|
}
|
|
}
|