Skip to content

Commit

Permalink
add countries module with custom repository
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 19, 2024
1 parent 513486d commit 85a3e5a
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 9 deletions.
2 changes: 2 additions & 0 deletions api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { UsersModule } from '@api/modules/users/users.module';
import { APP_FILTER } from '@nestjs/core';
import { AllExceptionsFilter } from '@api/filters/all-exceptions.exception.filter';
import { TsRestModule } from '@ts-rest/nest';
import { CountriesModule } from './modules/countries/countries.module';

@Module({
imports: [
Expand All @@ -27,6 +28,7 @@ import { TsRestModule } from '@ts-rest/nest';
AdminModule,
ImportModule,
UsersModule,
CountriesModule,
],
controllers: [AppController],
providers: [
Expand Down
4 changes: 4 additions & 0 deletions api/src/modules/countries/countries.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Controller } from '@nestjs/common';

@Controller('countries')
export class CountriesController {}
16 changes: 16 additions & 0 deletions api/src/modules/countries/countries.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Country } from '@shared/entities/countries/country.entity';
import { CountriesController } from '@api/modules/countries/countries.controller';

import { MapController } from './map/map.controller';
import { CountryRepository } from '@api/modules/countries/countries.repository';
import { MapRepository } from '@api/modules/countries/map/map.repository';

@Module({
imports: [TypeOrmModule.forFeature([Country])],
controllers: [CountriesController, MapController],
providers: [CountryRepository, MapRepository],
exports: [],
})
export class CountriesModule {}
14 changes: 14 additions & 0 deletions api/src/modules/countries/countries.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Country } from '@shared/entities/countries/country.entity';

@Injectable()
export class CountryRepository extends Repository<Country> {
constructor(
@InjectRepository(Country)
private readonly repository: Repository<Country>,
) {
super(repository.target, repository.manager, repository.queryRunner);
}
}
9 changes: 9 additions & 0 deletions api/src/modules/countries/map/map.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Controller, Get } from '@nestjs/common';

@Controller('map')
export class MapController {
@Get('/geojson')
getGeoJson() {
return 'This will return the geojson of the countries';
}
}
14 changes: 14 additions & 0 deletions api/src/modules/countries/map/map.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Country } from '@shared/entities/countries/country.entity';

@Injectable()
export class MapRepository extends Repository<Country> {
constructor(
@InjectRepository(Country)
private readonly repository: Repository<Country>,
) {
super(repository.target, repository.manager, repository.queryRunner);
}
}
3 changes: 2 additions & 1 deletion api/src/modules/import/import.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, ServiceUnavailableException } from '@nestjs/common';
import { XlsxParser } from '@api/modules/import/services/xlsx.parser';
import { EntityPreprocessor } from '@api/modules/import/services/entity.preprocessor';
import { BaseDataRepository } from '@api/modules/model/base-data.repository';
Expand All @@ -21,6 +21,7 @@ export class ImportService {
return dbResult;
} catch (e) {
console.log(e);
throw new ServiceUnavailableException('Error importing data');
} finally {
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/src/modules/import/services/entity.preprocessor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { BaseData } from '@api/modules/model/base-data.entity';
import { Country } from '@api/modules/model/entities/country.entity';
import { Country } from '@shared/entities/countries/country.entity';

export type ParsedDBEntities = {
countries: Country[];
Expand Down
2 changes: 1 addition & 1 deletion api/src/modules/model/base-data.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ManyToOne,
JoinColumn,
} from 'typeorm';
import { Country } from '@api/modules/model/entities/country.entity';
import { Country } from '@shared/entities/countries/country.entity';

export enum ECOSYSTEM {
MANGROVE = 'Mangrove',
Expand Down
2 changes: 1 addition & 1 deletion api/src/modules/model/base-data.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Injectable } from '@nestjs/common';

import { ParsedDBEntities } from '@api/modules/import/services/entity.preprocessor';
import { BaseData } from '@api/modules/model/base-data.entity';
import { Country } from '@api/modules/model/entities/country.entity';
import {
COST_INPUT_TYPE,
CostInput,
Expand All @@ -12,6 +11,7 @@ import {
CARBON_INPUT_TYPE,
CarbonInputEntity,
} from '@api/modules/model/entities/carbon-input.entity';
import { Country } from '@shared/entities/countries/country.entity';

@Injectable()
export class BaseDataRepository extends Repository<BaseData> {
Expand Down
2 changes: 1 addition & 1 deletion api/src/modules/model/entities/carbon-input.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
BaseEntity,
} from 'typeorm';
import { ACTIVITY, ECOSYSTEM } from '@api/modules/model/base-data.entity';
import { Country } from '@api/modules/model/entities/country.entity';
import { Country } from '@shared/entities/countries/country.entity';

export enum CARBON_INPUT_TYPE {
ECOSYSTEM_EXTENT = 'ecosystem_extent',
Expand Down
2 changes: 1 addition & 1 deletion api/src/modules/model/entities/cost-input.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
BaseEntity,
} from 'typeorm';
import { ACTIVITY, ECOSYSTEM } from '@api/modules/model/base-data.entity';
import { Country } from '@api/modules/model/entities/country.entity';
import { Country } from '@shared/entities/countries/country.entity';

export enum COST_INPUT_TYPE {
PROJECT_SIZE_HA = 'project_size_ha',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Column, Entity, PrimaryColumn, BaseEntity } from 'typeorm';
import {
Column,
Entity,
PrimaryColumn,
BaseEntity,
Geometry,
Index,
} from 'typeorm';

export enum CONTINENTS {
AFRICA = 'Africa',
Expand Down Expand Up @@ -34,4 +41,13 @@ export class Country extends BaseEntity {

@Column({ name: 'hdi', type: 'int', nullable: true })
hdi?: number;

@Index({ spatial: true })
@Column({
type: 'geometry',
srid: 4326,
// TODO: Make it nullable false once we have all the data
nullable: true,
})
geometry: Geometry;
}
3 changes: 2 additions & 1 deletion shared/entities/database.entities.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { User } from "@shared/entities/users/user.entity";
import { Country } from "@shared/entities/countries/country.entity";

export const COMMON_DATABASE_ENTITIES = [User];
export const COMMON_DATABASE_ENTITIES = [User, Country];
2 changes: 1 addition & 1 deletion shared/lib/db-entities.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { User } from "@shared/entities/users/user.entity";
import { ApiEventsEntity } from "@api/modules/api-events/api-events.entity";
import { Country } from "@api/modules/model/entities/country.entity";
import { BaseData } from "@api/modules/model/base-data.entity";
import { CostInput } from "@api/modules/model/entities/cost-input.entity";
import { CarbonInputEntity } from "@api/modules/model/entities/carbon-input.entity";
import { Country } from "@shared/entities/countries/country.entity";

export const DB_ENTITIES = [
User,
Expand Down

0 comments on commit 85a3e5a

Please sign in to comment.