using Core.Blueprint.External.Clients.Blueprint;
using Core.Blueprint.External.Clients.Blueprint.Requests.Mongo;
using Lib.Architecture.BuildingBlocks;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
namespace Core.Blueprint.API.Controllers
{
///
/// Handles all requests for blueprint.
///
[ApiVersion("1.0")]
//[Route("api/v{version:apiVersion}/[controller]")]
[Consumes("application/json")]
[Produces("application/json")]
[ApiController]
public class MongoBlueprintController(IBlueprintServiceClient blueprintServiceClient, ILogger logger) : BaseController(logger)
{
///
/// Creates a new blueprint.
///
[HttpPost("Create")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task CreateBlueprintService(CreateBlueprintRequest newBlueprint, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(CreateBlueprintService)} - Request received - Payload: {JsonSerializer.Serialize(newBlueprint)}");
if (newBlueprint == null) return BadRequest("Invalid blueprint object");
if (string.IsNullOrEmpty(newBlueprint.Name)) return BadRequest("Invalid blueprint name");
if (string.IsNullOrEmpty(newBlueprint.Description)) return BadRequest("Invalid blueprint description");
return await Handle(() => blueprintServiceClient.CreateBlueprintService(newBlueprint, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(CreateBlueprintService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newBlueprint)}");
throw;
}
}
///
/// Gets all blueprints.
///
[HttpGet("GetAll")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task GetAllBlueprintsService(CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(GetAllBlueprintsService)} - Request received - Payload: ");
return await Handle(() => blueprintServiceClient.GetAllBlueprintsService(cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(GetAllBlueprintsService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload");
throw;
}
}
///
/// Gets the blueprint by identifier.
///
[HttpPost("GetById")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task GetBlueprintByIdService(GetBlueprintRequest request, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(GetBlueprintByIdService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
if (string.IsNullOrEmpty(request._Id)) return BadRequest("Invalid blueprint identifier");
return await Handle(() => blueprintServiceClient.GetBlueprintByIdService(request, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(GetBlueprintByIdService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
throw;
}
}
///
/// Updates a full blueprint by identifier.
///
[HttpPut("Update")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task UpdateBlueprintService(UpdateBlueprintRequest newBlueprint, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(UpdateBlueprintService)} - Request received - Payload: {JsonSerializer.Serialize(newBlueprint)}");
if (newBlueprint == null) return BadRequest("Invalid blueprint object");
if (string.IsNullOrEmpty(newBlueprint.Name)) return BadRequest("Invalid blueprint name");
if (string.IsNullOrEmpty(newBlueprint.Description)) return BadRequest("Invalid blueprint description");
return await Handle(() => blueprintServiceClient.UpdateBlueprintService(newBlueprint, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(UpdateBlueprintService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newBlueprint)}");
throw;
}
}
///
/// Deletes the blueprint by identifier.
///
[HttpPost("Delete")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task DeleteBlueprintService(DeleteBlueprintRequest request, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(DeleteBlueprintService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
if (string.IsNullOrEmpty(request._Id)) return BadRequest("Invalid blueprint identifier");
return await Handle(() => blueprintServiceClient.DeleteBlueprintService(request, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(DeleteBlueprintService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
throw;
}
}
}
}