22using Microsoft . AspNetCore . Http ;
33using Microsoft . AspNetCore . Mvc ;
44using Microsoft . Extensions . Options ;
5+ using System . Web ;
56using Umbraco . Cms . Integrations . Crm . ActiveCampaign . Configuration ;
67using Umbraco . Cms . Integrations . Crm . ActiveCampaign . Models . Dtos ;
78
@@ -20,13 +21,11 @@ public GetFormsByPageController(IOptions<ActiveCampaignSettings> options, IHttpC
2021 [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
2122 [ ProducesResponseType ( StatusCodes . Status403Forbidden ) ]
2223 [ ProducesResponseType ( StatusCodes . Status500InternalServerError ) ]
23- public async Task < IActionResult > GetForms ( [ FromQuery ] int ? page = 1 )
24+ public async Task < IActionResult > GetForms ( [ FromQuery ] int ? page = 1 , string ? searchQuery = "" )
2425 {
2526 var client = HttpClientFactory . CreateClient ( Constants . FormsHttpClient ) ;
2627
27- var requestUriString = page == 1
28- ? $ "{ client . BaseAddress } { ApiPath } &limit={ Constants . DefaultPageSize } "
29- : $ "{ client . BaseAddress } { ApiPath } &limit={ Constants . DefaultPageSize } &offset={ ( page - 1 ) * Constants . DefaultPageSize } ";
28+ var requestUriString = BuildRequestUri ( client . BaseAddress . ToString ( ) , page ?? 1 , searchQuery ) ;
3029
3130 var requestMessage = new HttpRequestMessage
3231 {
@@ -38,5 +37,25 @@ public async Task<IActionResult> GetForms([FromQuery]int? page = 1)
3837
3938 return await HandleResponseAsync < FormCollectionResponseDto > ( response ) ;
4039 }
40+
41+ private string BuildRequestUri ( string baseAddress , int page , string searchQuery )
42+ {
43+ var uri = $ "{ baseAddress } { ApiPath } ?limit={ Constants . DefaultPageSize } ";
44+
45+ Dictionary < string , string > queryParamsDictionary = new Dictionary < string , string > ( ) ;
46+ if ( page > 1 )
47+ {
48+ queryParamsDictionary . Add ( "offset" , ( ( page - 1 ) * Constants . DefaultPageSize ) . ToString ( ) ) ;
49+ }
50+
51+ if ( ! string . IsNullOrWhiteSpace ( searchQuery ) )
52+ {
53+ queryParamsDictionary . Add ( "search" , HttpUtility . UrlEncode ( searchQuery ) ) ;
54+ }
55+
56+ return queryParamsDictionary . Count == 0
57+ ? uri
58+ : string . Format ( "{0}&{1}" , uri , string . Join ( "&" , queryParamsDictionary . Select ( kvp => $ "{ kvp . Key } ={ kvp . Value } ") ) ) ;
59+ }
4160 }
4261}
0 commit comments