Skip to content

Commit 0d2eace

Browse files
committed
Add PaginatedResult and replace all Result<IEnumerable<T>> with it
#58
1 parent 466ed67 commit 0d2eace

11 files changed

Lines changed: 143 additions & 30 deletions
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
namespace FlowSynx.Client.Messages.Responses;
2+
3+
public class PaginatedResult<T> : Result<List<T>>
4+
{
5+
private PaginatedResult()
6+
{
7+
}
8+
9+
private PaginatedResult(
10+
bool succeeded,
11+
List<T>? data,
12+
List<string>? messages,
13+
int totalCount,
14+
int page,
15+
int pageSize)
16+
{
17+
Succeeded = succeeded;
18+
Messages = messages ?? new List<string>();
19+
Data = data ?? new List<T>();
20+
TotalCount = Math.Max(totalCount, 0);
21+
PageSize = NormalizePageSize(pageSize, TotalCount);
22+
CurrentPage = NormalizePage(page);
23+
TotalPages = PageSize == 0
24+
? 0
25+
: (int)Math.Ceiling(TotalCount / (double)PageSize);
26+
}
27+
28+
public int CurrentPage { get; private set; }
29+
30+
public int TotalPages { get; private set; }
31+
32+
public int TotalCount { get; private set; }
33+
34+
public int PageSize { get; private set; }
35+
36+
public bool HasPreviousPage => CurrentPage > 1;
37+
38+
public bool HasNextPage => CurrentPage < TotalPages;
39+
40+
public static PaginatedResult<T> Success(
41+
List<T> data,
42+
int totalCount,
43+
int page,
44+
int pageSize)
45+
{
46+
return new PaginatedResult<T>(true, data, null, totalCount, page, pageSize);
47+
}
48+
49+
public static PaginatedResult<T> Success(List<T> data)
50+
{
51+
var list = data ?? new List<T>();
52+
return new PaginatedResult<T>(true, list, null, list.Count, 1, list.Count == 0 ? 1 : list.Count);
53+
}
54+
55+
public static PaginatedResult<T> Failure()
56+
{
57+
return new PaginatedResult<T>(false, default, null, 0, 1, 1);
58+
}
59+
60+
public static PaginatedResult<T> Failure(string message)
61+
{
62+
return new PaginatedResult<T>(false, default, new List<string> { message }, 0, 1, 1);
63+
}
64+
65+
public static PaginatedResult<T> Failure(List<string> messages)
66+
{
67+
return new PaginatedResult<T>(false, default, messages, 0, 1, 1);
68+
}
69+
70+
public static Task<PaginatedResult<T>> SuccessAsync(
71+
List<T> data,
72+
int totalCount,
73+
int page,
74+
int pageSize)
75+
{
76+
return Task.FromResult(Success(data, totalCount, page, pageSize));
77+
}
78+
79+
public static Task<PaginatedResult<T>> SuccessAsync(List<T> data)
80+
{
81+
return Task.FromResult(Success(data));
82+
}
83+
84+
public static Task<PaginatedResult<T>> FailureAsync()
85+
{
86+
return Task.FromResult(Failure());
87+
}
88+
89+
public static Task<PaginatedResult<T>> FailureAsync(string message)
90+
{
91+
return Task.FromResult(Failure(message));
92+
}
93+
94+
public static Task<PaginatedResult<T>> FailureAsync(List<string> messages)
95+
{
96+
return Task.FromResult(Failure(messages));
97+
}
98+
99+
private static int NormalizePage(int page)
100+
{
101+
return page < 1 ? 1 : page;
102+
}
103+
104+
private static int NormalizePageSize(int pageSize, int totalCount)
105+
{
106+
if (pageSize < 1)
107+
{
108+
return totalCount == 0 ? 1 : totalCount;
109+
}
110+
111+
return pageSize;
112+
}
113+
}

src/FlowSynx.Client/Services/AuditService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class AuditService : IAuditService
1717
public AuditService(IHttpRequestHandler httpRequestHandler) =>
1818
_httpRequestHandler = httpRequestHandler;
1919

20-
public async Task<HttpResult<Result<IEnumerable<AuditsListResponse>>>> ListAsync(
20+
public async Task<HttpResult<PaginatedResult<AuditsListResponse>>> ListAsync(
2121
CancellationToken cancellationToken = default)
2222
{
2323
var requestMessage = new Request
@@ -27,7 +27,7 @@ public async Task<HttpResult<Result<IEnumerable<AuditsListResponse>>>> ListAsync
2727
};
2828

2929
return await _httpRequestHandler
30-
.SendRequestAsync<Result<IEnumerable<AuditsListResponse>>>(requestMessage, cancellationToken);
30+
.SendRequestAsync< PaginatedResult < AuditsListResponse>>(requestMessage, cancellationToken);
3131
}
3232

3333
public async Task<HttpResult<Result<AuditDetailsResponse>>> DetailsAsync(

src/FlowSynx.Client/Services/IAuditService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface IAuditService
1717
/// A task that represents the asynchronous operation. The task result contains an
1818
/// <see cref="HttpResult{T}"/> with a <see cref="Result{T}"/> holding a collection of <see cref="AuditsListResponse"/>.
1919
/// </returns>
20-
Task<HttpResult<Result<IEnumerable<AuditsListResponse>>>> ListAsync(
20+
Task<HttpResult<PaginatedResult<AuditsListResponse>>> ListAsync(
2121
CancellationToken cancellationToken = default);
2222

2323
/// <summary>

src/FlowSynx.Client/Services/ILogsService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public interface ILogsService
1818
/// A task that represents the asynchronous operation. The task result contains an
1919
/// <see cref="HttpResult{T}"/> with a <see cref="Result{T}"/> holding a collection of <see cref="LogsListResponse"/>.
2020
/// </returns>
21-
Task<HttpResult<Result<IEnumerable<LogsListResponse>>>> ListAsync(
21+
Task<HttpResult<PaginatedResult<LogsListResponse>>> ListAsync(
2222
LogsListRequest request,
2323
CancellationToken cancellationToken = default);
2424
}

src/FlowSynx.Client/Services/IPluginConfigService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface IPluginConfigService
1717
/// A task that represents the asynchronous operation. The task result contains an
1818
/// <see cref="HttpResult{T}"/> with a <see cref="Result{T}"/> holding a collection of <see cref="PluginConfigListResponse"/>.
1919
/// </returns>
20-
Task<HttpResult<Result<IEnumerable<PluginConfigListResponse>>>> ListAsync(
20+
Task<HttpResult<PaginatedResult<PluginConfigListResponse>>> ListAsync(
2121
CancellationToken cancellationToken = default);
2222

2323
/// <summary>

src/FlowSynx.Client/Services/IPluginsService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface IPluginsService
1717
/// A task that represents the asynchronous operation. The task result contains an
1818
/// <see cref="HttpResult{T}"/> with a <see cref="Result{T}"/> holding a collection of <see cref="PluginsListResponse"/>.
1919
/// </returns>
20-
Task<HttpResult<Result<IEnumerable<PluginsListResponse>>>> ListAsync(
20+
Task<HttpResult<PaginatedResult<PluginsListResponse>>> ListAsync(
2121
CancellationToken cancellationToken = default);
2222

2323
/// <summary>

src/FlowSynx.Client/Services/IWorkflowsService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public interface IWorkflowsService
1414
/// </summary>
1515
/// <param name="cancellationToken">Token to cancel the asynchronous operation.</param>
1616
/// <returns>A result containing a list of workflow summaries.</returns>
17-
Task<HttpResult<Result<IEnumerable<WorkflowListResponse>>>> ListAsync(
17+
Task<HttpResult<PaginatedResult<WorkflowListResponse>>> ListAsync(
1818
CancellationToken cancellationToken = default);
1919

2020
/// <summary>
@@ -63,7 +63,7 @@ Task<HttpResult<Result<Unit>>> DeleteAsync(
6363
/// <param name="request">The request specifying filters for executions.</param>
6464
/// <param name="cancellationToken">Token to cancel the asynchronous operation.</param>
6565
/// <returns>A result containing a list of workflow executions.</returns>
66-
Task<HttpResult<Result<IEnumerable<WorkflowExecutionListResponse>>>> ExecutionsAsync(
66+
Task<HttpResult<PaginatedResult<WorkflowExecutionListResponse>>> ExecutionsAsync(
6767
WorkflowExecutionListRequest request,
6868
CancellationToken cancellationToken = default);
6969

@@ -103,7 +103,7 @@ Task<HttpResult<Result<Unit>>> CancelExecutionsAsync(
103103
/// <param name="request">The request for the approvals list.</param>
104104
/// <param name="cancellationToken">Token to cancel the asynchronous operation.</param>
105105
/// <returns>A result containing a collection of workflow execution approval responses.</returns>
106-
Task<HttpResult<Result<IEnumerable<WorkflowExecutionPendingApprovalsResponse>>>> ExecutionPendingApprovalsAsync(
106+
Task<HttpResult<PaginatedResult<WorkflowExecutionPendingApprovalsResponse>>> ExecutionPendingApprovalsAsync(
107107
WorkflowExecutionPendingApprovalsRequest request,
108108
CancellationToken cancellationToken = default);
109109

@@ -143,7 +143,7 @@ Task<HttpResult<Result<WorkflowExecutionLogsResponse>>> ExecutionsLogsAsync(
143143
/// <param name="request">The request object containing workflow execution identifiers associated tasks.</param>
144144
/// <param name="cancellationToken">A token that can be used to cancel the asynchronous operation.</param>
145145
/// <returns>A result containing list of execution tasks.</returns>
146-
Task<HttpResult<Result<IEnumerable<WorkflowExecutionTasksResponse>>>> ExecutionTasksAsync(
146+
Task<HttpResult<PaginatedResult<WorkflowExecutionTasksResponse>>> ExecutionTasksAsync(
147147
WorkflowExecutionTasksRequest request,
148148
CancellationToken cancellationToken = default);
149149

@@ -163,7 +163,7 @@ Task<HttpResult<Result<WorkflowTaskExecutionDetailsResponse>>> TaskExecutionDeta
163163
/// <param name="request">The request specifying which task execution logs to retrieve.</param>
164164
/// <param name="cancellationToken">Token to cancel the asynchronous operation.</param>
165165
/// <returns>A result containing the task execution logs.</returns>
166-
Task<HttpResult<Result<IEnumerable<WorkflowTaskExecutionLogsResponse>>>> TaskExecutionLogsAsync(
166+
Task<HttpResult<PaginatedResult<WorkflowTaskExecutionLogsResponse>>> TaskExecutionLogsAsync(
167167
WorkflowTaskExecutionLogsRequest request,
168168
CancellationToken cancellationToken = default);
169169

@@ -173,7 +173,7 @@ Task<HttpResult<Result<IEnumerable<WorkflowTaskExecutionLogsResponse>>>> TaskExe
173173
/// <param name="request">The request specifying filter criteria for triggers.</param>
174174
/// <param name="cancellationToken">Token to cancel the asynchronous operation.</param>
175175
/// <returns>A result containing a list of workflow triggers.</returns>
176-
Task<HttpResult<Result<IEnumerable<WorkflowTriggersListResponse>>>> TriggersAsync(
176+
Task<HttpResult<PaginatedResult<WorkflowTriggersListResponse>>> TriggersAsync(
177177
WorkflowTriggersListRequest request,
178178
CancellationToken cancellationToken = default);
179179

src/FlowSynx.Client/Services/LogsService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class LogsService: ILogsService
1212

1313
public LogsService(IHttpRequestHandler httpRequestHandler) => _httpRequestHandler = httpRequestHandler;
1414

15-
public async Task<HttpResult<Result<IEnumerable<LogsListResponse>>>> ListAsync(
15+
public async Task<HttpResult<PaginatedResult<LogsListResponse>>> ListAsync(
1616
LogsListRequest request,
1717
CancellationToken cancellationToken = default)
1818
{
@@ -24,6 +24,6 @@ public async Task<HttpResult<Result<IEnumerable<LogsListResponse>>>> ListAsync(
2424
};
2525

2626
return await _httpRequestHandler
27-
.SendRequestAsync<LogsListRequest, Result<IEnumerable<LogsListResponse>>>(requestMessage, cancellationToken);
27+
.SendRequestAsync<LogsListRequest, PaginatedResult < LogsListResponse>>(requestMessage, cancellationToken);
2828
}
2929
}

src/FlowSynx.Client/Services/PluginConfigService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class PluginConfigService: IPluginConfigService
1313
public PluginConfigService(IHttpRequestHandler httpRequestHandler) =>
1414
_httpRequestHandler = httpRequestHandler;
1515

16-
public async Task<HttpResult<Result<IEnumerable<PluginConfigListResponse>>>> ListAsync(
16+
public async Task<HttpResult<PaginatedResult<PluginConfigListResponse>>> ListAsync(
1717
CancellationToken cancellationToken = default)
1818
{
1919
var requestMessage = new Request
@@ -22,7 +22,7 @@ public async Task<HttpResult<Result<IEnumerable<PluginConfigListResponse>>>> Lis
2222
Uri = "config"
2323
};
2424

25-
return await _httpRequestHandler.SendRequestAsync<Result<IEnumerable<PluginConfigListResponse>>>(requestMessage, cancellationToken);
25+
return await _httpRequestHandler.SendRequestAsync<PaginatedResult<PluginConfigListResponse>>(requestMessage, cancellationToken);
2626
}
2727

2828
public async Task<HttpResult<Result<AddPluginConfigResponse>>> AddAsync(

src/FlowSynx.Client/Services/PluginsService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class PluginsService: IPluginsService
1313
public PluginsService(IHttpRequestHandler httpRequestHandler) =>
1414
_httpRequestHandler = httpRequestHandler;
1515

16-
public async Task<HttpResult<Result<IEnumerable<PluginsListResponse>>>> ListAsync(
16+
public async Task<HttpResult<PaginatedResult<PluginsListResponse>>> ListAsync(
1717
CancellationToken cancellationToken = default)
1818
{
1919
var requestMessage = new Request
@@ -23,7 +23,7 @@ public async Task<HttpResult<Result<IEnumerable<PluginsListResponse>>>> ListAsyn
2323
};
2424

2525
return await _httpRequestHandler
26-
.SendRequestAsync<Result<IEnumerable<PluginsListResponse>>>(requestMessage, cancellationToken);
26+
.SendRequestAsync<PaginatedResult<PluginsListResponse>>(requestMessage, cancellationToken);
2727
}
2828

2929
public async Task<HttpResult<Result<Unit>>> InstallAsync(

0 commit comments

Comments
 (0)