|
| 1 | +using Authorization.Policies; |
| 2 | +using Dapper; |
| 3 | +using Vote.Monitor.Core.Models; |
| 4 | +using Vote.Monitor.Domain.ConnectionFactory; |
| 5 | +using Vote.Monitor.Domain.Specifications; |
| 6 | + |
| 7 | +namespace Feature.Monitoring.ListAvailableNgos; |
| 8 | + |
| 9 | +public class Endpoint(INpgsqlConnectionFactory dbConnectionFactory) |
| 10 | + : Endpoint<Request, Results<Ok<PagedResponse<AvailableNgoModel>>, NotFound>> |
| 11 | +{ |
| 12 | + public override void Configure() |
| 13 | + { |
| 14 | + Get("/api/election-rounds/{electionRoundId}/monitoring-ngos:available"); |
| 15 | + DontAutoTag(); |
| 16 | + Options(x => x.WithTags("monitoring")); |
| 17 | + Summary(s => |
| 18 | + { |
| 19 | + s.Summary = "Lists ngos that have status Activated and currently are not monitoring specified election round"; |
| 20 | + }); |
| 21 | + Policies(PolicyNames.PlatformAdminsOnly); |
| 22 | + } |
| 23 | + |
| 24 | + public override async Task<Results<Ok<PagedResponse<AvailableNgoModel>>, NotFound>> ExecuteAsync(Request request, |
| 25 | + CancellationToken ct) |
| 26 | + { |
| 27 | + var sql = """ |
| 28 | + SELECT |
| 29 | + COUNT(*) |
| 30 | + FROM |
| 31 | + "Ngos" N |
| 32 | + WHERE |
| 33 | + "Status" = 'Activated' |
| 34 | + AND "Id" NOT IN ( |
| 35 | + SELECT |
| 36 | + "NgoId" |
| 37 | + FROM |
| 38 | + "MonitoringNgos" |
| 39 | + WHERE |
| 40 | + "ElectionRoundId" = @electionRoundId |
| 41 | + ) |
| 42 | + AND ( |
| 43 | + @searchText IS NULL |
| 44 | + OR N."Name" ILIKE @searchText |
| 45 | + OR N."Id"::TEXT ILIKE @searchText |
| 46 | + ); |
| 47 | +
|
| 48 | + SELECT |
| 49 | + N."Id", |
| 50 | + N."Name" |
| 51 | + FROM |
| 52 | + "Ngos" N |
| 53 | + WHERE |
| 54 | + "Status" = 'Activated' |
| 55 | + AND "Id" NOT IN ( |
| 56 | + SELECT |
| 57 | + "NgoId" |
| 58 | + FROM |
| 59 | + "MonitoringNgos" |
| 60 | + WHERE |
| 61 | + "ElectionRoundId" = @electionRoundId |
| 62 | + ) |
| 63 | + AND ( |
| 64 | + @searchText IS NULL |
| 65 | + OR N."Name" ILIKE @searchText |
| 66 | + OR N."Id"::TEXT ILIKE @searchText |
| 67 | + ) |
| 68 | + ORDER BY |
| 69 | + CASE |
| 70 | + WHEN @sortExpression = 'Name ASC' THEN "Name" |
| 71 | + END ASC, |
| 72 | + CASE |
| 73 | + WHEN @sortExpression = 'Name DESC' THEN "Name" |
| 74 | + END DESC |
| 75 | + OFFSET |
| 76 | + @offset ROWS |
| 77 | + FETCH NEXT |
| 78 | + @pageSize ROWS ONLY; |
| 79 | + """; |
| 80 | + |
| 81 | + var queryArgs = new |
| 82 | + { |
| 83 | + electionRoundId = request.ElectionRoundId, |
| 84 | + searchText = request.SearchText, |
| 85 | + offset = PaginationHelper.CalculateSkip(request.PageSize, request.PageNumber), |
| 86 | + pageSize = request.PageSize, |
| 87 | + sortExpression = GetSortExpression(request.SortColumnName, request.IsAscendingSorting) |
| 88 | + }; |
| 89 | + |
| 90 | + int totalRowCount; |
| 91 | + List<AvailableNgoModel> entries; |
| 92 | + using (var dbConnection = await dbConnectionFactory.GetOpenConnectionAsync(ct)) |
| 93 | + { |
| 94 | + using var multi = await dbConnection.QueryMultipleAsync(sql, queryArgs); |
| 95 | + |
| 96 | + totalRowCount = multi.Read<int>().Single(); |
| 97 | + entries = multi.Read<AvailableNgoModel>().ToList(); |
| 98 | + } |
| 99 | + |
| 100 | + return TypedResults.Ok( |
| 101 | + new PagedResponse<AvailableNgoModel>(entries, totalRowCount, request.PageNumber, request.PageSize)); |
| 102 | + } |
| 103 | + |
| 104 | + private static string GetSortExpression(string? sortColumnName, bool isAscendingSorting) |
| 105 | + { |
| 106 | + if (string.IsNullOrWhiteSpace(sortColumnName)) |
| 107 | + { |
| 108 | + return "Name ASC"; |
| 109 | + } |
| 110 | + |
| 111 | + var sortOrder = isAscendingSorting ? "ASC" : "DESC"; |
| 112 | + |
| 113 | + if (string.Equals(sortColumnName, nameof(AvailableNgoModel.Name), StringComparison.InvariantCultureIgnoreCase)) |
| 114 | + { |
| 115 | + return $"{nameof(AvailableNgoModel.Name)} {sortOrder}"; |
| 116 | + } |
| 117 | + |
| 118 | + return "Name ASC"; |
| 119 | + } |
| 120 | +} |
0 commit comments