diff --git a/src/evento-participante/evento-participante/evento-participante.controller.ts b/src/evento-participante/evento-participante/evento-participante.controller.ts index d54f2f3..751186f 100644 --- a/src/evento-participante/evento-participante/evento-participante.controller.ts +++ b/src/evento-participante/evento-participante/evento-participante.controller.ts @@ -1,14 +1,15 @@ -import { Body, Controller, Post } from '@nestjs/common'; +import { Body, Controller, Param, ParseIntPipe, Post } from '@nestjs/common'; import { RegistrarParticipanteDto } from './dto/registrarParticipanteDto.dto'; import { EventoParticipanteService } from './evento-participante.service'; +import { EventoParticipante } from './eventoParticipante.entity'; @Controller('evento-participante') export class EventoParticipanteController { constructor(private readonly eventoParticipanteService: EventoParticipanteService){} @Post() - regitrarParticipante(@Body() datos: RegistrarParticipanteDto){ - return this.eventoParticipanteService.registrarParticipante(datos) + regitrarParticipante(@Param()id_evento:number, @Body() datos: RegistrarParticipanteDto){ + return this.eventoParticipanteService.registrarParticipante(id_evento, datos) } diff --git a/src/evento-participante/evento-participante/evento-participante.service.ts b/src/evento-participante/evento-participante/evento-participante.service.ts index db861a0..b836b82 100644 --- a/src/evento-participante/evento-participante/evento-participante.service.ts +++ b/src/evento-participante/evento-participante/evento-participante.service.ts @@ -1,4 +1,4 @@ -import { ConflictException, Injectable } from '@nestjs/common'; +import { Body, ConflictException, Injectable, Param } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; @@ -7,26 +7,51 @@ import { RegistrarParticipanteDto } from './dto/registrarParticipanteDto.dto'; import { Evento } from 'src/eventos/evento.entity'; import { EventoParticipante } from './eventoParticipante.entity'; import { Participante } from 'src/participante/participante.entity'; +import { info } from 'console'; @Injectable() export class EventoParticipanteService { constructor( @InjectRepository(Participante) private participanteRepository: Repository, + @InjectRepository(EventoParticipante) + private eventoParticipanteRepository: Repository, ) {} - participanteExiste(correo: string): Promise { + participanteExiste(correo: string) { return this.participanteRepository.findOne({ where: { correo } }); } - registrarParticipante(registrarParticipanteDto: RegistrarParticipanteDto) { + registrarEnEvento( + id_evento: number, + registrarParticipanteDto: RegistrarParticipanteDto, + ) { + this.participanteExiste(registrarParticipanteDto.correo).then( + (participante) => { + let info : EventoParticipante; + + info.id_evento = id_evento + info.id_participante = participante.id_participante + + this.eventoParticipanteRepository.save( + this.eventoParticipanteRepository.create(info), + ); + }, + ); + } + + registrarParticipante( + id_evento: number, + registrarParticipanteDto: RegistrarParticipanteDto, + ) { return this.participanteExiste(registrarParticipanteDto.correo).then( (participante) => { if (participante) { + this.registrarEnEvento(id_evento, registrarParticipanteDto); throw new ConflictException('Este Usuario ya existe'); } return this.participanteRepository.save( - this.participanteRepository.create(registrarParticipanteDto) + this.participanteRepository.create(registrarParticipanteDto), ); }, ); diff --git a/src/evento-participante/evento-participante/eventoParticipante.entity.ts b/src/evento-participante/evento-participante/eventoParticipante.entity.ts index 55a0059..ce842af 100644 --- a/src/evento-participante/evento-participante/eventoParticipante.entity.ts +++ b/src/evento-participante/evento-participante/eventoParticipante.entity.ts @@ -1,5 +1,5 @@ import { Evento } from 'src/eventos/evento.entity'; -import { Entity, JoinColumn, ManyToOne, PrimaryColumn, PrimaryGeneratedColumn } from 'typeorm'; +import { Column, Entity, JoinColumn, ManyToOne, PrimaryColumn, PrimaryGeneratedColumn } from 'typeorm'; import { Participante } from '../../participante/participante.entity'; @Entity() @@ -7,6 +7,12 @@ export class EventoParticipante { @PrimaryGeneratedColumn() id_eventoParticipante: number + @Column({type: Number, nullable:false}) + id_evento: number + + @Column({type:Number, nullable:false}) + id_participante: number + @ManyToOne(() => Evento, (evento) => evento.eventoParticipantes) @JoinColumn({ name: 'id_evento' }) evento: Evento; diff --git a/src/main.ts b/src/main.ts index 744f328..d10bb48 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,6 +3,7 @@ import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); + app.enableCors() await app.listen(AppModule.port); } bootstrap();