diff --git a/src/Microservices/Services.Product/ClassifiedAds.Services.Product.Api/Controllers/ProductsController.cs b/src/Microservices/Services.Product/ClassifiedAds.Services.Product.Api/Controllers/ProductsController.cs deleted file mode 100644 index 78125f5cf..000000000 --- a/src/Microservices/Services.Product/ClassifiedAds.Services.Product.Api/Controllers/ProductsController.cs +++ /dev/null @@ -1,34 +0,0 @@ -using ClassifiedAds.CrossCuttingConcerns.Csv; -using ClassifiedAds.Services.Product.Models; -using ClassifiedAds.Services.Product.RateLimiterPolicies; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.RateLimiting; - -namespace ClassifiedAds.Services.Product.Controllers; - -[EnableRateLimiting(RateLimiterPolicyNames.DefaultPolicy)] -[Authorize] -[Produces("application/json")] -[Route("api/[controller]")] -[ApiController] -public class ProductsController : ControllerBase -{ - private readonly ICsvReader _productCsvReader; - - public ProductsController( - ICsvReader productCsvReader) - { - _productCsvReader = productCsvReader; - } - - [HttpPost("importcsv")] - public IActionResult ImportCsv([FromForm] UploadFileModel model) - { - using var stream = model.FormFile.OpenReadStream(); - var products = _productCsvReader.Read(stream); - - // TODO: import to database - return Ok(products); - } -} \ No newline at end of file diff --git a/src/Microservices/Services.Product/ClassifiedAds.Services.Product.Api/Endpoints/ImportCsvRequest.cs b/src/Microservices/Services.Product/ClassifiedAds.Services.Product.Api/Endpoints/ImportCsvRequest.cs new file mode 100644 index 000000000..d643688c7 --- /dev/null +++ b/src/Microservices/Services.Product/ClassifiedAds.Services.Product.Api/Endpoints/ImportCsvRequest.cs @@ -0,0 +1,45 @@ +using ClassifiedAds.CrossCuttingConcerns.Csv; +using ClassifiedAds.Infrastructure.Web.MinimalApis; +using ClassifiedAds.Services.Product.Models; +using ClassifiedAds.Services.Product.RateLimiterPolicies; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Routing; +using Microsoft.OpenApi.Models; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace ClassifiedAds.Services.Product.Api.Endpoints; + +public class ImportCsvRequest +{ + public IFormFile FormFile { get; set; } +} + +public class ImportCsvRequestHandler : IEndpointHandler +{ + public static void MapEndpoint(IEndpointRouteBuilder builder) + { + builder.MapPost("api/products/importcsv", HandleAsync) + .RequireAuthorization() + .RequireRateLimiting(RateLimiterPolicyNames.DefaultPolicy) + .WithName("ImportCsv") + .Produces(StatusCodes.Status200OK, contentType: "application/json") + .ProducesProblem(StatusCodes.Status400BadRequest) + .WithOpenApi(operation => new OpenApiOperation(operation) + { + Tags = new List { new OpenApiTag { Name = "Products" } } + }) + .DisableAntiforgery(); + } + + private static Task HandleAsync(ICsvReader productCsvReader, [FromForm] ImportCsvRequest request) + { + using var stream = request.FormFile.OpenReadStream(); + var products = productCsvReader.Read(stream); + + // TODO: import to database + return Task.FromResult(Results.Ok(products)); + } +} \ No newline at end of file diff --git a/src/Microservices/Services.Product/ClassifiedAds.Services.Product.Api/Models/UploadFileModel.cs b/src/Microservices/Services.Product/ClassifiedAds.Services.Product.Api/Models/UploadFileModel.cs deleted file mode 100644 index 32c58996d..000000000 --- a/src/Microservices/Services.Product/ClassifiedAds.Services.Product.Api/Models/UploadFileModel.cs +++ /dev/null @@ -1,8 +0,0 @@ -using Microsoft.AspNetCore.Http; - -namespace ClassifiedAds.Services.Product.Models; - -public class UploadFileModel -{ - public IFormFile FormFile { get; set; } -}