diff --git a/backend/.eslintrc.js b/backend/.eslintrc.js index 259de13c733a7284a352a5cba1e9fd57e97e431d..b518b4a86d65d5653875ba21ad36f288018ee869 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/.gitignore b/backend/.gitignore index 4b56acfbebf41e17d1ff0dd945bb4c545eae1db1..f793d3364da02cf3083991d0c44eac4ad2130630 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,3 +1,5 @@ +#static files +/static # compiled output /dist /node_modules diff --git a/backend/.prettierrc b/backend/.prettierrc index dcb72794f5300a3e0ccd2ad0669d802b62f3d370..9db28a2bd97d756a3f30dfa5d61f3aa5cb7a1c91 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 +} diff --git a/backend/package-lock.json b/backend/package-lock.json index 02e86f6a23038d6a09c4d8a3b01b6ec3e21e19ea..8379722035e9fba7999977c18739e49dbabb0795 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 0198a4d469d0cb85399f23c9019d923f553e8ad3..8a2054410214a180a97e47e3d84e332fad0b74d8 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", diff --git a/backend/src/admin/admin.service.ts b/backend/src/admin/admin.service.ts index ca642dc95b9c474c260f02e44be70bc5e7d8506e..e614d7819e02e07fe0798e3c9f66294888507a82 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/app.module.ts b/backend/src/app.module.ts index 190781ced06c78eb853b133ec5e19a6782eee153..203a432ca68251b7fd796cb07392b869c5b423b6 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -14,6 +14,8 @@ 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'; +import { TownModule } from './town/town.module'; @Module({ imports: [ TypeOrmModule.forRoot({ @@ -23,7 +25,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, @@ -32,6 +34,7 @@ import { join } from 'path'; AuthUserModule, StateModule, DatabaseSeederModule, + TownModule, ServeStaticModule.forRoot({ rootPath: join(__dirname, '..', 'static'), }), diff --git a/backend/src/auth/admin/authAdmincontroller.ts b/backend/src/auth/admin/authAdmincontroller.ts index 72f0c0a33e47e895a948ba215a5694c26fb35a9e..bc2e0906e8850c0e4bbebfaeb16549692d6610e0 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 cd8dbfb750f7ee7ebd9c6ee9486e8e293a0557cf..3d780937e4cc0f05d322873e2cb3e538a18fe6e9 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 a00ac5ac71a6646539b7ccbb214654837390fd0e..827d334708d1bca50975a4deba956f2a8d4a5cce 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 bc072bbf725ffbf758d5a86dcf6880951b8f3dd5..f82a1147551ef652141d4fe480996bff8ed3ec2c 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 7a2eef2567a8f97fe362595464cd1c556527ec42..2e5c49a0b9fc304a22a041618d22edac533bb64b 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 13f7af267765cb6f336e07175ab7a426ec07a55b..cb710461b092c4456e5ccc3470a8e9f07930956f 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 67d19ee0788af98f207582ae99765a1735bd36af..ec37b52a7d38cc9871cfd33aea88c208ba105072 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/shared/interceptors/file-save.interceptor.ts b/backend/src/shared/interceptors/file-save.interceptor.ts new file mode 100644 index 0000000000000000000000000000000000000000..7406828e5c7562862f41869ecfe06f6860564f27 --- /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); + }, + }), + }); 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 0000000000000000000000000000000000000000..4b6fe9d42a5562695be76152dac84f832914007b --- /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; + } +} 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 0000000000000000000000000000000000000000..8494f6d3134ac0810a3e6b036100d315760a97d3 --- /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; +} diff --git a/backend/src/state/entities/state.entity.ts b/backend/src/state/entities/state.entity.ts index afc58a072e66a9d90a8722c477113b054feb062c..22040e87bf79632f9ffee02af2832c10b3ad8bcf 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; diff --git a/backend/src/state/state.controller.ts b/backend/src/state/state.controller.ts index 71114a0903da38721b28093ac629351dbaa83d95..5a586f0ba2af2310bdf0b408bb9f1557c20119bb 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; } } diff --git a/backend/src/state/state.module.ts b/backend/src/state/state.module.ts index 523b6c39c99a6f7ee5ff643fd453bff60dd886af..ba6dc6065e9e9c04f44825c7ba5ae3c3796b1b28 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 {} diff --git a/backend/src/state/state.service.ts b/backend/src/state/state.service.ts index 44234781ece74e40e9f563ad13324b1b8e37e1c2..1c1d05c931919feb57c012767f83854fb6b84abb 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}`, + }; + } } 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 0000000000000000000000000000000000000000..59e19ab9e3c434f9bbd601826c377338b31d782a --- /dev/null +++ b/backend/src/town/dto/create-town.dto.ts @@ -0,0 +1,11 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class CreateTownDto { + @ApiProperty() + name: string; + @ApiProperty() + description: string; + imageName: string = 'default.jpg'; + @ApiProperty() + state: number; +} 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 0000000000000000000000000000000000000000..6982b689f2ff37d9ebfa57f10c3f063039cfdc02 --- /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 0000000000000000000000000000000000000000..a2d69e5dc081fbe0af5060e25fcc60c2e9e5659e --- /dev/null +++ b/backend/src/town/entities/town.entity.ts @@ -0,0 +1,23 @@ +import { State } from 'src/state/entities/state.entity'; +import { Entity, Column, ManyToOne, JoinColumn, PrimaryGeneratedColumn } from 'typeorm'; +@Entity() +export class Town { + @PrimaryGeneratedColumn() + townId: number; + + @ManyToOne(() => State, (state) => state.stateId, { nullable: false }) + @JoinColumn() + state: number; + + @Column() + name: string; + + @Column() + description: string; + + @Column() + imageName: string; + + @Column({ default: true }) + active: boolean = true; +} diff --git a/backend/src/town/town.controller.ts b/backend/src/town/town.controller.ts new file mode 100644 index 0000000000000000000000000000000000000000..19e1459fb33cbb373fb4cb392d897607ceb5ee5d --- /dev/null +++ b/backend/src/town/town.controller.ts @@ -0,0 +1,35 @@ +import { Controller, Get, Post, Param, Delete, UseInterceptors, UploadedFile, Body } 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') +@ApiTags('Agregar un pueblo') +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) { + try { + createTownDto.imageName = file.filename; + await this.townService.create(createTownDto); + return { message: 'Town created successfully' }; + } catch (error) { + throw error; + } + } + + @Get() + findAll() { + return this.townService.findAll(); + } + + @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 0000000000000000000000000000000000000000..13393bc233fa370202cee0b8e97287fc41b0b560 --- /dev/null +++ b/backend/src/town/town.module.ts @@ -0,0 +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, StateService], + imports: [TypeOrmModule.forFeature([Town, State])], +}) +export class TownModule {} diff --git a/backend/src/town/town.service.ts b/backend/src/town/town.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..027ad857b647c895d62cd66c8a00cdbe179c6e58 --- /dev/null +++ b/backend/src/town/town.service.ts @@ -0,0 +1,28 @@ +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 { StateService } from 'src/state/state.service'; + +@Injectable() +export class TownService { + 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`; + } + + remove(id: number) { + return `This action removes a #${id} town`; + } +} diff --git a/backend/src/user/user.service.ts b/backend/src/user/user.service.ts index c538fcd5a9b08737cf089a7fce6477951a7848b5..561a0be72a8654c5f35b5ff43361995a50d6e69a 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 });