119 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			119 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Asp.Versioning;
 | |
| using Core.Blueprint.Application.UsesCases.BlobStorage.Ports;
 | |
| using Core.Blueprint.Application.UsesCases.KeyVault.Input;
 | |
| using Lib.Architecture.BuildingBlocks;
 | |
| using Microsoft.AspNetCore.Mvc;
 | |
| 
 | |
| namespace Core.Cerberos.Service.API.Controllers
 | |
| {
 | |
|     /// <summary>
 | |
|     /// Handles all services and business rules related to <see cref="MStorageController"/>.
 | |
|     /// </summary>
 | |
|     [ApiVersion("1.0")]
 | |
|     [Route("api/v{api-version:apiVersion}/[controller]")]
 | |
|     [Produces("application/json")]
 | |
|     [ApiController]
 | |
|     public class BlobStorageController : ControllerBase
 | |
|     {
 | |
|         private readonly IComponentHandler<UploadBlobRequest> uploadBlobHandler;
 | |
|         private readonly IComponentHandler<GetBlobListRequest> getBlobListHandler;
 | |
|         private readonly IComponentHandler<DownloadBlobRequest> downloadBlobHandler;
 | |
|         private readonly IComponentHandler<DeleteBlobRequest> deleteBlobHandler;
 | |
|         private readonly IStoragePort port;
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Handles all services and business rules related to <see cref="MStorageController"/>.
 | |
|         /// </summary>
 | |
|         public BlobStorageController(
 | |
|             IComponentHandler<UploadBlobRequest> uploadBlobHandler,
 | |
|             IComponentHandler<GetBlobListRequest> getBlobListHandler,
 | |
|             IComponentHandler<DownloadBlobRequest> downloadBlobHandler,
 | |
|             IComponentHandler<DeleteBlobRequest> deleteBlobHandler,
 | |
|             IStoragePort port
 | |
|             )
 | |
|         {
 | |
|             this.uploadBlobHandler = uploadBlobHandler;
 | |
|             this.getBlobListHandler = getBlobListHandler;
 | |
|             this.downloadBlobHandler = downloadBlobHandler;
 | |
|             this.deleteBlobHandler = deleteBlobHandler;
 | |
|             this.port = port;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Uploads a new blob.
 | |
|         /// </summary>
 | |
|         [HttpPost("UploadBlob")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status204NoContent)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         public async Task<IActionResult> UploadBlobAsync([FromBody] UploadBlobRequest newBlob, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             await uploadBlobHandler.ExecuteAsync(newBlob, cancellationToken).ConfigureAwait(false);
 | |
|             return port.ViewModel;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets all blobs into the container.
 | |
|         /// </summary>
 | |
|         [HttpGet]
 | |
|         [Route("GetBlobList")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status204NoContent)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         public async Task<IActionResult> GetBlobList([FromQuery] string? prefix, CancellationToken cancellationToken)
 | |
|         {
 | |
|             await getBlobListHandler.ExecuteAsync(new GetBlobListRequest { Prefix = prefix }, cancellationToken).ConfigureAwait(false);
 | |
|             return port.ViewModel;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Downloads a blob by name.
 | |
|         /// </summary>
 | |
|         [HttpPost("DownloadBlob")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status204NoContent)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         public async Task<IActionResult> DownloadBlobAsync([FromBody] DownloadBlobRequest request, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             if (string.IsNullOrEmpty(request.BlobName)) { return BadRequest("Invalid blob name"); }
 | |
| 
 | |
|             await downloadBlobHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
|             return port.ViewModel;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Deletes a blob by name.
 | |
|         /// </summary>
 | |
|         [HttpDelete]
 | |
|         [Route("DeleteBlob")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status204NoContent)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         public async Task<IActionResult> DeleteBlobAsync([FromBody] DeleteBlobRequest request,
 | |
|                                                                      CancellationToken cancellationToken)
 | |
|         {
 | |
|             if (string.IsNullOrEmpty(request.BlobName)) { return BadRequest("Invalid blob name"); }
 | |
| 
 | |
|             await deleteBlobHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
|             return port.ViewModel;
 | |
|         }
 | |
|     }
 | |
| } | 
