-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
Stream.cs
37 lines (33 loc) · 1.03 KB
/
Stream.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
32
33
34
35
36
37
using System.Runtime.CompilerServices;
using Ardalis.ApiEndpoints;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using SampleEndpointApp.DomainModel;
namespace SampleEndpointApp.Endpoints.Authors;
public class Stream : EndpointBaseAsync
.WithoutRequest
.WithAsyncEnumerableResult<AuthorListResult>
{
private readonly IAsyncRepository<Author> repository;
private readonly IMapper mapper;
public Stream(
IAsyncRepository<Author> repository,
IMapper mapper)
{
this.repository = repository;
this.mapper = mapper;
}
/// <summary>
/// Stream all authors with a one second delay between entries
/// </summary>
[HttpGet("api/[namespace]/stream")]
public override async IAsyncEnumerable<AuthorListResult> HandleAsync([EnumeratorCancellation] CancellationToken cancellationToken)
{
var result = await repository.ListAllAsync(cancellationToken);
foreach (var author in result)
{
yield return mapper.Map<AuthorListResult>(author);
await Task.Delay(1000, cancellationToken);
}
}
}