@@ -4,12 +4,11 @@ import { map, Observable } from 'rxjs';
44import { environment } from '../../../environments/environment' ;
55import { PagingResponse } from '../models/paging-response' ;
66import { ListingUploadHistoryRecord } from '../models/listing-upload-history-record' ;
7- import { ListingTableRow } from '../models/listing-table-row' ;
7+ import { AggregatedListingTableRow , ListingTableRow } from '../models/listing-table-row' ;
88import { ListingSearchRequest } from '../models/listing-search-request' ;
99import { ListingAddressCandidate , ListingDetails } from '../models/listing-details' ;
1010import { ExportJurisdiction } from '../models/export-listing' ;
1111import { ListingFilter } from '../models/listing-filter' ;
12- import { ListingResponseWithCounts , AggregatedListingResponseWithCounts } from '../models/listing-response-with-counts' ;
1312
1413@Injectable ( {
1514 providedIn : 'root' ,
@@ -73,90 +72,114 @@ export class ListingDataService {
7372 ) ;
7473 }
7574
76- getListings (
77- pageNumber : number = 1 ,
78- pageSize : number = 10 ,
79- orderBy : string = '' ,
80- direction : 'asc' | 'desc' = 'asc' ,
81- searchReq : ListingSearchRequest = { } ,
75+ /**
76+ * Builds query string for listings filter params (searchReq, filter, recent).
77+ * Used by both getListings and getListingsCount.
78+ */
79+ private buildListingsFilterParams (
80+ searchReq : ListingSearchRequest ,
8281 filter ?: ListingFilter ,
8382 recent : boolean = false ,
84- ) : Observable < ListingResponseWithCounts < ListingTableRow > > {
85- let endpointUrl = `${ environment . API_HOST } /rentallistings?pageSize=${ pageSize } &pageNumber=${ pageNumber } ` ;
86-
87- if ( orderBy ) {
88- endpointUrl += `&orderBy=${ orderBy } &direction=${ direction } ` ;
89- }
90-
83+ ) : string {
84+ const params : string [ ] = [ ] ;
9185 if ( recent ) {
92- endpointUrl += `& recent=${ recent } `;
86+ params . push ( ` recent=${ recent } `) ;
9387 }
94-
9588 if ( searchReq . all ) {
96- endpointUrl += `& all=${ searchReq . all } ` ;
89+ params . push ( ` all=${ encodeURIComponent ( searchReq . all ) } ` ) ;
9790 }
9891 if ( searchReq . address ) {
99- endpointUrl += `& address=${ searchReq . address } ` ;
92+ params . push ( ` address=${ encodeURIComponent ( searchReq . address ) } ` ) ;
10093 }
10194 if ( searchReq . url ) {
102- endpointUrl += `& url=${ searchReq . url } ` ;
95+ params . push ( ` url=${ encodeURIComponent ( searchReq . url ) } ` ) ;
10396 }
10497 if ( searchReq . listingId ) {
105- endpointUrl += `& listingId=${ searchReq . listingId } ` ;
98+ params . push ( ` listingId=${ encodeURIComponent ( searchReq . listingId ) } ` ) ;
10699 }
107100 if ( searchReq . hostName ) {
108- endpointUrl += `& hostName=${ searchReq . hostName } ` ;
101+ params . push ( ` hostName=${ encodeURIComponent ( searchReq . hostName ) } ` ) ;
109102 }
110103 if ( searchReq . businessLicence ) {
111- endpointUrl += `& businessLicence=${ searchReq . businessLicence } ` ;
104+ params . push ( ` businessLicence=${ encodeURIComponent ( searchReq . businessLicence ) } ` ) ;
112105 }
113106 if ( searchReq . registrationNumber ) {
114- endpointUrl += `& registrationNumber=${ searchReq . registrationNumber } ` ;
107+ params . push ( ` registrationNumber=${ encodeURIComponent ( searchReq . registrationNumber ) } ` ) ;
115108 }
116-
117109 if ( filter ) {
118110 if ( filter . byLocation ) {
119111 if ( ! ! filter . byLocation ?. isPrincipalResidenceRequired ) {
120- endpointUrl += `&prRequirement=${ filter . byLocation . isPrincipalResidenceRequired == 'Yes'
121- } `;
112+ params . push ( `prRequirement=${ filter . byLocation . isPrincipalResidenceRequired == 'Yes' } ` ) ;
122113 }
123114 if ( ! ! filter . byLocation ?. isBusinessLicenceRequired ) {
124- endpointUrl += `&blRequirement=${ filter . byLocation . isBusinessLicenceRequired == 'Yes'
125- } `;
115+ params . push ( `blRequirement=${ filter . byLocation . isBusinessLicenceRequired == 'Yes' } ` ) ;
126116 }
127117 }
128118 if ( filter . byStatus ) {
129119 if (
130120 filter . byStatus . reassigned !== null &&
131121 filter . byStatus . reassigned !== undefined
132122 ) {
133- endpointUrl += `& reassigned=${ ! ! filter . byStatus . reassigned } `;
123+ params . push ( ` reassigned=${ ! ! filter . byStatus . reassigned } `) ;
134124 }
135125 if (
136126 filter . byStatus . takedownComplete !== null &&
137127 filter . byStatus . takedownComplete !== undefined
138128 ) {
139- endpointUrl += `& takedownComplete=${ ! ! filter . byStatus . takedownComplete } `;
129+ params . push ( ` takedownComplete=${ ! ! filter . byStatus . takedownComplete } `) ;
140130 }
141-
142131 const statuses = new Array < string > ( ) ;
143132 if ( filter . byStatus . active ) {
144133 statuses . push ( 'A' ) ;
145- statuses . push ( 'U' ) ; // Include Update when Active is selected
134+ statuses . push ( 'U' ) ;
146135 }
147136 if ( filter . byStatus . inactive ) statuses . push ( 'I' ) ;
148137 if ( filter . byStatus . new ) statuses . push ( 'N' ) ;
149-
150138 if ( statuses . length ) {
151- endpointUrl += `& statuses=${ statuses . join ( ',' ) } `;
139+ params . push ( ` statuses=${ statuses . join ( ',' ) } `) ;
152140 }
153141 }
154142 if ( ! ! filter . community ) {
155- endpointUrl += `& lgId=${ filter . community } `;
143+ params . push ( ` lgId=${ filter . community } `) ;
156144 }
157145 }
146+ return params . length ? '&' + params . join ( '&' ) : '' ;
147+ }
148+
149+ getListingsCount (
150+ searchReq : ListingSearchRequest = { } ,
151+ filter ?: ListingFilter ,
152+ recent : boolean = false ,
153+ ) : Observable < number > {
154+ const baseUrl = `${ environment . API_HOST } /rentallistings/count` ;
155+ const filterParams = this . buildListingsFilterParams ( searchReq , filter , recent ) ;
156+ const url = filterParams ? `${ baseUrl } ?${ filterParams . slice ( 1 ) } ` : baseUrl ;
157+ return this . httpClient . get < number > ( url ) ;
158+ }
159+
160+ getListings (
161+ pageNumber : number = 1 ,
162+ pageSize : number = 10 ,
163+ orderBy : string = '' ,
164+ direction : 'asc' | 'desc' = 'asc' ,
165+ searchReq : ListingSearchRequest = { } ,
166+ filter ?: ListingFilter ,
167+ recent : boolean = false ,
168+ includeTotalCount : boolean = true ,
169+ ) : Observable < PagingResponse < ListingTableRow > > {
170+ let endpointUrl = `${ environment . API_HOST } /rentallistings?pageSize=${ pageSize } &pageNumber=${ pageNumber } ` ;
171+
172+ if ( orderBy ) {
173+ endpointUrl += `&orderBy=${ orderBy } &direction=${ direction } ` ;
174+ }
175+
176+ if ( ! includeTotalCount ) {
177+ endpointUrl += '&includeTotalCount=false' ;
178+ }
179+
180+ endpointUrl += this . buildListingsFilterParams ( searchReq , filter , recent ) ;
158181
159- return this . httpClient . get < ListingResponseWithCounts < ListingTableRow > > ( endpointUrl ) ;
182+ return this . httpClient . get < PagingResponse < ListingTableRow > > ( endpointUrl ) ;
160183 }
161184
162185 getHostListingsCount ( primaryHostNm : string ) : Observable < { primaryHostNm : string , hasMultipleProperties : boolean } > {
@@ -168,7 +191,7 @@ export class ListingDataService {
168191 searchReq : ListingSearchRequest = { } ,
169192 filter ?: ListingFilter ,
170193 recent : boolean = false ,
171- ) : Observable < AggregatedListingResponseWithCounts > {
194+ ) : Observable < AggregatedListingTableRow [ ] > {
172195 let listingsEndpointUrl = `${ environment . API_HOST } /rentallistings/grouped` ;
173196 const params : string [ ] = [ ] ;
174197
@@ -242,8 +265,7 @@ export class ListingDataService {
242265 listingsEndpointUrl += `?${ params . join ( '&' ) } ` ;
243266 }
244267
245- // API now returns object with data, recentCount, and allCount
246- return this . httpClient . get < AggregatedListingResponseWithCounts > ( listingsEndpointUrl ) ;
268+ return this . httpClient . get < AggregatedListingTableRow [ ] > ( listingsEndpointUrl ) ;
247269 }
248270
249271 getListingDetailsById ( id : number ) : Observable < ListingDetails > {
0 commit comments