Compare commits
No commits in common. "5a44c81b8eed01e261949815f4f050ac41a8de14" and "750e3c54cbfa7896e1f9faefb9b0a9751b83559b" have entirely different histories.
5a44c81b8e
...
750e3c54cb
@ -9,8 +9,8 @@ export class CountriesController {
|
|||||||
constructor(private readonly service: CountriesService) {}
|
constructor(private readonly service: CountriesService) {}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
async getAllCountries(@Query() query: CountriesQueryDto) {
|
async getAllCountries(@Query() { q, fields }: CountriesQueryDto) {
|
||||||
return this.service.search(query);
|
return this.service.search(q, fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(':iso')
|
@Get(':iso')
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
export interface CountriesQueryDto {
|
export interface CountriesQueryDto {
|
||||||
fields?: string[];
|
fields?: string[];
|
||||||
q?: string;
|
q?: string;
|
||||||
limit?: string;
|
|
||||||
offset?: string;
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ import { InjectRepository } from '@nestjs/typeorm';
|
|||||||
import { ILike, Repository } from 'typeorm';
|
import { ILike, Repository } from 'typeorm';
|
||||||
import { Country } from './countries.entity';
|
import { Country } from './countries.entity';
|
||||||
import { intOrNull } from 'src/utils/int-or-null';
|
import { intOrNull } from 'src/utils/int-or-null';
|
||||||
import { CountriesQueryDto } from './countries.interfaces';
|
|
||||||
|
|
||||||
const COUNTRIES_URL =
|
const COUNTRIES_URL =
|
||||||
'https://download.geonames.org/export/dump/countryInfo.txt';
|
'https://download.geonames.org/export/dump/countryInfo.txt';
|
||||||
@ -84,17 +83,9 @@ export class CountriesService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async search({
|
async search(query?: string, fields = ACCEPT_FIELDS) {
|
||||||
q: query,
|
|
||||||
fields = ACCEPT_FIELDS,
|
|
||||||
limit,
|
|
||||||
offset,
|
|
||||||
}: CountriesQueryDto) {
|
|
||||||
const select = this.mapAllowedQuery(fields);
|
const select = this.mapAllowedQuery(fields);
|
||||||
const filter = {};
|
const filter = {};
|
||||||
const take = Math.max(Math.min(intOrNull(limit as string) || 50, 1000), 1);
|
|
||||||
|
|
||||||
const skip = intOrNull(offset as string) || 0;
|
|
||||||
|
|
||||||
if (query) {
|
if (query) {
|
||||||
filter['country'] = ILike(`%${query}%`);
|
filter['country'] = ILike(`%${query}%`);
|
||||||
@ -103,8 +94,6 @@ export class CountriesService {
|
|||||||
return this.countryRepository.find({
|
return this.countryRepository.find({
|
||||||
where: filter,
|
where: filter,
|
||||||
select,
|
select,
|
||||||
skip,
|
|
||||||
take,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Controller, Get, Query } from '@nestjs/common';
|
import { Controller, Get, Query } from '@nestjs/common';
|
||||||
import { FeatureQuery, GeonameQuery } from './geonames.interfaces';
|
import { GeonameQuery } from './geonames.interfaces';
|
||||||
import { GeonamesService } from './geonames.service';
|
import { GeonamesService } from './geonames.service';
|
||||||
|
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -12,9 +12,4 @@ export class GeonamesController {
|
|||||||
async search(@Query() query: GeonameQuery) {
|
async search(@Query() query: GeonameQuery) {
|
||||||
return this.service.search(query);
|
return this.service.search(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('features')
|
|
||||||
async features(@Query() query: FeatureQuery) {
|
|
||||||
return this.service.getFeatureList(query);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,3 @@ export type GeonameQuery = Record<keyof Geoname, unknown> & {
|
|||||||
offset?: string;
|
offset?: string;
|
||||||
q?: string;
|
q?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface FeatureQuery {
|
|
||||||
q?: string;
|
|
||||||
class?: string;
|
|
||||||
}
|
|
||||||
|
@ -3,17 +3,12 @@ import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
|
|||||||
import { DataSource, ILike, In, Repository } from 'typeorm';
|
import { DataSource, ILike, In, Repository } from 'typeorm';
|
||||||
import { pipeline } from 'stream/promises';
|
import { pipeline } from 'stream/promises';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import {
|
import { GeonameQuery, WorkDirectory } from './geonames.interfaces';
|
||||||
FeatureQuery,
|
|
||||||
GeonameQuery,
|
|
||||||
WorkDirectory,
|
|
||||||
} from './geonames.interfaces';
|
|
||||||
import { createInterface } from 'readline';
|
import { createInterface } from 'readline';
|
||||||
import { Geoname } from './geonames.entity';
|
import { Geoname } from './geonames.entity';
|
||||||
import { intOrNull } from 'src/utils/int-or-null';
|
import { intOrNull } from 'src/utils/int-or-null';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as unzipper from 'unzipper';
|
import * as unzipper from 'unzipper';
|
||||||
import { GeoFeature } from './feature.dictionary';
|
|
||||||
|
|
||||||
const GEONAMES_DUMP =
|
const GEONAMES_DUMP =
|
||||||
'https://download.geonames.org/export/dump/allCountries.zip';
|
'https://download.geonames.org/export/dump/allCountries.zip';
|
||||||
@ -221,33 +216,4 @@ export class GeonamesService {
|
|||||||
|
|
||||||
Logger.log('Done!', context);
|
Logger.log('Done!', context);
|
||||||
}
|
}
|
||||||
|
|
||||||
getFeatureList({ q, class: className }: FeatureQuery) {
|
|
||||||
return Object.keys(GeoFeature)
|
|
||||||
.filter((code) => {
|
|
||||||
if (!className) return true;
|
|
||||||
return GeoFeature[code][0] === className.toUpperCase();
|
|
||||||
})
|
|
||||||
.filter((code) => {
|
|
||||||
if (!q) return true;
|
|
||||||
const lower = q.toLowerCase();
|
|
||||||
return (
|
|
||||||
code.includes(lower) ||
|
|
||||||
GeoFeature[code][1]?.includes(lower) ||
|
|
||||||
GeoFeature[code][2]?.includes(lower)
|
|
||||||
);
|
|
||||||
})
|
|
||||||
.reduce(
|
|
||||||
(prev, curr) => [
|
|
||||||
...prev,
|
|
||||||
{
|
|
||||||
code: curr,
|
|
||||||
class: GeoFeature[curr][0],
|
|
||||||
name: GeoFeature[curr][1],
|
|
||||||
description: GeoFeature[curr][2],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
import { Module } from '@nestjs/common';
|
|
||||||
import { TimezonesService } from './timezones.service';
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
providers: [TimezonesService],
|
|
||||||
exports: [TimezonesService],
|
|
||||||
})
|
|
||||||
export class TimezonesModule {}
|
|
@ -1,4 +0,0 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class TimezonesService {}
|
|
Loading…
Reference in New Issue
Block a user