generated from Greenstand/treetracker-microservice-template
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from dagmawig/main
feat: implement countries/v2 and countries/v2/[id]
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import Country from 'interfaces/Country'; | ||
import HttpError from 'utils/HttpError'; | ||
import BaseRepository from './BaseRepository'; | ||
import Session from './Session'; | ||
|
||
type Filter = Partial<{ lat: number; lon: number }>; | ||
|
||
export default class CountryRepositoryV2 extends BaseRepository<Country> { | ||
constructor(session: Session) { | ||
super('region', session); | ||
} | ||
|
||
async getById(id: string | number) { | ||
const object = await this.session | ||
.getDB() | ||
.select( | ||
this.session.getDB().raw(` | ||
id, | ||
name, | ||
St_asgeojson(centroid) as centroid | ||
`), | ||
) | ||
.table(this.tableName) | ||
.where('id', id) | ||
.first(); | ||
if (!object) { | ||
throw new HttpError(404, `Can not found ${this.tableName} by id:${id}`); | ||
} | ||
return object; | ||
} | ||
|
||
async getByFilter( | ||
filter: Filter, | ||
// options?: { limit?: number | undefined } | undefined, | ||
): Promise<Country[]> { | ||
const { lat, lon } = filter; | ||
const sql = ` | ||
WITH country_id AS ( | ||
select id from region_type where type = 'country' | ||
) | ||
SELECT | ||
id, | ||
name, | ||
St_asgeojson(centroid) as centroid | ||
FROM | ||
region | ||
WHERE | ||
ST_Contains(geom, ST_GeomFromText('POINT(${lon} ${lat})', 4326)) = true | ||
AND | ||
type_id in (select id from country_id); | ||
`; | ||
const object = await this.session.getDB().raw(sql); | ||
if (!object || object.rows.length <= 0) { | ||
throw new HttpError( | ||
404, | ||
`Can not found ${this.tableName} by lat:${lat} lon:${lon}`, | ||
); | ||
} | ||
return object.rows; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import CountryRepositoryV2 from 'infra/database/CountryRepositoryV2'; | ||
import Country from 'interfaces/Country'; | ||
import { delegateRepository } from '../infra/database/delegateRepository'; | ||
|
||
type Filter = Partial<{ lat: number; lon: number }>; | ||
|
||
function getCountries( | ||
countryRepository: CountryRepositoryV2, | ||
): (filter: Filter) => Promise<Country[]> { | ||
return async function (filter: Filter) { | ||
const countries = await countryRepository.getByFilter(filter); | ||
return countries; | ||
}; | ||
} | ||
|
||
export default { | ||
getCountries, | ||
getById: delegateRepository<CountryRepositoryV2, Country>('getById'), | ||
getByFilter: delegateRepository<CountryRepositoryV2, Country>('getByFilter'), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters