From c58de1b033e40141e7a26f5824f1168dd24cd9ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 2 May 2024 10:57:10 -0600 Subject: [PATCH 1/7] agregando Town a el modulo --- backend/src/database-seeder/database-seeder.module.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/src/database-seeder/database-seeder.module.ts b/backend/src/database-seeder/database-seeder.module.ts index 1b3475fe..eb8e2f96 100644 --- a/backend/src/database-seeder/database-seeder.module.ts +++ b/backend/src/database-seeder/database-seeder.module.ts @@ -3,9 +3,10 @@ import { DatabaseSeederService } from './database-seeder.service'; import { TypeOrmModule } from '@nestjs/typeorm'; import { State } from 'src/state/entities/state.entity'; import { StateService } from 'src/state/state.service'; +import { Town } from 'src/town/entities/town.entity'; @Module({ providers: [DatabaseSeederService, StateService], - imports: [TypeOrmModule.forFeature([State])], + imports: [TypeOrmModule.forFeature([State, Town])], }) export class DatabaseSeederModule {} -- GitLab From 00a5ff00a9f5de1c1a88e5533bf99a4837fa5dce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 2 May 2024 10:57:33 -0600 Subject: [PATCH 2/7] habilitando en loggeo de typeorm --- backend/src/app.module.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 4cb9adc6..5a809a0c 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -17,6 +17,7 @@ import { join } from 'path'; import { Town } from './town/entities/town.entity'; import { TownModule } from './town/town.module'; import { TownTraduction } from './town/entities/town-traduction.entity'; + @Module({ imports: [ TypeOrmModule.forRoot({ @@ -28,6 +29,7 @@ import { TownTraduction } from './town/entities/town-traduction.entity'; database: DbConstants.DB_NAME, entities: [Admin, User, State, Town, TownTraduction], synchronize: DbConstants.DB_SYNC, + logging: true, }), AuthAdminModule, AdminModule, @@ -42,5 +44,6 @@ import { TownTraduction } from './town/entities/town-traduction.entity'; ], controllers: [AppController], providers: [AppService, DatabaseSeederModule], + exports: [TypeOrmModule], }) export class AppModule {} -- GitLab From 53b3f5b6310a654168da18e825919299099be67d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 2 May 2024 10:58:17 -0600 Subject: [PATCH 3/7] agregando dto de la respuesta al obtener un pueblo --- backend/src/town/dto/town-res.dto.ts | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 backend/src/town/dto/town-res.dto.ts diff --git a/backend/src/town/dto/town-res.dto.ts b/backend/src/town/dto/town-res.dto.ts new file mode 100644 index 00000000..a573354a --- /dev/null +++ b/backend/src/town/dto/town-res.dto.ts @@ -0,0 +1,7 @@ +export class TownResDto { + townId: number; + name: string; + description: string; + imageName: string; + stateId: number; +} -- GitLab From f5e8bd254a6136d7dd798d8ad93783bfef8a2167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 2 May 2024 10:59:12 -0600 Subject: [PATCH 4/7] agregando townTraduction a los imports --- backend/src/town/town.module.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/src/town/town.module.ts b/backend/src/town/town.module.ts index 528cfcc3..3bcef4a4 100644 --- a/backend/src/town/town.module.ts +++ b/backend/src/town/town.module.ts @@ -9,7 +9,8 @@ import { TownTraduction } from './entities/town-traduction.entity'; @Module({ controllers: [TownController], - providers: [TownService, StateService], - imports: [TypeOrmModule.forFeature([Town, State, TownTraduction])], + providers: [TownService, StateService, TypeOrmModule], + imports: [TypeOrmModule.forFeature([Town, State, TownTraduction]), TypeOrmModule], + exports: [TownService], }) export class TownModule {} -- GitLab From 473dc400c9b3575b58e7d224a0b12504f6a62b98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 2 May 2024 11:03:26 -0600 Subject: [PATCH 5/7] =?UTF-8?q?corrigiendo=20relaci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/town/entities/town-traduction.entity.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/src/town/entities/town-traduction.entity.ts b/backend/src/town/entities/town-traduction.entity.ts index 52f55877..77922ec5 100644 --- a/backend/src/town/entities/town-traduction.entity.ts +++ b/backend/src/town/entities/town-traduction.entity.ts @@ -1,10 +1,10 @@ -import { Entity, Column, OneToOne, PrimaryColumn } from 'typeorm'; +import { Entity, Column, PrimaryColumn, ManyToOne } from 'typeorm'; import { Town } from './town.entity'; import { LANGUAGES } from 'src/enum/languages.enum'; @Entity() export class TownTraduction { - @PrimaryColumn() - @OneToOne(() => Town, (town) => town.townId) + @PrimaryColumn({ name: 'townId' }) + @ManyToOne(() => Town, (town) => town.townId) townId: number; @PrimaryColumn() language: LANGUAGES; -- GitLab From 80bab47c9fd6105f40fddc16679d2dc0a55da7ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 2 May 2024 11:03:57 -0600 Subject: [PATCH 6/7] agregando metodo que regresa towns by state --- backend/src/town/town.service.ts | 34 +++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/backend/src/town/town.service.ts b/backend/src/town/town.service.ts index e547f2da..6f04b289 100644 --- a/backend/src/town/town.service.ts +++ b/backend/src/town/town.service.ts @@ -2,17 +2,22 @@ import { BadRequestException, Injectable } from '@nestjs/common'; import { CreateTownDto } from './dto/create-town.dto'; import { Repository } from 'typeorm'; import { Town } from './entities/town.entity'; -import { InjectRepository } from '@nestjs/typeorm'; +import { InjectDataSource, InjectRepository } from '@nestjs/typeorm'; import { StateService } from 'src/state/state.service'; import { TownTraduction } from './entities/town-traduction.entity'; import { CreateTownTraductionDto } from './dto/create-town-trad.dto'; import { LANGUAGES } from 'src/enum/languages.enum'; +import { DataSource } from 'typeorm'; +import { ServerConstants } from 'src/constants/server.contants'; +import { TownResDto } from './dto/town-res.dto'; @Injectable() export class TownService { constructor( @InjectRepository(Town) private townRepository: Repository, @InjectRepository(TownTraduction) private townTradRepository: Repository, + @InjectDataSource() private dataSource: DataSource, + private state: StateService, ) {} @@ -38,6 +43,33 @@ export class TownService { await this.townTradRepository.save(createTownTradDtoEN); } + async findTownsByState(stateId: number, lang: string): Promise { + const ans = await this.dataSource + .getRepository(TownTraduction) + .createQueryBuilder('townTrad') + .leftJoin('townTrad.townId', 'town') + .select([ + 'town.townId AS townId', + 'town.name AS name', + 'town.imageName AS imageName', + 'townTrad.language AS language', + 'townTrad.description AS description', + 'town.state AS stateId', + ]) + .where('town.state = :stateId', { stateId }) + .andWhere('townTrad.language = :language', { language: lang }) + .andWhere('town.active = true') + .getRawMany(); + + return ans.map((item: TownResDto) => ({ + townId: item.townId, + name: item.name, + imageName: `${ServerConstants.HOST}/towns/${item.imageName}`, + description: item.description, + stateId: item.stateId, + })); + } + findAll() { return `This action returns all town`; } -- GitLab From eeb6a2626e1bffd7fbcba46f6d2e93faf3b0d8c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 2 May 2024 11:04:15 -0600 Subject: [PATCH 7/7] agregando endpoint que regresa los pueblos --- backend/src/town/town.controller.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/backend/src/town/town.controller.ts b/backend/src/town/town.controller.ts index 19e1459f..dacf583d 100644 --- a/backend/src/town/town.controller.ts +++ b/backend/src/town/town.controller.ts @@ -1,17 +1,20 @@ -import { Controller, Get, Post, Param, Delete, UseInterceptors, UploadedFile, Body } from '@nestjs/common'; +import { Controller, Get, Post, Param, Delete, UseInterceptors, UploadedFile, Body, Query } from '@nestjs/common'; import { TownService } from './town.service'; import { ApiBody, ApiConsumes, ApiTags } from '@nestjs/swagger'; import { FileValidationPipe } from 'src/shared/pipe/file-validation.pipe'; import { fileInterceptor } from 'src/shared/interceptors/file-save.interceptor'; import { CreateTownDto } from './dto/create-town.dto'; -@Controller('town') +@Controller() @ApiTags('Agregar un pueblo') export class TownController { - constructor(private readonly townService: TownService) {} + constructor( + private readonly townService: TownService, + // private readonly stateService: StateService, + ) {} @ApiBody({ type: CreateTownDto }) @ApiConsumes('multipart/form-data') - @Post() + @Post('town') @UseInterceptors(fileInterceptor('image', 'static/towns/', ['.jpg', '.jpeg', '.png'])) async create(@UploadedFile(new FileValidationPipe()) file, @Body() createTownDto: CreateTownDto) { try { @@ -23,6 +26,16 @@ export class TownController { } } + @Get('state/:stateId/town') + async findTownsByState(@Param('stateId') stateId: number, @Query('lang') lang) { + try { + stateId = parseInt(stateId.toString()); + return await this.townService.findTownsByState(stateId, lang); + } catch (error) { + throw error; + } + } + @Get() findAll() { return this.townService.findAll(); -- GitLab