Skip to content

Commit fd4fe70

Browse files
committed
Some IEnumerable/IAsyncEnumerable changes
1 parent 52f8a53 commit fd4fe70

5 files changed

Lines changed: 14 additions & 10 deletions

File tree

API/Controller/Admin/Configuration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public sealed partial class AdminController
1616
/// <response code="200"></response>
1717
/// <response code="401">Unauthorized</response>
1818
[HttpGet("config")]
19-
[ProducesResponseType<IAsyncEnumerable<ConfigurationItemDto>>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)] // Ok
19+
[ProducesResponseType<ConfigurationItemDto[]>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)] // Ok
2020
public IAsyncEnumerable<ConfigurationItemDto> ConfigurationList([FromServices] IConfigurationService configurationService)
2121
{
2222
return configurationService

API/Controller/Admin/GetOnlineDevices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public sealed partial class AdminController
1717
/// <response code="200">All online devices</response>
1818
/// <response code="401">Unauthorized</response>
1919
[HttpGet("monitoring/onlineDevices")]
20-
[ProducesResponseType<LegacyDataResponse<IEnumerable<AdminOnlineDeviceResponse>>>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
20+
[ProducesResponseType<LegacyDataResponse<AdminOnlineDeviceResponse[]>>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
2121
public async Task<IActionResult> GetOnlineDevices()
2222
{
2323
var devicesOnline = _redis.RedisCollection<DeviceOnline>(false);
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using System.Net.Mime;
2+
using System.Runtime.CompilerServices;
3+
using Microsoft.AspNetCore.Mvc;
24
using OpenShock.API.Models.Response;
35

46
namespace OpenShock.API.Controller.Sessions;
57

68
public sealed partial class SessionsController
79
{
810
[HttpGet]
9-
public async Task<IEnumerable<LoginSessionResponse>> ListSessions()
11+
[ProducesResponseType<LoginSessionResponse[]>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
12+
public async IAsyncEnumerable<LoginSessionResponse> ListSessions([EnumeratorCancellation] CancellationToken cancellationToken)
1013
{
11-
var sessions = await _sessionService.ListSessionsByUserIdAsync(CurrentUser.Id);
12-
13-
return sessions.Select(LoginSessionResponse.MapFrom);
14+
await foreach (var session in _sessionService.ListSessionsByUserIdAsync(CurrentUser.Id).WithCancellation(cancellationToken))
15+
{
16+
yield return LoginSessionResponse.MapFrom(session);
17+
}
1418
}
1519
}

Common/Services/Session/ISessionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public interface ISessionService
66
{
77
public Task<CreateSessionResult> CreateSessionAsync(Guid userId, string userAgent, string ipAddress);
88

9-
public Task<IReadOnlyList<LoginSession>> ListSessionsByUserIdAsync(Guid userId);
9+
public IAsyncEnumerable<LoginSession> ListSessionsByUserIdAsync(Guid userId);
1010

1111
public Task<LoginSession?> GetSessionByTokenAsync(string sessionToken);
1212

Common/Services/Session/SessionService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ await _loginSessions.InsertAsync(new LoginSession
4343
return new CreateSessionResult(id, token);
4444
}
4545

46-
public async Task<IReadOnlyList<LoginSession>> ListSessionsByUserIdAsync(Guid userId)
46+
public IAsyncEnumerable<LoginSession> ListSessionsByUserIdAsync(Guid userId)
4747
{
48-
return await _loginSessions.Where(x => x.UserId == userId).ToArrayAsync();
48+
return _loginSessions.Where(x => x.UserId == userId);
4949
}
5050

5151
public async Task<LoginSession?> GetSessionByTokenAsync(string sessionToken)

0 commit comments

Comments
 (0)