Skip to content

Commit

Permalink
feat(collectivites): ajout d'un debut de service pour gerer les colle…
Browse files Browse the repository at this point in the history
…ctivites
  • Loading branch information
dthib committed Jul 22, 2024
1 parent 815cf87 commit 1f7488d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
7 changes: 7 additions & 0 deletions backend/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Controller, Get, NotFoundException } from '@nestjs/common';
import { AppService } from './app.service';
import SheetService from './spreadsheets/services/sheet.service';
import CollectivitesService from './collectivites/services/collectivites.service';

@Controller()
export class AppController {
constructor(
private readonly appService: AppService,
private readonly sheetService: SheetService,
private readonly collectiviteService: CollectivitesService,
) {}

@Get()
Expand All @@ -23,4 +25,9 @@ export class AppController {
getTestResult() {
return this.sheetService.testDownload();
}

@Get('collectivite')
getCollectivite() {
return this.collectiviteService.getEpciBySiren('200043495');
}
}
4 changes: 3 additions & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { AppController } from './app.controller';
import { AppService } from './app.service';
import { SheetModule } from './spreadsheets/sheet.module';
import { CommonModule } from './common/common.module';
import { CollectivitesModule } from './collectivites/collectivites.module';

@Module({
imports: [CommonModule, SheetModule],
imports: [CommonModule, SheetModule, CollectivitesModule],
controllers: [AppController],
providers: [AppService],
})
Expand Down
11 changes: 11 additions & 0 deletions backend/src/collectivites/collectivites.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import CollectivitesService from './services/collectivites.service';
import { CommonModule } from 'src/common/common.module';

@Module({
imports: [CommonModule],
providers: [CollectivitesService],
exports: [CollectivitesService],
controllers: [],
})
export class CollectivitesModule {}
41 changes: 41 additions & 0 deletions backend/src/collectivites/services/collectivites.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Injectable } from '@nestjs/common';
import { eq } from 'drizzle-orm';
import { boolean, integer, pgTable, serial, text } from 'drizzle-orm/pg-core';
import DatabaseService from 'src/common/services/database.service';

@Injectable()
export default class CollectivitesService {
private readonly collectiviteTable = pgTable('collectivite', {
id: serial('id').primaryKey(),
access_restreint: boolean('access_restreint'),
});

private readonly epciTable = pgTable('epci', {
id: serial('id').primaryKey(),
collectivite_id: integer('collectivite_id')
.notNull()
.references(() => this.collectiviteTable.id),
nom: text('nom').notNull(),
siren: text('siren'),
});

constructor(private readonly databaseService: DatabaseService) {}

async getEpciById(id: number) {
return this.databaseService.db
.select()
.from(this.epciTable)
.where(eq(this.epciTable.id, id));
}

async getEpciBySiren(siren: string) {
return this.databaseService.db
.select()
.from(this.epciTable)
.leftJoin(
this.collectiviteTable,
eq(this.epciTable.collectivite_id, this.collectiviteTable.id),
)
.where(eq(this.epciTable.siren, siren));
}
}

0 comments on commit 1f7488d

Please sign in to comment.