From 500379fe5b6fd1e80e06dac8e56dea672ca0ff4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 18 Apr 2024 10:20:18 -0600 Subject: [PATCH 01/21] inicializando town resource --- backend/src/town/dto/create-town.dto.ts | 1 + backend/src/town/dto/update-town.dto.ts | 4 +++ backend/src/town/entities/town.entity.ts | 17 ++++++++++ backend/src/town/town.controller.ts | 42 ++++++++++++++++++++++++ backend/src/town/town.module.ts | 9 +++++ backend/src/town/town.service.ts | 26 +++++++++++++++ 6 files changed, 99 insertions(+) create mode 100644 backend/src/town/dto/create-town.dto.ts create mode 100644 backend/src/town/dto/update-town.dto.ts create mode 100644 backend/src/town/entities/town.entity.ts create mode 100644 backend/src/town/town.controller.ts create mode 100644 backend/src/town/town.module.ts create mode 100644 backend/src/town/town.service.ts diff --git a/backend/src/town/dto/create-town.dto.ts b/backend/src/town/dto/create-town.dto.ts new file mode 100644 index 00000000..3be6400a --- /dev/null +++ b/backend/src/town/dto/create-town.dto.ts @@ -0,0 +1 @@ +export class CreateTownDto {} diff --git a/backend/src/town/dto/update-town.dto.ts b/backend/src/town/dto/update-town.dto.ts new file mode 100644 index 00000000..6982b689 --- /dev/null +++ b/backend/src/town/dto/update-town.dto.ts @@ -0,0 +1,4 @@ +import { PartialType } from '@nestjs/swagger'; +import { CreateTownDto } from './create-town.dto'; + +export class UpdateTownDto extends PartialType(CreateTownDto) {} diff --git a/backend/src/town/entities/town.entity.ts b/backend/src/town/entities/town.entity.ts new file mode 100644 index 00000000..87d18182 --- /dev/null +++ b/backend/src/town/entities/town.entity.ts @@ -0,0 +1,17 @@ +import { State } from 'src/state/entities/state.entity'; +import { Entity, Column, PrimaryColumn, ManyToOne, JoinColumn } from 'typeorm'; +@Entity() +export class Town { + @PrimaryColumn() + townId: number; + + @ManyToOne(() => State, (state) => state.stateId, { nullable: false }) + @JoinColumn() + name: string; + + @Column() + description: string; + + @Column() + imageURL: string; +} diff --git a/backend/src/town/town.controller.ts b/backend/src/town/town.controller.ts new file mode 100644 index 00000000..66d202ab --- /dev/null +++ b/backend/src/town/town.controller.ts @@ -0,0 +1,42 @@ +import { + Controller, + Get, + Post, + Body, + Patch, + Param, + Delete, +} from '@nestjs/common'; +import { TownService } from './town.service'; +import { CreateTownDto } from './dto/create-town.dto'; +import { UpdateTownDto } from './dto/update-town.dto'; + +@Controller('town') +export class TownController { + constructor(private readonly townService: TownService) {} + + @Post() + create(@Body() createTownDto: CreateTownDto) { + return this.townService.create(createTownDto); + } + + @Get() + findAll() { + return this.townService.findAll(); + } + + @Get(':id') + findOne(@Param('id') id: string) { + return this.townService.findOne(+id); + } + + @Patch(':id') + update(@Param('id') id: string, @Body() updateTownDto: UpdateTownDto) { + return this.townService.update(+id, updateTownDto); + } + + @Delete(':id') + remove(@Param('id') id: string) { + return this.townService.remove(+id); + } +} diff --git a/backend/src/town/town.module.ts b/backend/src/town/town.module.ts new file mode 100644 index 00000000..114d14c1 --- /dev/null +++ b/backend/src/town/town.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { TownService } from './town.service'; +import { TownController } from './town.controller'; + +@Module({ + controllers: [TownController], + providers: [TownService], +}) +export class TownModule {} diff --git a/backend/src/town/town.service.ts b/backend/src/town/town.service.ts new file mode 100644 index 00000000..96e0bd87 --- /dev/null +++ b/backend/src/town/town.service.ts @@ -0,0 +1,26 @@ +import { Injectable } from '@nestjs/common'; +import { CreateTownDto } from './dto/create-town.dto'; +import { UpdateTownDto } from './dto/update-town.dto'; + +@Injectable() +export class TownService { + create(createTownDto: CreateTownDto) { + return 'This action adds a new town'; + } + + findAll() { + return `This action returns all town`; + } + + findOne(id: number) { + return `This action returns a #${id} town`; + } + + update(id: number, updateTownDto: UpdateTownDto) { + return `This action updates a #${id} town`; + } + + remove(id: number) { + return `This action removes a #${id} town`; + } +} -- GitLab From e819252a9857330f498e13e1c2ad9d0d851854cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 18 Apr 2024 10:20:52 -0600 Subject: [PATCH 02/21] agregando Town al app module --- backend/src/app.module.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 190781ce..fe98b9d8 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -14,6 +14,7 @@ import { DatabaseSeederModule } from './database-seeder/database-seeder.module'; import { State } from './state/entities/state.entity'; import { ServeStaticModule } from '@nestjs/serve-static'; import { join } from 'path'; +import { Town } from './town/entities/town.entity'; @Module({ imports: [ TypeOrmModule.forRoot({ @@ -23,7 +24,7 @@ import { join } from 'path'; username: DbConstants.DB_USER, password: DbConstants.DB_PASSWORD, database: DbConstants.DB_NAME, - entities: [Admin, User, State], + entities: [Admin, User, State, Town], synchronize: DbConstants.DB_SYNC, }), AuthAdminModule, -- GitLab From 44dc0887fe70ef3675ac4849f96e212a18cd2687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 18 Apr 2024 10:22:16 -0600 Subject: [PATCH 03/21] agregando relacion one to many de state a town --- backend/src/state/entities/state.entity.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/backend/src/state/entities/state.entity.ts b/backend/src/state/entities/state.entity.ts index afc58a07..22040e87 100644 --- a/backend/src/state/entities/state.entity.ts +++ b/backend/src/state/entities/state.entity.ts @@ -1,10 +1,15 @@ -import { Entity, Column, PrimaryColumn } from 'typeorm'; +import { Town } from 'src/town/entities/town.entity'; +import { Entity, Column, PrimaryColumn, OneToMany, JoinColumn } from 'typeorm'; @Entity() export class State { @PrimaryColumn() stateId: number; + @OneToMany(() => Town, (town) => town.townId) + @JoinColumn() + towns: Town[]; + @Column() name: string; -- GitLab From 810fbd3149e0f027ba0af64e4e3ac9fd8f6f58cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:29:16 -0600 Subject: [PATCH 04/21] cambiando el limite de caracteres por linea --- backend/.eslintrc.js | 6 ++---- backend/.prettierrc | 5 +++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/backend/.eslintrc.js b/backend/.eslintrc.js index 259de13c..b518b4a8 100644 --- a/backend/.eslintrc.js +++ b/backend/.eslintrc.js @@ -6,10 +6,7 @@ module.exports = { sourceType: 'module', }, plugins: ['@typescript-eslint/eslint-plugin'], - extends: [ - 'plugin:@typescript-eslint/recommended', - 'plugin:prettier/recommended', - ], + extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'], root: true, env: { node: true, @@ -21,5 +18,6 @@ module.exports = { '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/explicit-module-boundary-types': 'off', '@typescript-eslint/no-explicit-any': 'off', + 'max-len': ['error', { code: 120 }], }, }; diff --git a/backend/.prettierrc b/backend/.prettierrc index dcb72794..9db28a2b 100644 --- a/backend/.prettierrc +++ b/backend/.prettierrc @@ -1,4 +1,5 @@ { "singleQuote": true, - "trailingComma": "all" -} \ No newline at end of file + "trailingComma": "all", + "printWidth": 120 +} -- GitLab From c33c300e61c2a09e411e29ac913b40fbd11fdbb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:30:16 -0600 Subject: [PATCH 05/21] cambiando la entidad town --- backend/src/town/entities/town.entity.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/backend/src/town/entities/town.entity.ts b/backend/src/town/entities/town.entity.ts index 87d18182..a2d69e5d 100644 --- a/backend/src/town/entities/town.entity.ts +++ b/backend/src/town/entities/town.entity.ts @@ -1,17 +1,23 @@ import { State } from 'src/state/entities/state.entity'; -import { Entity, Column, PrimaryColumn, ManyToOne, JoinColumn } from 'typeorm'; +import { Entity, Column, ManyToOne, JoinColumn, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Town { - @PrimaryColumn() + @PrimaryGeneratedColumn() townId: number; @ManyToOne(() => State, (state) => state.stateId, { nullable: false }) @JoinColumn() + state: number; + + @Column() name: string; @Column() description: string; @Column() - imageURL: string; + imageName: string; + + @Column({ default: true }) + active: boolean = true; } -- GitLab From c11aa48bd8169a4e718fb35a9ea042ffe24bfd1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:31:50 -0600 Subject: [PATCH 06/21] agregando paquete multer --- backend/package-lock.json | 10 ++++++++++ backend/package.json | 1 + 2 files changed, 11 insertions(+) diff --git a/backend/package-lock.json b/backend/package-lock.json index 02e86f6a..83797220 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -31,6 +31,7 @@ "@types/bcrypt": "^5.0.2", "@types/express": "^4.17.17", "@types/jest": "^29.5.2", + "@types/multer": "^1.4.11", "@types/node": "^20.3.1", "@types/supertest": "^6.0.0", "@typescript-eslint/eslint-plugin": "^6.0.0", @@ -2472,6 +2473,15 @@ "integrity": "sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==", "dev": true }, + "node_modules/@types/multer": { + "version": "1.4.11", + "resolved": "https://registry.npmjs.org/@types/multer/-/multer-1.4.11.tgz", + "integrity": "sha512-svK240gr6LVWvv3YGyhLlA+6LRRWA4mnGIU7RcNmgjBYFl6665wcXrRfxGp5tEPVHUNm5FMcmq7too9bxCwX/w==", + "dev": true, + "dependencies": { + "@types/express": "*" + } + }, "node_modules/@types/node": { "version": "20.11.26", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.26.tgz", diff --git a/backend/package.json b/backend/package.json index 0198a4d4..8a205441 100644 --- a/backend/package.json +++ b/backend/package.json @@ -42,6 +42,7 @@ "@types/bcrypt": "^5.0.2", "@types/express": "^4.17.17", "@types/jest": "^29.5.2", + "@types/multer": "^1.4.11", "@types/node": "^20.3.1", "@types/supertest": "^6.0.0", "@typescript-eslint/eslint-plugin": "^6.0.0", -- GitLab From 8331bc14b27357693aa4cafcc83fd8d287c61177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:32:11 -0600 Subject: [PATCH 07/21] agregando createTownDTO --- backend/src/town/dto/create-town.dto.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/backend/src/town/dto/create-town.dto.ts b/backend/src/town/dto/create-town.dto.ts index 3be6400a..8f335fad 100644 --- a/backend/src/town/dto/create-town.dto.ts +++ b/backend/src/town/dto/create-town.dto.ts @@ -1 +1,6 @@ -export class CreateTownDto {} +export class CreateTownDto { + name: string; + description: string; + imageName: string = 'default.jpg'; + state: number; +} -- GitLab From 51b9269f458d5aae5533ef15a50534b66caba4a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:32:52 -0600 Subject: [PATCH 08/21] agregando dto para la respuesta de estados --- backend/src/state/dto/state-res.dto.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 backend/src/state/dto/state-res.dto.ts diff --git a/backend/src/state/dto/state-res.dto.ts b/backend/src/state/dto/state-res.dto.ts new file mode 100644 index 00000000..8494f6d3 --- /dev/null +++ b/backend/src/state/dto/state-res.dto.ts @@ -0,0 +1,10 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class StateResponseDto { + @ApiProperty() + stateId: number; + @ApiProperty() + name: string; + @ApiProperty() + imageURL: string; +} -- GitLab From 547dddd51e80a48f84394c176ab51289348597b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:33:21 -0600 Subject: [PATCH 09/21] agregando modulo town --- backend/src/app.module.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index fe98b9d8..203a432c 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -15,6 +15,7 @@ import { State } from './state/entities/state.entity'; import { ServeStaticModule } from '@nestjs/serve-static'; import { join } from 'path'; import { Town } from './town/entities/town.entity'; +import { TownModule } from './town/town.module'; @Module({ imports: [ TypeOrmModule.forRoot({ @@ -33,6 +34,7 @@ import { Town } from './town/entities/town.entity'; AuthUserModule, StateModule, DatabaseSeederModule, + TownModule, ServeStaticModule.forRoot({ rootPath: join(__dirname, '..', 'static'), }), -- GitLab From a792909e5de1d56a4c642e7a2b296fc4a79419bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:33:56 -0600 Subject: [PATCH 10/21] agregando pipe de archivos --- backend/src/shared/pipe/file-validation.pipe.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 backend/src/shared/pipe/file-validation.pipe.ts diff --git a/backend/src/shared/pipe/file-validation.pipe.ts b/backend/src/shared/pipe/file-validation.pipe.ts new file mode 100644 index 00000000..4b6fe9d4 --- /dev/null +++ b/backend/src/shared/pipe/file-validation.pipe.ts @@ -0,0 +1,12 @@ +import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common'; + +@Injectable() +export class FileValidationPipe implements PipeTransform { + transform(value: any) { + const size = value.size; + if (size > 1000000) { + throw new BadRequestException('File is too large'); + } + return value; + } +} -- GitLab From d50b8cdeabc276282a0789a54fbd335440d10f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:34:13 -0600 Subject: [PATCH 11/21] agregando interceptor de archivos --- .../interceptors/file-save.interceptor.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 backend/src/shared/interceptors/file-save.interceptor.ts diff --git a/backend/src/shared/interceptors/file-save.interceptor.ts b/backend/src/shared/interceptors/file-save.interceptor.ts new file mode 100644 index 00000000..7406828e --- /dev/null +++ b/backend/src/shared/interceptors/file-save.interceptor.ts @@ -0,0 +1,23 @@ +// file.interceptor.ts +import { BadRequestException } from '@nestjs/common'; +import { FileInterceptor } from '@nestjs/platform-express'; +import { diskStorage } from 'multer'; +import { extname } from 'path'; +import { v4 as uuidv4 } from 'uuid'; + +export const fileInterceptor = (fieldName: string, dest: string, validExt: string[]) => + FileInterceptor(fieldName, { + storage: diskStorage({ + destination: (req, file, callback) => { + callback(null, dest); + }, + filename: (req, file, callback) => { + const extension = extname(file.originalname); + if (!validExt.includes(extension)) { + return callback(new BadRequestException('Only image files are allowed!'), ''); + } + const uniqueFilename = `${uuidv4()}${extension}`; + callback(null, uniqueFilename); + }, + }), + }); -- GitLab From b2c30c4ae2c8cd687ff91f67fd4957d5df069efc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:35:10 -0600 Subject: [PATCH 12/21] agregando metodo findOne --- backend/src/state/state.service.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/backend/src/state/state.service.ts b/backend/src/state/state.service.ts index 44234781..1c1d05c9 100644 --- a/backend/src/state/state.service.ts +++ b/backend/src/state/state.service.ts @@ -4,20 +4,19 @@ import { InjectRepository } from '@nestjs/typeorm'; import { State } from './entities/state.entity'; import { Repository } from 'typeorm'; import { ServerConstants } from 'src/constants/server.contants'; +import { StateResponseDto } from './dto/state-res.dto'; @Injectable() export class StateService { - constructor( - @InjectRepository(State) private stateRepository: Repository, - ) {} + constructor(@InjectRepository(State) private stateRepository: Repository) {} async create(createStateDto: CreateStateDto) { await this.stateRepository.save(createStateDto); } - async findAll() { + async findAll(): Promise> { const states = await this.stateRepository.find(); - const statesResponse = states.map(({ imageName, name, stateId }) => { + const statesResponse: Array = states.map(({ imageName, name, stateId }) => { return { stateId, name, @@ -27,4 +26,14 @@ export class StateService { return statesResponse; } + + async findOne(id: number) { + const state = await this.stateRepository.findOneBy({ stateId: id }); + if (!state) return null; + return { + stateId: state.stateId, + name: state.name, + imageURL: `${ServerConstants.HOST}/states/${state.imageName}`, + }; + } } -- GitLab From a3be0e9c1afce6739f72f9012fcc1024c88cc8d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:35:38 -0600 Subject: [PATCH 13/21] agregando metodo para crear town --- backend/src/town/town.service.ts | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/backend/src/town/town.service.ts b/backend/src/town/town.service.ts index 96e0bd87..027ad857 100644 --- a/backend/src/town/town.service.ts +++ b/backend/src/town/town.service.ts @@ -1,25 +1,27 @@ -import { Injectable } from '@nestjs/common'; +import { BadRequestException, Injectable } from '@nestjs/common'; import { CreateTownDto } from './dto/create-town.dto'; -import { UpdateTownDto } from './dto/update-town.dto'; +import { Repository } from 'typeorm'; +import { Town } from './entities/town.entity'; +import { InjectRepository } from '@nestjs/typeorm'; +import { StateService } from 'src/state/state.service'; @Injectable() export class TownService { - create(createTownDto: CreateTownDto) { - return 'This action adds a new town'; + constructor( + @InjectRepository(Town) private townRepository: Repository, + private state: StateService, + ) {} + + async create(createTownDto: CreateTownDto) { + const state = await this.state.findOne(createTownDto.state); + if (!state) throw new BadRequestException('State does not exist'); + await this.townRepository.save(createTownDto); } findAll() { return `This action returns all town`; } - findOne(id: number) { - return `This action returns a #${id} town`; - } - - update(id: number, updateTownDto: UpdateTownDto) { - return `This action updates a #${id} town`; - } - remove(id: number) { return `This action removes a #${id} town`; } -- GitLab From 957a5247b313b7cf55d952549c499364376bc77f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:35:59 -0600 Subject: [PATCH 14/21] agregando imports y providers --- backend/src/town/town.module.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/backend/src/town/town.module.ts b/backend/src/town/town.module.ts index 114d14c1..13393bc2 100644 --- a/backend/src/town/town.module.ts +++ b/backend/src/town/town.module.ts @@ -1,9 +1,14 @@ import { Module } from '@nestjs/common'; import { TownService } from './town.service'; import { TownController } from './town.controller'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { Town } from './entities/town.entity'; +import { StateService } from 'src/state/state.service'; +import { State } from 'src/state/entities/state.entity'; @Module({ controllers: [TownController], - providers: [TownService], + providers: [TownService, StateService], + imports: [TypeOrmModule.forFeature([Town, State])], }) export class TownModule {} -- GitLab From faa5f679eb0f53bcde8e52e57d47566548ece019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:36:25 -0600 Subject: [PATCH 15/21] agregando como export stateService --- backend/src/state/state.module.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/src/state/state.module.ts b/backend/src/state/state.module.ts index 523b6c39..ba6dc606 100644 --- a/backend/src/state/state.module.ts +++ b/backend/src/state/state.module.ts @@ -8,5 +8,6 @@ import { StateController } from './state.controller'; controllers: [StateController], providers: [StateService], imports: [TypeOrmModule.forFeature([State])], + exports: [StateService], }) export class StateModule {} -- GitLab From 6a3da0d1069c03339e9e049d80808874f446b512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:36:50 -0600 Subject: [PATCH 16/21] agregando endpoint para agregar un town --- backend/src/town/town.controller.ts | 37 +++++++++++------------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/backend/src/town/town.controller.ts b/backend/src/town/town.controller.ts index 66d202ab..aa4884a3 100644 --- a/backend/src/town/town.controller.ts +++ b/backend/src/town/town.controller.ts @@ -1,23 +1,24 @@ -import { - Controller, - Get, - Post, - Body, - Patch, - Param, - Delete, -} from '@nestjs/common'; +import { Controller, Get, Post, Param, Delete, UseInterceptors, UploadedFile, Body } from '@nestjs/common'; import { TownService } from './town.service'; +import { 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'; -import { UpdateTownDto } from './dto/update-town.dto'; - @Controller('town') +@ApiTags('Agregar un pueblo') export class TownController { constructor(private readonly townService: TownService) {} @Post() - create(@Body() createTownDto: CreateTownDto) { - return this.townService.create(createTownDto); + @UseInterceptors(fileInterceptor('image', 'static/towns/', ['.jpg', '.jpeg', '.png'])) + async create(@UploadedFile(new FileValidationPipe()) file, @Body() createTownDto: CreateTownDto) { + try { + createTownDto.imageName = file.filename; + await this.townService.create(createTownDto); + return { message: 'Town created successfully' }; + } catch (error) { + throw error; + } } @Get() @@ -25,16 +26,6 @@ export class TownController { return this.townService.findAll(); } - @Get(':id') - findOne(@Param('id') id: string) { - return this.townService.findOne(+id); - } - - @Patch(':id') - update(@Param('id') id: string, @Body() updateTownDto: UpdateTownDto) { - return this.townService.update(+id, updateTownDto); - } - @Delete(':id') remove(@Param('id') id: string) { return this.townService.remove(+id); -- GitLab From f578b68361a314c6a98823a82cf5d0b3f0653ad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:37:38 -0600 Subject: [PATCH 17/21] agregando docs para endpoin de state --- backend/src/state/state.controller.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/backend/src/state/state.controller.ts b/backend/src/state/state.controller.ts index 71114a09..5a586f0b 100644 --- a/backend/src/state/state.controller.ts +++ b/backend/src/state/state.controller.ts @@ -1,13 +1,19 @@ import { Controller, Get } from '@nestjs/common'; import { StateService } from './state.service'; +import { ApiCreatedResponse, ApiTags } from '@nestjs/swagger'; +import { StateResponseDto } from './dto/state-res.dto'; @Controller('state') -@Controller('town') +@ApiTags('State') export class StateController { constructor(private readonly stateService: StateService) {} - + @ApiCreatedResponse({ + isArray: true, + type: StateResponseDto, + }) @Get() - findAll() { - return this.stateService.findAll(); + async findAll() { + const states: StateResponseDto[] = await this.stateService.findAll(); + return states; } } -- GitLab From 23d83137f91ba7582e3c833db6c399b9d48beb79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:43:53 -0600 Subject: [PATCH 18/21] agregando estaticos .gitignore --- backend/.gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backend/.gitignore b/backend/.gitignore index 4b56acfb..f793d336 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,3 +1,5 @@ +#static files +/static # compiled output /dist /node_modules -- GitLab From b0dde7582fb76927c1f2e29106bf2b6e863a60ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:45:09 -0600 Subject: [PATCH 19/21] formateando archivos con nuevas reglas de prettier --- backend/src/admin/admin.service.ts | 4 +--- backend/src/auth/admin/authAdmincontroller.ts | 10 ++-------- backend/src/auth/admin/authAdminservice.ts | 17 ++++------------- .../src/auth/encryption/encryption.service.ts | 5 +---- backend/src/auth/user/authUsercontroller.ts | 11 ++--------- backend/src/auth/user/authUserservice.ts | 18 ++++-------------- backend/src/constants/db.constants.ts | 4 +--- backend/src/constants/server.contants.ts | 4 +--- backend/src/user/user.service.ts | 4 +--- 9 files changed, 17 insertions(+), 60 deletions(-) diff --git a/backend/src/admin/admin.service.ts b/backend/src/admin/admin.service.ts index ca642dc9..e614d781 100644 --- a/backend/src/admin/admin.service.ts +++ b/backend/src/admin/admin.service.ts @@ -6,9 +6,7 @@ import { InjectRepository } from '@nestjs/typeorm'; @Injectable() export class AdminService { - constructor( - @InjectRepository(Admin) private adminRepository: Repository, - ) {} + constructor(@InjectRepository(Admin) private adminRepository: Repository) {} async create(createAdminDto: CreateAdminDto) { await this.adminRepository.insert(createAdminDto); } diff --git a/backend/src/auth/admin/authAdmincontroller.ts b/backend/src/auth/admin/authAdmincontroller.ts index 72f0c0a3..bc2e0906 100644 --- a/backend/src/auth/admin/authAdmincontroller.ts +++ b/backend/src/auth/admin/authAdmincontroller.ts @@ -2,12 +2,7 @@ import { Body, Controller, Post } from '@nestjs/common'; import { AuthAdminService } from './authAdminservice'; import { CreateAdminDto } from 'src/admin/dto/create-admin.dto'; import { LoginAdminDto } from 'src/auth/admin/dto/login-admin.dto'; -import { - ApiBody, - ApiCreatedResponse, - ApiTags, - ApiUnauthorizedResponse, -} from '@nestjs/swagger'; +import { ApiBody, ApiCreatedResponse, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger'; import { AdminSigninResDto } from './dto/admin-signin-res.dto'; @Controller() @@ -43,8 +38,7 @@ export class AuthAdminController { @Post('admin/signin') async signIn(@Body() loginAdminDto: LoginAdminDto) { try { - const adminSigninResDto = - await this.authAdminService.signIn(loginAdminDto); + const adminSigninResDto = await this.authAdminService.signIn(loginAdminDto); return adminSigninResDto; } catch (e) { throw e; diff --git a/backend/src/auth/admin/authAdminservice.ts b/backend/src/auth/admin/authAdminservice.ts index cd8dbfb7..3d780937 100644 --- a/backend/src/auth/admin/authAdminservice.ts +++ b/backend/src/auth/admin/authAdminservice.ts @@ -22,30 +22,21 @@ export class AuthAdminService { email: createAdminDto.email, password: createAdminDto.password, }; - const hashedPwd = await this.encryptionService.hashPassword( - createAdminDto.password, - ); + const hashedPwd = await this.encryptionService.hashPassword(createAdminDto.password); createAdminDto.password = hashedPwd; await this.adminService.create(createAdminDto); - const adminSigninResDto: AdminSigninResDto = - await this.signIn(loginAdminDto); + const adminSigninResDto: AdminSigninResDto = await this.signIn(loginAdminDto); return adminSigninResDto.token; } async signIn(logInAdmin: LoginAdminDto): Promise { const admin: Admin = await this.adminService.findOne(logInAdmin.email); - const validPwd: boolean = await this.encryptionService.comparePassword( - logInAdmin.password, - admin.password, - ); + const validPwd: boolean = await this.encryptionService.comparePassword(logInAdmin.password, admin.password); if (!validPwd) { throw new HttpException('Invalid credentials', HttpStatus.UNAUTHORIZED); } - const accessToken = await this.jwtService.sign( - { email: admin.email }, - { secret: JwtConstants.SECRET }, - ); + const accessToken = await this.jwtService.sign({ email: admin.email }, { secret: JwtConstants.SECRET }); const adminSigninResDto: AdminSigninResDto = { email: admin.email, name: admin.name, diff --git a/backend/src/auth/encryption/encryption.service.ts b/backend/src/auth/encryption/encryption.service.ts index a00ac5ac..827d3347 100644 --- a/backend/src/auth/encryption/encryption.service.ts +++ b/backend/src/auth/encryption/encryption.service.ts @@ -8,10 +8,7 @@ export class EncryptionService { return hashedPwd; } - async comparePassword( - password: string, - hashedPassword: string, - ): Promise { + async comparePassword(password: string, hashedPassword: string): Promise { return await bcrypt.compare(password, hashedPassword); } } diff --git a/backend/src/auth/user/authUsercontroller.ts b/backend/src/auth/user/authUsercontroller.ts index bc072bbf..f82a1147 100644 --- a/backend/src/auth/user/authUsercontroller.ts +++ b/backend/src/auth/user/authUsercontroller.ts @@ -1,11 +1,5 @@ import { Body, Controller, Post } from '@nestjs/common'; -import { - ApiBearerAuth, - ApiBody, - ApiCreatedResponse, - ApiTags, - ApiUnauthorizedResponse, -} from '@nestjs/swagger'; +import { ApiBearerAuth, ApiBody, ApiCreatedResponse, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger'; import { AuthUserService } from './authUserservice'; import { CreateUserDto } from 'src/user/dto/create-user.dto'; import { LoginUserDto } from './dto/login-user.dto'; @@ -24,8 +18,7 @@ export class AuthUserController { @Post('user/signup') async signUp(@Body() createAdminDto: CreateUserDto) { try { - const adminSigninResDto = - await this.authUserService.signUp(createAdminDto); + const adminSigninResDto = await this.authUserService.signUp(createAdminDto); return { user: adminSigninResDto }; } catch (e) { throw e; diff --git a/backend/src/auth/user/authUserservice.ts b/backend/src/auth/user/authUserservice.ts index 7a2eef25..2e5c49a0 100644 --- a/backend/src/auth/user/authUserservice.ts +++ b/backend/src/auth/user/authUserservice.ts @@ -1,8 +1,4 @@ -import { - BadRequestException, - Injectable, - UnauthorizedException, -} from '@nestjs/common'; +import { BadRequestException, Injectable, UnauthorizedException } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { EncryptionService } from '../encryption/encryption.service'; import { JwtConstants } from 'src/constants/jwt.constants'; @@ -25,27 +21,21 @@ export class AuthUserService { email: createAdminDto.email, password: createAdminDto.password, }; - const hashedPwd = await this.encryptionService.hashPassword( - createAdminDto.password, - ); + const hashedPwd = await this.encryptionService.hashPassword(createAdminDto.password); createAdminDto.password = hashedPwd; if (await this.userService.userExists(createAdminDto.email)) { throw new BadRequestException('User already exists'); } await this.userService.create(createAdminDto); - const adminSigninResDto: UserSigninResDto = - await this.signIn(loginAdminDto); + const adminSigninResDto: UserSigninResDto = await this.signIn(loginAdminDto); return adminSigninResDto; } async signIn(logInAdmin: LoginUserDto): Promise { const user: User = await this.userService.findOne(logInAdmin.email); if (!user) throw new UnauthorizedException('Invalid credentials'); - const validPwd: boolean = await this.encryptionService.comparePassword( - logInAdmin.password, - user.password, - ); + const validPwd: boolean = await this.encryptionService.comparePassword(logInAdmin.password, user.password); if (!validPwd) { throw new UnauthorizedException('Invalid credentials'); } diff --git a/backend/src/constants/db.constants.ts b/backend/src/constants/db.constants.ts index 13f7af26..cb710461 100644 --- a/backend/src/constants/db.constants.ts +++ b/backend/src/constants/db.constants.ts @@ -2,9 +2,7 @@ import * as dotenv from 'dotenv'; dotenv.config(); export class DbConstants { static DB_HOST: string = process.env.DB_HOST || 'localhost'; - static DB_PORT: number = process.env.DB_PORT - ? parseInt(process.env.DB_PORT) - : 3306; + static DB_PORT: number = process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 3306; static DB_USER: string = process.env.DB_USER || 'root'; static DB_PASSWORD: string = process.env.DB_PASSWORD || 'root'; static DB_NAME: string = process.env.DB_NAME || 'pueblos'; diff --git a/backend/src/constants/server.contants.ts b/backend/src/constants/server.contants.ts index 67d19ee0..ec37b52a 100644 --- a/backend/src/constants/server.contants.ts +++ b/backend/src/constants/server.contants.ts @@ -2,8 +2,6 @@ import * as dotenv from 'dotenv'; dotenv.config(); export class ServerConstants { - static PORT: number = process.env.SERVER_PORT - ? parseInt(process.env.SERVER_PORT) - : 3003; + static PORT: number = process.env.SERVER_PORT ? parseInt(process.env.SERVER_PORT) : 3003; static HOST: string = `${process.env.SERVER_HOST || 'http://localhost'}:${this.PORT}`; } diff --git a/backend/src/user/user.service.ts b/backend/src/user/user.service.ts index c538fcd5..561a0be7 100644 --- a/backend/src/user/user.service.ts +++ b/backend/src/user/user.service.ts @@ -9,9 +9,7 @@ import { CreateUserDto } from './dto/create-user.dto'; * Service responsible for handling user-related operations. */ export class UserService { - constructor( - @InjectRepository(User) private userRepository: Repository, - ) {} + constructor(@InjectRepository(User) private userRepository: Repository) {} async findOne(email: string) { return await this.userRepository.findOneBy({ email }); -- GitLab From 5b45ac0adb638a21f3692dbc08f10cf68160a2da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:57:21 -0600 Subject: [PATCH 20/21] agregando docs de town --- backend/src/town/town.controller.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/src/town/town.controller.ts b/backend/src/town/town.controller.ts index aa4884a3..19e1459f 100644 --- a/backend/src/town/town.controller.ts +++ b/backend/src/town/town.controller.ts @@ -1,6 +1,6 @@ import { Controller, Get, Post, Param, Delete, UseInterceptors, UploadedFile, Body } from '@nestjs/common'; import { TownService } from './town.service'; -import { ApiTags } from '@nestjs/swagger'; +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'; @@ -9,6 +9,8 @@ import { CreateTownDto } from './dto/create-town.dto'; export class TownController { constructor(private readonly townService: TownService) {} + @ApiBody({ type: CreateTownDto }) + @ApiConsumes('multipart/form-data') @Post() @UseInterceptors(fileInterceptor('image', 'static/towns/', ['.jpg', '.jpeg', '.png'])) async create(@UploadedFile(new FileValidationPipe()) file, @Body() createTownDto: CreateTownDto) { -- GitLab From 678e2d4749dd1ac004165a2d02fbe2ab18d0b445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:57:58 -0600 Subject: [PATCH 21/21] agregando api property a los atributos --- backend/src/town/dto/create-town.dto.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/backend/src/town/dto/create-town.dto.ts b/backend/src/town/dto/create-town.dto.ts index 8f335fad..59e19ab9 100644 --- a/backend/src/town/dto/create-town.dto.ts +++ b/backend/src/town/dto/create-town.dto.ts @@ -1,6 +1,11 @@ +import { ApiProperty } from '@nestjs/swagger'; + export class CreateTownDto { + @ApiProperty() name: string; + @ApiProperty() description: string; imageName: string = 'default.jpg'; + @ApiProperty() state: number; } -- GitLab