Add pagination for API GetNotifications (#3145)

This commit is contained in:
Samuel Adams
2021-11-26 04:55:59 +02:00
committed by GitHub
parent 57852821f5
commit 9b730e784f
4 changed files with 41 additions and 12 deletions

View File

@@ -9,13 +9,22 @@ namespace BTCPayServer.Client
{ {
public partial class BTCPayServerClient public partial class BTCPayServerClient
{ {
public virtual async Task<IEnumerable<NotificationData>> GetNotifications(bool? seen = null, public virtual async Task<IEnumerable<NotificationData>> GetNotifications(bool? seen = null, int? skip = null,
CancellationToken token = default) int? take = null, CancellationToken token = default)
{ {
var response = Dictionary<string, object> queryPayload = new Dictionary<string, object>();
await _httpClient.SendAsync(
CreateHttpRequest($"api/v1/users/me/notifications", if (seen != null)
seen != null ? new Dictionary<string, object>() {{nameof(seen), seen}} : null), token); queryPayload.Add(nameof(seen), seen);
if (skip != null)
queryPayload.Add(nameof(skip), skip);
if (take != null)
queryPayload.Add(nameof(take), take);
var response = await _httpClient.SendAsync(
CreateHttpRequest($"api/v1/users/me/notifications",
queryPayload), token);
return await HandleResponse<IEnumerable<NotificationData>>(response); return await HandleResponse<IEnumerable<NotificationData>>(response);
} }

View File

@@ -612,12 +612,12 @@ namespace BTCPayServer.Controllers.GreenField
{ {
HandleActionResult(await _apiKeysController.RevokeKey(apikey)); HandleActionResult(await _apiKeysController.RevokeKey(apikey));
} }
public override async Task<IEnumerable<NotificationData>> GetNotifications(bool? seen = null, public override async Task<IEnumerable<NotificationData>> GetNotifications(bool? seen = null,
CancellationToken token = default) int? skip = null, int? take = null, CancellationToken token = default)
{ {
return GetFromActionResult<IEnumerable<NotificationData>>( return GetFromActionResult<IEnumerable<NotificationData>>(
await _notificationsController.GetNotifications(seen)); await _notificationsController.GetNotifications(seen, skip, take));
} }
public override async Task<NotificationData> GetNotification(string notificationId, public override async Task<NotificationData> GetNotification(string notificationId,

View File

@@ -33,11 +33,11 @@ namespace BTCPayServer.Controllers.GreenField
[Authorize(Policy = Policies.CanViewNotificationsForUser, [Authorize(Policy = Policies.CanViewNotificationsForUser,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/users/me/notifications")] [HttpGet("~/api/v1/users/me/notifications")]
public async Task<IActionResult> GetNotifications(bool? seen = null) public async Task<IActionResult> GetNotifications(bool? seen = null, [FromQuery] int? skip = null, [FromQuery] int? take = null)
{ {
var items = await _notificationManager.GetNotifications(new NotificationsQuery() var items = await _notificationManager.GetNotifications(new NotificationsQuery()
{ {
Seen = seen, UserId = _userManager.GetUserId(User) Seen = seen, UserId = _userManager.GetUserId(User), Skip = skip, Take = take
}); });
return Ok(items.Items.Select(ToModel)); return Ok(items.Items.Select(ToModel));

View File

@@ -16,6 +16,26 @@
"type": "string", "type": "string",
"nullable": true "nullable": true
} }
},
{
"name": "skip",
"in": "query",
"required": false,
"description": "Number of records to skip",
"schema": {
"nullable": true,
"type": "number"
}
},
{
"name": "take",
"in": "query",
"required": false,
"description": "Number of records returned in response",
"schema": {
"nullable": true,
"type": "number"
}
} }
], ],
"description": "View current user's notifications", "description": "View current user's notifications",