diff --git a/src/almacenamiento/almacenamiento.controller.ts b/src/almacenamiento/almacenamiento.controller.ts new file mode 100644 index 0000000..528eb0a --- /dev/null +++ b/src/almacenamiento/almacenamiento.controller.ts @@ -0,0 +1,23 @@ +import { Controller, Post, Get, Body } from '@nestjs/common'; +import { AlmacenamientoService } from './almacenamiento.service'; + +@Controller('almacenamiento') +export class AlmacenamientoController { + constructor(private readonly almacenamientoService: AlmacenamientoService) {} + + @Post() + async create(@Body() body: { email: string; agree: boolean }) { + // Save only if agree is true + if (!body.agree) { + return { message: 'User did not agree', data: body }; + } + const saved = await this.almacenamientoService.create(body.email); + return { message: 'Data saved', data: saved }; + } + + @Get() + async findAll() { + const data = await this.almacenamientoService.findAll(); + return { message: 'GET endpoint reached', data }; + } +} diff --git a/src/almacenamiento/almacenamiento.module.ts b/src/almacenamiento/almacenamiento.module.ts new file mode 100644 index 0000000..af83d13 --- /dev/null +++ b/src/almacenamiento/almacenamiento.module.ts @@ -0,0 +1,31 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { Almacenamiento } from '../entities/almacenamiento.entity'; +import { AlmacenamientoService } from './almacenamiento.service'; +import { AlmacenamientoController } from './almacenamiento.controller'; +import { ConfigModule, ConfigService } from '@nestjs/config'; + +@Module({ + imports: [ + TypeOrmModule.forRootAsync({ + name: 'almacenamiento', + imports: [ConfigModule], + inject: [ConfigService], + useFactory: async (configService: ConfigService) => ({ + name: 'almacenamiento', + type: 'mariadb', + host: configService.get('HOST_TYL'), + port: configService.get('PORT_TYL'), + username: configService.get('USUARIO_TYL'), + password: configService.get('CONTRASENA_TYL'), + database: configService.get('DATABASE_TYL'), + entities: [Almacenamiento], + synchronize: false, + }), + }), + TypeOrmModule.forFeature([Almacenamiento], 'almacenamiento'), + ], + providers: [AlmacenamientoService], + controllers: [AlmacenamientoController], +}) +export class AlmacenamientoModule {} diff --git a/src/almacenamiento/almacenamiento.service.ts b/src/almacenamiento/almacenamiento.service.ts new file mode 100644 index 0000000..a5b6718 --- /dev/null +++ b/src/almacenamiento/almacenamiento.service.ts @@ -0,0 +1,23 @@ +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { Almacenamiento } from '../entities/almacenamiento.entity'; + +@Injectable() +export class AlmacenamientoService { + constructor( + @InjectRepository(Almacenamiento, 'almacenamiento') + private readonly almacenamientoRepository: Repository, + ) {} + + // Example method to create a new record + async create(correo: string): Promise { + const nuevo = this.almacenamientoRepository.create({ correo }); + return this.almacenamientoRepository.save(nuevo); + } + + // Example method to get all records + async findAll(): Promise { + return this.almacenamientoRepository.find(); + } +} diff --git a/src/app.module.ts b/src/app.module.ts index 07d553e..cb4978b 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -52,6 +52,8 @@ import { CarruselModule } from './carrusel/carrusel.module'; import { Carrusel } from './entities/carrusel.entity'; import { ComentariosModule } from './comentarios/comentarios.module'; import { Comentarios } from './comentarios/entity/comentarios.entity'; +import { AlmacenamientoModule } from './almacenamiento/almacenamiento.module'; +import { Almacenamiento } from './entities/almacenamiento.entity'; @Module({ imports: [ @@ -100,7 +102,6 @@ import { Comentarios } from './comentarios/entity/comentarios.entity'; /* No recuerdo porque empece a ponerle fechas */ Comentarios, ], - synchronize: true, }), }), EventosModule, @@ -127,6 +128,7 @@ import { Comentarios } from './comentarios/entity/comentarios.entity'; }), CarruselModule, ComentariosModule, + AlmacenamientoModule, ], controllers: [ AppController, diff --git a/src/entities/almacenamiento.entity.ts b/src/entities/almacenamiento.entity.ts new file mode 100644 index 0000000..c25f994 --- /dev/null +++ b/src/entities/almacenamiento.entity.ts @@ -0,0 +1,16 @@ +import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; + +@Entity('almacenamiento') +export class Almacenamiento { + @PrimaryGeneratedColumn({ type: 'int', name: 'id_almacenamiento' }) + idAlmacenamiento: number; + + @Column('varchar', { name: 'correo', length: 100 }) + correo: string; + + @Column('timestamp', { + name: 'fecha_ampliacion', + default: () => 'CURRENT_TIMESTAMP', + }) + fechaAmpliacion: Date; +}