Compare commits

..

No commits in common. "5a44c81b8eed01e261949815f4f050ac41a8de14" and "750e3c54cbfa7896e1f9faefb9b0a9751b83559b" have entirely different histories.

8 changed files with 5 additions and 74 deletions

View File

@ -9,8 +9,8 @@ export class CountriesController {
constructor(private readonly service: CountriesService) {}
@Get()
async getAllCountries(@Query() query: CountriesQueryDto) {
return this.service.search(query);
async getAllCountries(@Query() { q, fields }: CountriesQueryDto) {
return this.service.search(q, fields);
}
@Get(':iso')

View File

@ -1,6 +1,4 @@
export interface CountriesQueryDto {
fields?: string[];
q?: string;
limit?: string;
offset?: string;
}

View File

@ -3,7 +3,6 @@ import { InjectRepository } from '@nestjs/typeorm';
import { ILike, Repository } from 'typeorm';
import { Country } from './countries.entity';
import { intOrNull } from 'src/utils/int-or-null';
import { CountriesQueryDto } from './countries.interfaces';
const COUNTRIES_URL =
'https://download.geonames.org/export/dump/countryInfo.txt';
@ -84,17 +83,9 @@ export class CountriesService {
});
}
async search({
q: query,
fields = ACCEPT_FIELDS,
limit,
offset,
}: CountriesQueryDto) {
async search(query?: string, fields = ACCEPT_FIELDS) {
const select = this.mapAllowedQuery(fields);
const filter = {};
const take = Math.max(Math.min(intOrNull(limit as string) || 50, 1000), 1);
const skip = intOrNull(offset as string) || 0;
if (query) {
filter['country'] = ILike(`%${query}%`);
@ -103,8 +94,6 @@ export class CountriesService {
return this.countryRepository.find({
where: filter,
select,
skip,
take,
});
}

View File

@ -1,5 +1,5 @@
import { Controller, Get, Query } from '@nestjs/common';
import { FeatureQuery, GeonameQuery } from './geonames.interfaces';
import { GeonameQuery } from './geonames.interfaces';
import { GeonamesService } from './geonames.service';
@Controller({
@ -12,9 +12,4 @@ export class GeonamesController {
async search(@Query() query: GeonameQuery) {
return this.service.search(query);
}
@Get('features')
async features(@Query() query: FeatureQuery) {
return this.service.getFeatureList(query);
}
}

View File

@ -11,8 +11,3 @@ export type GeonameQuery = Record<keyof Geoname, unknown> & {
offset?: string;
q?: string;
};
export interface FeatureQuery {
q?: string;
class?: string;
}

View File

@ -3,17 +3,12 @@ import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
import { DataSource, ILike, In, Repository } from 'typeorm';
import { pipeline } from 'stream/promises';
import { join } from 'path';
import {
FeatureQuery,
GeonameQuery,
WorkDirectory,
} from './geonames.interfaces';
import { GeonameQuery, WorkDirectory } from './geonames.interfaces';
import { createInterface } from 'readline';
import { Geoname } from './geonames.entity';
import { intOrNull } from 'src/utils/int-or-null';
import * as fs from 'fs';
import * as unzipper from 'unzipper';
import { GeoFeature } from './feature.dictionary';
const GEONAMES_DUMP =
'https://download.geonames.org/export/dump/allCountries.zip';
@ -221,33 +216,4 @@ export class GeonamesService {
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],
},
],
[],
);
}
}

View File

@ -1,8 +0,0 @@
import { Module } from '@nestjs/common';
import { TimezonesService } from './timezones.service';
@Module({
providers: [TimezonesService],
exports: [TimezonesService],
})
export class TimezonesModule {}

View File

@ -1,4 +0,0 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class TimezonesService {}