-
-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathListJsonFile.cs
31 lines (26 loc) · 851 Bytes
/
ListJsonFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System.Text.Json;
using Ardalis.ApiEndpoints;
using Microsoft.AspNetCore.Mvc;
using SampleEndpointApp.DomainModel;
namespace SampleEndpointApp.Endpoints.Authors;
public class ListJsonFile : EndpointBaseAsync
.WithoutRequest
.WithActionResult
{
private readonly IAsyncRepository<Author> repository;
public ListJsonFile(IAsyncRepository<Author> repository)
{
this.repository = repository;
}
/// <summary>
/// List all Authors as a JSON file
/// </summary>
[HttpGet("api/[namespace]/Json")]
public override async Task<ActionResult> HandleAsync(
CancellationToken cancellationToken = default)
{
var result = (await repository.ListAllAsync(cancellationToken)).ToList();
var streamData = JsonSerializer.SerializeToUtf8Bytes(result);
return File(streamData, "text/json", "authors.json");
}
}