diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 4cb9adc60459df315c12580eb4a77f134a694e56..5a809a0cd1ddd58c0565a2d9f2abdb8740af4c8e 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 {} diff --git a/backend/src/database-seeder/database-seeder.module.ts b/backend/src/database-seeder/database-seeder.module.ts index 1b3475feb550dcd2ad0398aa8191374c76d5500e..eb8e2f96fc6ee64c031311e8a1c863ec85df63d7 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 {} 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 0000000000000000000000000000000000000000..a573354a61140ac991882ef8583051a6d708566e --- /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; +} diff --git a/backend/src/town/entities/town-traduction.entity.ts b/backend/src/town/entities/town-traduction.entity.ts index 52f5587761833af05cc412792bb099dce033c6c6..77922ec5f9f65a200eb91eb90e48743c7303bff3 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; diff --git a/backend/src/town/town.controller.ts b/backend/src/town/town.controller.ts index 19e1459fb33cbb373fb4cb392d897607ceb5ee5d..dacf583d9f6f41a5f896a8fba7af1da12a06a687 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(); diff --git a/backend/src/town/town.module.ts b/backend/src/town/town.module.ts index 528cfcc3ecd4b8a5ed6112f6bcdb0538aa901b10..3bcef4a42bd75212908304ccb38fcc2fa24250c5 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 {} diff --git a/backend/src/town/town.service.ts b/backend/src/town/town.service.ts index e547f2daf2e1b007417870762091e4616cbc8647..6f04b28920c60e87e53be13650c5976aae8bf472 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`; }