|
1 | | -using Vote.Monitor.Core.Models; |
| 1 | +using Dapper; |
| 2 | +using Vote.Monitor.Core.Models; |
| 3 | +using Vote.Monitor.Domain.ConnectionFactory; |
| 4 | +using Vote.Monitor.Domain.Specifications; |
2 | 5 |
|
3 | 6 | namespace Vote.Monitor.Api.Feature.ElectionRound.List; |
4 | 7 |
|
5 | | -public class Endpoint(IReadRepository<ElectionRoundAggregate> repository) |
6 | | - : Endpoint<Request, Results<Ok<PagedResponse<ElectionRoundModel>>, ProblemDetails>> |
| 8 | +public class Endpoint(INpgsqlConnectionFactory dbConnectionFactory) |
| 9 | + : Endpoint<Request, Results<Ok<PagedResponse<Response>>, ProblemDetails>> |
7 | 10 | { |
8 | 11 | public override void Configure() |
9 | 12 | { |
10 | 13 | Get("/api/election-rounds"); |
11 | 14 | Policies(PolicyNames.PlatformAdminsOnly); |
12 | 15 | } |
13 | 16 |
|
14 | | - public override async Task<Results<Ok<PagedResponse<ElectionRoundModel>>, ProblemDetails>> ExecuteAsync(Request req, CancellationToken ct) |
| 17 | + public override async Task<Results<Ok<PagedResponse<Response>>, ProblemDetails>> ExecuteAsync(Request req, |
| 18 | + CancellationToken ct) |
15 | 19 | { |
16 | | - var specification = new ListElectionRoundsSpecification(req); |
17 | | - var electionRounds = await repository.ListAsync(specification, ct); |
18 | | - var electionRoundsCount = await repository.CountAsync(specification, ct); |
| 20 | + var sql = """ |
| 21 | + SELECT COUNT(*) |
| 22 | + FROM "ElectionRounds" ER |
| 23 | + WHERE (@searchText IS NULL |
| 24 | + OR ER."Title" ILIKE @searchText |
| 25 | + OR ER."EnglishTitle" ILIKE @searchText |
| 26 | + OR ER."Id"::TEXT ILIKE @searchText) |
| 27 | + AND (@electionRoundStatus IS NULL OR ER."Status" = @electionRoundStatus) |
| 28 | + AND (@countryId IS NULL OR ER."CountryId" = @countryId); |
19 | 29 |
|
20 | | - return TypedResults.Ok(new PagedResponse<ElectionRoundModel>(electionRounds, electionRoundsCount, req.PageNumber, req.PageSize)); |
| 30 | + WITH FilteredElections as (SELECT ER."Id", |
| 31 | + ER."Title", |
| 32 | + ER."EnglishTitle", |
| 33 | + ER."StartDate", |
| 34 | + ER."Status", |
| 35 | + ER."CreatedOn", |
| 36 | + ER."LastModifiedOn", |
| 37 | + ER."CountryId", |
| 38 | + "C"."Iso2" AS "CountryIso2", |
| 39 | + "C"."Iso3" AS "CountryIso3", |
| 40 | + "C"."Name" AS "CountryName", |
| 41 | + "C"."FullName" AS "CountryFullName", |
| 42 | + "C"."NumericCode" AS "CountryNumericCode", |
| 43 | + COALESCE((select jsonb_agg(jsonb_build_object('Id', MN."Id", 'Name', n."Name", 'Status', n."Status")) |
| 44 | + FROM "MonitoringNgos" MN |
| 45 | + INNER join "Ngos" n on n."Id" = mn."NgoId" |
| 46 | + where MN."ElectionRoundId" = ER."Id"), '[]'::JSONB) AS "MonitoringNgos" |
| 47 | + FROM "ElectionRounds" ER |
| 48 | + INNER JOIN "Countries" "C" ON ER."CountryId" = "C"."Id" |
| 49 | + WHERE (@searchText IS NULL |
| 50 | + OR ER."Title" ILIKE @searchText |
| 51 | + OR ER."EnglishTitle" ILIKE @searchText |
| 52 | + OR ER."Id"::TEXT ILIKE @searchText) |
| 53 | + AND (@electionRoundStatus IS NULL OR ER."Status" = @electionRoundStatus) |
| 54 | + AND (@countryId IS NULL OR ER."CountryId" = @countryId)) |
| 55 | +
|
| 56 | + select * |
| 57 | + from FilteredElections |
| 58 | + ORDER BY CASE WHEN @sortExpression = 'Title ASC' THEN "Title" END ASC, |
| 59 | + CASE WHEN @sortExpression = 'Title DESC' THEN "Title" END DESC, |
| 60 | + CASE WHEN @sortExpression = 'EnglishTitle ASC' THEN "EnglishTitle" END ASC, |
| 61 | + CASE WHEN @sortExpression = 'EnglishTitle DESC' THEN "EnglishTitle" END DESC, |
| 62 | + CASE WHEN @sortExpression = 'StartDate ASC' THEN "StartDate" END ASC, |
| 63 | + CASE WHEN @sortExpression = 'StartDate DESC' THEN "StartDate" END DESC, |
| 64 | + CASE WHEN @sortExpression = 'Status ASC' THEN "Status" END ASC, |
| 65 | + CASE WHEN @sortExpression = 'Status DESC' THEN "Status" END DESC, |
| 66 | + CASE WHEN @sortExpression = 'CountryName ASC' THEN "CountryName" END ASC, |
| 67 | + CASE WHEN @sortExpression = 'CountryName DESC' THEN "CountryName" END DESC, |
| 68 | + CASE WHEN @sortExpression = 'MonitoringNgos ASC' THEN JSONB_ARRAY_LENGTH("MonitoringNgos") END ASC, |
| 69 | + CASE WHEN @sortExpression = 'MonitoringNgos DESC' THEN JSONB_ARRAY_LENGTH("MonitoringNgos") END DESC |
| 70 | + OFFSET @offset ROWS FETCH NEXT @pageSize ROWS ONLY; |
| 71 | + """; |
| 72 | + |
| 73 | + var queryArgs = new |
| 74 | + { |
| 75 | + searchText = $"%{req.SearchText?.Trim() ?? string.Empty}%", |
| 76 | + offset = PaginationHelper.CalculateSkip(req.PageSize, req.PageNumber), |
| 77 | + pageSize = req.PageSize, |
| 78 | + electionroundstatus = req.ElectionRoundStatus?.ToString(), |
| 79 | + countryId = req.CountryId, |
| 80 | + sortExpression = GetSortExpression(req.SortColumnName, req.IsAscendingSorting) |
| 81 | + }; |
| 82 | + |
| 83 | + int totalRowCount = 0; |
| 84 | + List<Response> entries; |
| 85 | + |
| 86 | + using (var dbConnection = await dbConnectionFactory.GetOpenConnectionAsync(ct)) |
| 87 | + { |
| 88 | + using var multi = await dbConnection.QueryMultipleAsync(sql, queryArgs); |
| 89 | + totalRowCount = multi.Read<int>().Single(); |
| 90 | + entries = multi.Read<Response>().ToList(); |
| 91 | + } |
| 92 | + |
| 93 | + return TypedResults.Ok( |
| 94 | + new PagedResponse<Response>(entries, totalRowCount, req.PageNumber, req.PageSize)); |
| 95 | + } |
| 96 | + |
| 97 | + private static string GetSortExpression(string? sortColumnName, bool isAscendingSorting) |
| 98 | + { |
| 99 | + if (string.IsNullOrWhiteSpace(sortColumnName)) |
| 100 | + { |
| 101 | + return $"{nameof(List.Response.StartDate)} DESC"; |
| 102 | + } |
| 103 | + |
| 104 | + var sortOrder = isAscendingSorting ? "ASC" : "DESC"; |
| 105 | + |
| 106 | + |
| 107 | + if (string.Equals(sortColumnName, nameof(List.Response.Title), |
| 108 | + StringComparison.InvariantCultureIgnoreCase)) |
| 109 | + { |
| 110 | + return $"{nameof(List.Response.Title)} {sortOrder}"; |
| 111 | + } |
| 112 | + |
| 113 | + if (string.Equals(sortColumnName, nameof(List.Response.EnglishTitle), |
| 114 | + StringComparison.InvariantCultureIgnoreCase)) |
| 115 | + { |
| 116 | + return $"{nameof(List.Response.EnglishTitle)} {sortOrder}"; |
| 117 | + } |
| 118 | + |
| 119 | + if (string.Equals(sortColumnName, nameof(List.Response.StartDate), |
| 120 | + StringComparison.InvariantCultureIgnoreCase)) |
| 121 | + { |
| 122 | + return $"{nameof(List.Response.StartDate)} {sortOrder}"; |
| 123 | + } |
| 124 | + |
| 125 | + if (string.Equals(sortColumnName, nameof(List.Response.CountryName), |
| 126 | + StringComparison.InvariantCultureIgnoreCase)) |
| 127 | + { |
| 128 | + return $"{nameof(List.Response.CountryName)} {sortOrder}"; |
| 129 | + } |
| 130 | + |
| 131 | + if (string.Equals(sortColumnName, nameof(List.Response.MonitoringNgos), |
| 132 | + StringComparison.InvariantCultureIgnoreCase)) |
| 133 | + { |
| 134 | + return $"{nameof(List.Response.MonitoringNgos)} {sortOrder}"; |
| 135 | + } |
| 136 | + |
| 137 | + return $"{nameof(List.Response.StartDate)} DESC"; |
21 | 138 | } |
22 | 139 | } |
0 commit comments