aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Endpoints/V1/Projects/GetProjectsRoute.cs
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2023-02-25 13:15:44 +0100
committerivarlovlie <git@ivarlovlie.no>2023-02-25 13:15:44 +0100
commit900bb5e845c3ad44defbd427cae3d44a4a43321f (patch)
treedf3d96a93771884add571e82336c29fc3d9c7a1c /code/api/src/Endpoints/V1/Projects/GetProjectsRoute.cs
downloadgreatoffice-900bb5e845c3ad44defbd427cae3d44a4a43321f.tar.xz
greatoffice-900bb5e845c3ad44defbd427cae3d44a4a43321f.zip
feat: Initial commit
Diffstat (limited to 'code/api/src/Endpoints/V1/Projects/GetProjectsRoute.cs')
-rw-r--r--code/api/src/Endpoints/V1/Projects/GetProjectsRoute.cs40
1 files changed, 40 insertions, 0 deletions
diff --git a/code/api/src/Endpoints/V1/Projects/GetProjectsRoute.cs b/code/api/src/Endpoints/V1/Projects/GetProjectsRoute.cs
new file mode 100644
index 0000000..8fe70a6
--- /dev/null
+++ b/code/api/src/Endpoints/V1/Projects/GetProjectsRoute.cs
@@ -0,0 +1,40 @@
+using MR.AspNetCore.Pagination;
+
+namespace IOL.GreatOffice.Api.Endpoints.V1.Projects;
+
+public class GetProjectsRoute : RouteBaseAsync.WithRequest<GetProjectsQueryParameters>.WithActionResult<KeysetPaginationResult<GetProjectsResponseDto>>
+{
+ private readonly MainAppDatabase _database;
+ private readonly PaginationService _pagination;
+
+ public GetProjectsRoute(MainAppDatabase database, PaginationService pagination) {
+ _database = database;
+ _pagination = pagination;
+ }
+
+ [HttpGet("~/v{version:apiVersion}/projects")]
+ public override async Task<ActionResult<KeysetPaginationResult<GetProjectsResponseDto>>> HandleAsync([FromQuery] GetProjectsQueryParameters request, CancellationToken cancellationToken = default) {
+ var result = await _pagination.KeysetPaginateAsync(
+ _database.Projects.ForTenant(LoggedInUser).ConditionalWhere(() => request.NameQuery.HasValue(), p => p.Name.Contains(request.NameQuery)),
+ b => b.Descending(x => x.CreatedAt),
+ async id => await _database.Projects.FindAsync(id),
+ query => query.Select(p => new GetProjectsResponseDto() {
+ Id = p.Id,
+ Name = p.Name
+ })
+ );
+ return Ok(result);
+ }
+}
+
+public class GetProjectsResponseDto
+{
+ public Guid Id { get; set; }
+ public string Name { get; set; }
+}
+
+public class GetProjectsQueryParameters
+{
+ [FromQuery(Name = "name")]
+ public string NameQuery { get; set; }
+} \ No newline at end of file