Add Almacenamiento module, controller, service, and entity for data management
This commit is contained in:
@@ -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 };
|
||||
}
|
||||
}
|
||||
@@ -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 {}
|
||||
@@ -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<Almacenamiento>,
|
||||
) {}
|
||||
|
||||
// Example method to create a new record
|
||||
async create(correo: string): Promise<Almacenamiento> {
|
||||
const nuevo = this.almacenamientoRepository.create({ correo });
|
||||
return this.almacenamientoRepository.save(nuevo);
|
||||
}
|
||||
|
||||
// Example method to get all records
|
||||
async findAll(): Promise<Almacenamiento[]> {
|
||||
return this.almacenamientoRepository.find();
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user