166 lines
8.2 KiB
C#
166 lines
8.2 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.UserProject.API.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Handles all requests for user project.
|
|
/// </summary>
|
|
[ApiVersion("1.0")]
|
|
//[Route("api/v{version:apiVersion}/[controller]")]
|
|
[Consumes("application/json")]
|
|
[Produces("application/json")]
|
|
[ApiController]
|
|
public class SQLUserProjectController(IBlueprintServiceClient blueprintServiceClient, ILogger<SQLUserProjectController> logger) : BaseController(logger)
|
|
{
|
|
/// <summary>
|
|
/// Creates a new user project.
|
|
/// </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> CreateUserProjectService(CreateUserProjectRequest newUserProject, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation($"{nameof(CreateUserProjectService)} - Request received - Payload: {JsonSerializer.Serialize(newUserProject)}");
|
|
|
|
if (newUserProject == null) return BadRequest("Invalid user project object");
|
|
|
|
if (string.IsNullOrEmpty(newUserProject.ProjectCode)) return BadRequest("Invalid project code");
|
|
|
|
if (string.IsNullOrEmpty(newUserProject.ProjectDescription)) return BadRequest("Invalid project description");
|
|
|
|
if (string.IsNullOrEmpty(newUserProject.UserId)) return BadRequest("Invalid user identifier");
|
|
|
|
return await Handle(() => blueprintServiceClient.CreateUserProjectService(newUserProject, cancellationToken)).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError($"{nameof(CreateUserProjectService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newUserProject)}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets all user projects.
|
|
/// </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> GetAllUserProjectsService(CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation($"{nameof(GetAllUserProjectsService)} - Request received - Payload: ");
|
|
|
|
return await Handle(() => blueprintServiceClient.GetAllUserProjectsService(cancellationToken)).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError($"{nameof(GetAllUserProjectsService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the user project 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> GetUserProjectByIdService(GetUserProjectRequest request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation($"{nameof(GetUserProjectByIdService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
|
|
|
|
if (request.Id <= 0) return BadRequest("Invalid user project identifier");
|
|
|
|
return await Handle(() => blueprintServiceClient.GetUserProjectByIdService(request, cancellationToken)).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError($"{nameof(GetUserProjectByIdService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates a full user project 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> UpdateUserProjectService(UpdateUserProjectRequest request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation($"{nameof(UpdateUserProjectService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
|
|
|
|
if (request == null) return BadRequest("Invalid user project object");
|
|
|
|
if (string.IsNullOrEmpty(request.ProjectCode)) return BadRequest("Invalid user project code");
|
|
|
|
if (string.IsNullOrEmpty(request.ProjectDescription)) return BadRequest("Invalid user project description");
|
|
|
|
if (string.IsNullOrEmpty(request.UserId)) return BadRequest("Invalid user identifier");
|
|
|
|
return await Handle(() => blueprintServiceClient.UpdateUserProjectService(request, cancellationToken)).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError($"{nameof(UpdateUserProjectService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes the user project 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> DeleteUserProjectService(DeleteUserProjectRequest request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation($"{nameof(DeleteUserProjectService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
|
|
|
|
if (request.Id <= 0) return BadRequest("Invalid user project identifier");
|
|
|
|
return await Handle(() => blueprintServiceClient.DeleteUserProjectService(request, cancellationToken)).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError($"{nameof(DeleteUserProjectService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|