-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net 8 - [FromForm] with Minimal API
- Loading branch information
1 parent
bc5bb36
commit eeae3cc
Showing
3 changed files
with
45 additions
and
42 deletions.
There are no files selected for viewing
34 changes: 0 additions & 34 deletions
34
...ces/Services.Product/ClassifiedAds.Services.Product.Api/Controllers/ProductsController.cs
This file was deleted.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
...ervices/Services.Product/ClassifiedAds.Services.Product.Api/Endpoints/ImportCsvRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CreateProductResponse>(StatusCodes.Status200OK, contentType: "application/json") | ||
.ProducesProblem(StatusCodes.Status400BadRequest) | ||
.WithOpenApi(operation => new OpenApiOperation(operation) | ||
{ | ||
Tags = new List<OpenApiTag> { new OpenApiTag { Name = "Products" } } | ||
}) | ||
.DisableAntiforgery(); | ||
} | ||
|
||
private static Task<IResult> HandleAsync(ICsvReader<ProductModel> 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)); | ||
} | ||
} |
8 changes: 0 additions & 8 deletions
8
...croservices/Services.Product/ClassifiedAds.Services.Product.Api/Models/UploadFileModel.cs
This file was deleted.
Oops, something went wrong.