Skip to content

Commit

Permalink
.Net 8 - [FromForm] with Minimal API
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed May 16, 2024
1 parent bc5bb36 commit eeae3cc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.

This file was deleted.

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));
}
}

This file was deleted.

0 comments on commit eeae3cc

Please sign in to comment.