counties, pagination, filter count, show total results
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { readFileSync } from 'fs';
|
||||
import path, { join } from 'path';
|
||||
import { GeoResult } from 'src/models/main.model.js';
|
||||
import { CountyResult, GeoResult } from 'src/models/main.model.js';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { City, Geo, State } from '../models/server.model.js';
|
||||
import { City, CountyData, Geo, State } from '../models/server.model.js';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
@@ -11,6 +11,7 @@ const __dirname = path.dirname(__filename);
|
||||
@Injectable()
|
||||
export class GeoService {
|
||||
geo: Geo;
|
||||
counties: CountyData[];
|
||||
constructor() {
|
||||
this.loadGeo();
|
||||
}
|
||||
@@ -18,9 +19,32 @@ export class GeoService {
|
||||
const filePath = join(__dirname, '../..', 'assets', 'geo.json');
|
||||
const rawData = readFileSync(filePath, 'utf8');
|
||||
this.geo = JSON.parse(rawData);
|
||||
const countiesFilePath = join(__dirname, '../..', 'assets', 'counties.json');
|
||||
const rawCountiesData = readFileSync(countiesFilePath, 'utf8');
|
||||
this.counties = JSON.parse(rawCountiesData);
|
||||
}
|
||||
findCountiesStartingWith(prefix: string, states?: string[]) {
|
||||
let results: CountyResult[] = [];
|
||||
let idCounter = 1;
|
||||
|
||||
findCitiesStartingWith(prefix: string, state?: string): { city: string; state: string; state_code: string }[] {
|
||||
this.counties.forEach(stateData => {
|
||||
if (!states || states.includes(stateData.state)) {
|
||||
stateData.counties.forEach(county => {
|
||||
if (county.startsWith(prefix.toUpperCase())) {
|
||||
results.push({
|
||||
id: idCounter++,
|
||||
name: county,
|
||||
state: stateData.state_full,
|
||||
state_code: stateData.state,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return results;
|
||||
}
|
||||
findCitiesStartingWith(prefix: string, state?: string): GeoResult[] {
|
||||
const result: GeoResult[] = [];
|
||||
|
||||
this.geo.states.forEach((state: State) => {
|
||||
|
||||
Reference in New Issue
Block a user