164 lines
7.8 KiB
C#
164 lines
7.8 KiB
C#
using Core.Blueprint.API.Controllers;
|
|
using Core.Blueprint.External.Clients.Blueprint;
|
|
using Core.Blueprint.External.Clients.Blueprint.Requests.SQL;
|
|
using Lib.Architecture.BuildingBlocks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Text.Json;
|
|
|
|
namespace Core.SqlSample.API.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Handles all requests for sql sample.
|
|
/// </summary>
|
|
[ApiVersion("1.0")]
|
|
//[Route("api/v{version:apiVersion}/[controller]")]
|
|
[Consumes("application/json")]
|
|
[Produces("application/json")]
|
|
[ApiController]
|
|
public class SqlSampleController(IBlueprintServiceClient blueprintServiceClient, ILogger<SqlSampleController> logger) : BaseController(logger)
|
|
{
|
|
/// <summary>
|
|
/// Creates a new sql sample.
|
|
/// </summary>
|
|
[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<IActionResult> CreateSqlSampleService(CreateSqlSampleRequest sample, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation($"{nameof(CreateSqlSampleService)} - Request received - Payload: {JsonSerializer.Serialize(sample)}");
|
|
|
|
if (sample == null) return BadRequest("Invalid sql sample object");
|
|
|
|
if (string.IsNullOrEmpty(sample.Name)) return BadRequest("Invalid sample name");
|
|
|
|
if (string.IsNullOrEmpty(sample.Description)) return BadRequest("Invalid sample description");
|
|
|
|
|
|
return await Handle(() => blueprintServiceClient.CreateSqlSampleService(sample, cancellationToken)).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError($"{nameof(CreateSqlSampleService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(sample)}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets all sql samples.
|
|
/// </summary>
|
|
[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<IActionResult> GetAllSqlSamplesService(CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation($"{nameof(GetAllSqlSamplesService)} - Request received - Payload: ");
|
|
|
|
return await Handle(() => blueprintServiceClient.GetAllSqlSamplesService(cancellationToken)).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError($"{nameof(GetAllSqlSamplesService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the sql sample by identifier.
|
|
/// </summary>
|
|
[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<IActionResult> GetSqlSampleByIdService(GetSqlSampleRequest request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation($"{nameof(GetSqlSampleByIdService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
|
|
|
|
if (request.Id <= 0) return BadRequest("Invalid sql sample identifier");
|
|
|
|
return await Handle(() => blueprintServiceClient.GetSqlSampleByIdService(request, cancellationToken)).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError($"{nameof(GetSqlSampleByIdService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates a full sql sample by identifier.
|
|
/// </summary>
|
|
[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<IActionResult> UpdateSqlSampleService(UpdateSqlSampleRequest request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation($"{nameof(UpdateSqlSampleService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
|
|
|
|
if (request == null) return BadRequest("Invalid sql sample object");
|
|
|
|
if (string.IsNullOrEmpty(request.Name)) return BadRequest("Invalid sql sample name");
|
|
|
|
if (string.IsNullOrEmpty(request.Description)) return BadRequest("Invalid sql sample description");
|
|
|
|
|
|
return await Handle(() => blueprintServiceClient.UpdateSqlSampleService(request, cancellationToken)).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError($"{nameof(UpdateSqlSampleService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes the sql sample by identifier.
|
|
/// </summary>
|
|
[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<IActionResult> DeleteSqlSampleService(DeleteSqlSampleRequest request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation($"{nameof(DeleteSqlSampleService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
|
|
|
|
if (request.Id <= 0) return BadRequest("Invalid sql sample identifier");
|
|
|
|
return await Handle(() => blueprintServiceClient.DeleteSqlSampleService(request, cancellationToken)).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError($"{nameof(DeleteSqlSampleService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|