using Core.Blueprint.API.Controllers; using Core.Blueprint.External.Clients.Blueprint; using Core.Blueprint.External.Clients.Blueprint.Requests.BlobStorage; using Lib.Architecture.BuildingBlocks; using Microsoft.AspNetCore.Mvc; using System.Text.Json; namespace Core.Blob.API.Controllers { /// /// Handles all requests for blob. /// [ApiVersion("1.0")] //[Route("api/v{version:apiVersion}/[controller]")] [Consumes("application/json")] [Produces("application/json")] [ApiController] public class BlobStorageController(IBlueprintServiceClient blueprintServiceClient, ILogger logger) : BaseController(logger) { /// /// Uploads a new blob. /// [HttpPost("UploadBlob")] [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 UploadBlobService(UploadBlobRequest newBlob, CancellationToken cancellationToken) { try { logger.LogInformation($"{nameof(UploadBlobService)} - Request received - Payload: {JsonSerializer.Serialize(newBlob)}"); if (newBlob == null) return BadRequest("Invalid blob object"); if (string.IsNullOrEmpty(newBlob.BlobName)) return BadRequest("Invalid blob name"); if (newBlob.BlobContent is null) return BadRequest("Invalid blob content"); return await Handle(() => blueprintServiceClient.UploadBlobService(newBlob, cancellationToken)).ConfigureAwait(false); } catch (Exception ex) { logger.LogError($"{nameof(UploadBlobService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newBlob)}"); throw; } } /// /// Gets all blobs into the container. /// [HttpGet("GetBlobList")] [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 GetBlobListService([FromQuery] string? prefix, CancellationToken cancellationToken) { try { logger.LogInformation($"{nameof(GetBlobListService)} - Request received - Payload: "); return await Handle(() => blueprintServiceClient.GetBlobListAsync(prefix, cancellationToken)).ConfigureAwait(false); } catch (Exception ex) { logger.LogError($"{nameof(GetBlobListService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload"); throw; } } /// /// Donwloads a blob by name. /// [HttpPost("DownloadBlob")] [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 DownloadBlobService(DownloadBlobRequest request, CancellationToken cancellationToken) { try { logger.LogInformation($"{nameof(DownloadBlobService)} - Request received - Payload: {JsonSerializer.Serialize(request)}"); if (string.IsNullOrEmpty(request.BlobName)) return BadRequest("Invalid blob name"); return await Handle(() => blueprintServiceClient.DownloadBlobAsync(request, cancellationToken)).ConfigureAwait(false); } catch (Exception ex) { logger.LogError($"{nameof(DownloadBlobService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}"); throw; } } /// /// Deletes the blob by identifier. /// [HttpDelete("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 DeleteBlobService([FromBody] DeleteBlobRequest request, CancellationToken cancellationToken) { try { logger.LogInformation($"{nameof(DeleteBlobService)} - Request received - Payload: {JsonSerializer.Serialize(request)}"); if (string.IsNullOrEmpty(request.BlobName)) return BadRequest("Invalid blob name"); return await Handle(() => blueprintServiceClient.DeleteBlobService(request, cancellationToken)).ConfigureAwait(false); } catch (Exception ex) { logger.LogError($"{nameof(DeleteBlobService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}"); throw; } } } }