|
1 | | -using Vote.Monitor.Api.Feature.Ngo.Specifications; |
| 1 | +using Authorization.Policies; |
| 2 | +using Dapper; |
2 | 3 | using Vote.Monitor.Core.Models; |
| 4 | +using Vote.Monitor.Domain.ConnectionFactory; |
| 5 | +using Vote.Monitor.Domain.Specifications; |
3 | 6 |
|
4 | 7 | namespace Vote.Monitor.Api.Feature.Ngo.List; |
5 | 8 |
|
6 | | -public class Endpoint(IReadRepository<NgoAggregate> repository) : Endpoint<Request, Results<Ok<PagedResponse<NgoModel>>, ProblemDetails>> |
| 9 | +public class Endpoint(INpgsqlConnectionFactory dbConnectionFactory) |
| 10 | + : Endpoint<Request, Results<Ok<PagedResponse<NgoModel>>, NotFound>> |
7 | 11 | { |
8 | 12 | public override void Configure() |
9 | 13 | { |
10 | 14 | Get("/api/ngos"); |
| 15 | + DontAutoTag(); |
| 16 | + Options(x => x.WithTags("ngos")); |
| 17 | + Policies(PolicyNames.PlatformAdminsOnly); |
11 | 18 | } |
12 | 19 |
|
13 | | - public override async Task<Results<Ok<PagedResponse<NgoModel>>, ProblemDetails>> ExecuteAsync(Request req, CancellationToken ct) |
| 20 | + public override async Task<Results<Ok<PagedResponse<NgoModel>>, NotFound>> ExecuteAsync(Request req, |
| 21 | + CancellationToken ct) |
14 | 22 | { |
15 | | - var specification = new ListNgosSpecification(req); |
16 | | - var ngos = await repository.ListAsync(specification, ct); |
17 | | - var ngosCount = await repository.CountAsync(specification, ct); |
| 23 | + var sql = """ |
| 24 | + SELECT |
| 25 | + COUNT(*) AS COUNT |
| 26 | + FROM |
| 27 | + "Ngos" N |
| 28 | + WHERE |
| 29 | + ( |
| 30 | + @searchText IS NULL |
| 31 | + OR N."Name" ILIKE @searchText |
| 32 | + OR N."Id"::TEXT ILIKE @searchText |
| 33 | + ) |
| 34 | + AND (@status IS NULL OR N."Status" = @status); |
18 | 35 |
|
19 | | - var result = ngos.Select(x => new NgoModel |
| 36 | + WITH CTE_Ngos AS ( |
| 37 | + SELECT |
| 38 | + N."Id", |
| 39 | + N."Name", |
| 40 | + N."Status", |
| 41 | + (SELECT COUNT(1) |
| 42 | + FROM "NgoAdmins" NA |
| 43 | + WHERE NA."NgoId" = N."Id") AS "NumberOfNgoAdmins", |
| 44 | + (SELECT COUNT(1) |
| 45 | + FROM "MonitoringNgos" MN |
| 46 | + WHERE MN."NgoId" = N."Id") AS "NumberOfElectionsMonitoring", |
| 47 | + ( |
| 48 | + SELECT MAX(ER."StartDate") |
| 49 | + FROM "MonitoringNgos" MN |
| 50 | + INNER JOIN "ElectionRounds" ER ON ER."Id" = MN."ElectionRoundId" |
| 51 | + WHERE MN."NgoId" = N."Id" |
| 52 | + ) AS "DateOfLastElection" |
| 53 | + FROM |
| 54 | + "Ngos" N |
| 55 | + WHERE |
| 56 | + ( |
| 57 | + @searchText IS NULL |
| 58 | + OR N."Name" ILIKE @searchText |
| 59 | + OR N."Id"::TEXT ILIKE @searchText |
| 60 | + ) |
| 61 | + AND (@status IS NULL OR N."Status" = @status) |
| 62 | + ) |
| 63 | + SELECT * FROM CTE_Ngos |
| 64 | + ORDER BY |
| 65 | + CASE |
| 66 | + WHEN @sortExpression = 'Name ASC' THEN "Name" |
| 67 | + END ASC, |
| 68 | + CASE |
| 69 | + WHEN @sortExpression = 'Name DESC' THEN "Name" |
| 70 | + END DESC, |
| 71 | + CASE |
| 72 | + WHEN @sortExpression = 'Status ASC' THEN "Status" |
| 73 | + END ASC, |
| 74 | + CASE |
| 75 | + WHEN @sortExpression = 'Status DESC' THEN "Status" |
| 76 | + END DESC, |
| 77 | + CASE |
| 78 | + WHEN @sortExpression = 'NumberOfNgoAdmins ASC' THEN "NumberOfNgoAdmins" |
| 79 | + END ASC, |
| 80 | + CASE |
| 81 | + WHEN @sortExpression = 'NumberOfNgoAdmins DESC' THEN "NumberOfNgoAdmins" |
| 82 | + END DESC, |
| 83 | + CASE |
| 84 | + WHEN @sortExpression = 'NumberOfElectionsMonitoring ASC' THEN "NumberOfElectionsMonitoring" |
| 85 | + END ASC, |
| 86 | + CASE |
| 87 | + WHEN @sortExpression = 'NumberOfElectionsMonitoring DESC' THEN "NumberOfElectionsMonitoring" |
| 88 | + END DESC, |
| 89 | + CASE |
| 90 | + WHEN @sortExpression = 'DateOfLastElection ASC' THEN "DateOfLastElection" |
| 91 | + END ASC, |
| 92 | + CASE |
| 93 | + WHEN @sortExpression = 'DateOfLastElection DESC' THEN "DateOfLastElection" |
| 94 | + END DESC |
| 95 | + OFFSET @offset ROWS |
| 96 | + FETCH NEXT @pageSize ROWS ONLY; |
| 97 | + """; |
| 98 | + |
| 99 | + var queryArgs = new |
| 100 | + { |
| 101 | + searchText = $"%{req.SearchText?.Trim() ?? string.Empty}%", |
| 102 | + status = req.Status?.ToString(), |
| 103 | + offset = PaginationHelper.CalculateSkip(req.PageSize, req.PageNumber), |
| 104 | + pageSize = req.PageSize, |
| 105 | + sortExpression = GetSortExpression(req.SortColumnName, req.IsAscendingSorting) |
| 106 | + }; |
| 107 | + |
| 108 | + int totalRowCount; |
| 109 | + List<NgoModel> entries; |
| 110 | + |
| 111 | + using (var dbConnection = await dbConnectionFactory.GetOpenConnectionAsync(ct)) |
| 112 | + { |
| 113 | + using var multi = await dbConnection.QueryMultipleAsync(sql, queryArgs); |
| 114 | + totalRowCount = multi.Read<int>().Single(); |
| 115 | + entries = multi.Read<NgoModel>().ToList(); |
| 116 | + } |
| 117 | + |
| 118 | + return TypedResults.Ok( |
| 119 | + new PagedResponse<NgoModel>(entries, totalRowCount, req.PageNumber, req.PageSize)); |
| 120 | + } |
| 121 | + |
| 122 | + private static string GetSortExpression(string? sortColumnName, bool isAscendingSorting) |
| 123 | + { |
| 124 | + if (string.IsNullOrWhiteSpace(sortColumnName)) |
20 | 125 | { |
21 | | - Id = x.Id, |
22 | | - Name = x.Name, |
23 | | - Status = x.Status, |
24 | | - CreatedOn = x.CreatedOn, |
25 | | - LastModifiedOn = x.LastModifiedOn |
26 | | - }).ToList(); |
27 | | - |
28 | | - return TypedResults.Ok(new PagedResponse<NgoModel>(result, ngosCount, req.PageNumber, req.PageSize)); |
| 126 | + return $"{nameof(NgoModel.Name)} ASC"; |
| 127 | + } |
| 128 | + |
| 129 | + var sortOrder = isAscendingSorting ? "ASC" : "DESC"; |
| 130 | + |
| 131 | + if (string.Equals(sortColumnName, nameof(NgoModel.Status), |
| 132 | + StringComparison.InvariantCultureIgnoreCase)) |
| 133 | + { |
| 134 | + return $"{nameof(NgoModel.Status)} {sortOrder}"; |
| 135 | + } |
| 136 | + |
| 137 | + if (string.Equals(sortColumnName, nameof(NgoModel.NumberOfNgoAdmins), |
| 138 | + StringComparison.InvariantCultureIgnoreCase)) |
| 139 | + { |
| 140 | + return $"{nameof(NgoModel.NumberOfNgoAdmins)} {sortOrder}"; |
| 141 | + } |
| 142 | + |
| 143 | + if (string.Equals(sortColumnName, nameof(NgoModel.NumberOfElectionsMonitoring), |
| 144 | + StringComparison.InvariantCultureIgnoreCase)) |
| 145 | + { |
| 146 | + return $"{nameof(NgoModel.NumberOfElectionsMonitoring)} {sortOrder}"; |
| 147 | + } |
| 148 | + |
| 149 | + if (string.Equals(sortColumnName, nameof(NgoModel.DateOfLastElection), |
| 150 | + StringComparison.InvariantCultureIgnoreCase)) |
| 151 | + { |
| 152 | + return $"{nameof(NgoModel.DateOfLastElection)} {sortOrder}"; |
| 153 | + } |
| 154 | + |
| 155 | + return $"{nameof(NgoModel.Name)} DESC"; |
29 | 156 | } |
30 | 157 | } |
0 commit comments