Skip to content

Commit f0a9dec

Browse files
committed
AsyncEnumerable responses and doc cleanup
1 parent f8f4b80 commit f0a9dec

5 files changed

Lines changed: 16 additions & 15 deletions

File tree

API/Controller/Account/Activate.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Net.Mime;
22
using Microsoft.AspNetCore.Mvc;
33
using Asp.Versioning;
4-
using OpenShock.API.Models.Requests;
4+
using OpenShock.Common.Errors;
55
using OpenShock.Common.Problems;
66

77
namespace OpenShock.API.Controller.Account;
@@ -14,12 +14,12 @@ public sealed partial class AccountController
1414
/// <response code="200"></response>
1515
[HttpPost("activate")]
1616
[ProducesResponseType(StatusCodes.Status200OK)]
17-
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status403Forbidden, MediaTypeNames.Application.ProblemJson)]
17+
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status400BadRequest, MediaTypeNames.Application.ProblemJson)]
1818
[MapToApiVersion("1")]
1919
public async Task<IActionResult> Activate([FromQuery(Name = "token")] string token, CancellationToken cancellationToken)
2020
{
2121
bool ok = await _accountService.TryActivateAccountAsync(token, cancellationToken);
2222

23-
return Ok();
23+
return ok ? Ok() : Problem(AccountError.AccountActivationNotFound);
2424
}
2525
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using System.Net.Mime;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.EntityFrameworkCore;
24
using OpenShock.API.Models.Response;
35
using OpenShock.API.Services.OAuthConnection;
46

@@ -12,19 +14,18 @@ public sealed partial class AuthenticatedAccountController
1214
/// <returns>Array of connections with provider key, external id, display name and link time.</returns>
1315
/// <response code="200">Returns the list of connections.</response>
1416
[HttpGet("connections")]
15-
[ProducesResponseType(StatusCodes.Status200OK)]
16-
public async Task<OAuthConnectionResponse[]> ListOAuthConnections([FromServices] IOAuthConnectionService connectionService, CancellationToken cancellationToken)
17+
[ProducesResponseType<OAuthConnectionResponse[]>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
18+
public IActionResult ListOAuthConnections([FromServices] IOAuthConnectionService connectionService, CancellationToken cancellationToken)
1719
{
18-
var connections = await connectionService.GetConnectionsAsync(CurrentUser.Id, cancellationToken);
19-
20-
return connections
20+
return Ok(connectionService
21+
.GetConnectionsAsNonTrackingQuery(CurrentUser.Id, cancellationToken)
2122
.Select(c => new OAuthConnectionResponse
2223
{
2324
ProviderKey = c.ProviderKey,
2425
ExternalId = c.ExternalId,
2526
DisplayName = c.DisplayName,
2627
LinkedAt = c.CreatedAt
2728
})
28-
.ToArray();
29+
.AsAsyncEnumerable());
2930
}
3031
}

API/Services/OAuthConnection/IOAuthConnectionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace OpenShock.API.Services.OAuthConnection;
77
/// </summary>
88
public interface IOAuthConnectionService
99
{
10-
Task<UserOAuthConnection[]> GetConnectionsAsync(Guid userId, CancellationToken cancellationToken = default);
10+
IQueryable<UserOAuthConnection> GetConnectionsAsNonTrackingQuery(Guid userId, CancellationToken cancellationToken = default);
1111
Task<UserOAuthConnection?> GetByProviderExternalIdAsync(string provider, string providerAccountId, CancellationToken cancellationToken = default);
1212
Task<bool> ConnectionExistsAsync(string provider, string providerAccountId, CancellationToken cancellationToken = default);
1313
Task<bool> HasConnectionAsync(Guid userId, string provider, CancellationToken cancellationToken = default);

API/Services/OAuthConnection/OAuthConnectionService.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ public OAuthConnectionService(OpenShockContext db, ILogger<OAuthConnectionServic
1515
_logger = logger;
1616
}
1717

18-
public async Task<UserOAuthConnection[]> GetConnectionsAsync(Guid userId, CancellationToken cancellationToken)
18+
public IQueryable<UserOAuthConnection> GetConnectionsAsNonTrackingQuery(Guid userId, CancellationToken cancellationToken)
1919
{
20-
return await _db.UserOAuthConnections
20+
return _db.UserOAuthConnections
2121
.AsNoTracking()
22-
.Where(c => c.UserId == userId)
23-
.ToArrayAsync(cancellationToken);
22+
.Where(c => c.UserId == userId);
2423
}
2524

2625
public async Task<UserOAuthConnection?> GetByProviderExternalIdAsync(string provider, string providerAccountId, CancellationToken cancellationToken)

Common/Errors/AccountError.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static class AccountError
2626
"Account.Username.RecentlyChanged", "You have recently changed your username. You can only change your username every 7 days", HttpStatusCode.Forbidden);
2727

2828
public static OpenShockProblem AccountNotActivated => new OpenShockProblem("Account.AccountNotActivated", "Your account has not been activated", HttpStatusCode.Unauthorized);
29+
public static OpenShockProblem AccountActivationNotFound => new OpenShockProblem("Account.Activation.NotFound", "There is no account activation request matching the supplied token", HttpStatusCode.BadRequest);
2930
public static OpenShockProblem AccountDeactivated => new OpenShockProblem("Account.Deactivated", "Your account has been deactivated", HttpStatusCode.Unauthorized);
3031

3132
public static OpenShockProblem AccountOAuthOnly => new OpenShockProblem("Account.OAuthOnly", "This account is only accessible via OAuth", HttpStatusCode.Unauthorized);

0 commit comments

Comments
 (0)