added new EP
This commit is contained in:
@@ -1,16 +1,13 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
|
||||
import { AlumnoSancionService } from './alumno_sancion.service';
|
||||
import { CreateAlumnoSancionDto } from './dto/create-alumno_sancion.dto';
|
||||
import { AlumnoSancion } from './entities/alumno_sancion.entity';
|
||||
|
||||
@Controller('alumno-sancion')
|
||||
export class AlumnoSancionController {
|
||||
constructor(private readonly alumnoSancionService: AlumnoSancionService) {}
|
||||
|
||||
@Post()
|
||||
async create(
|
||||
@Body() createAlumnoSancionDto: CreateAlumnoSancionDto,
|
||||
): Promise<AlumnoSancion> {
|
||||
async create(@Body() createAlumnoSancionDto: CreateAlumnoSancionDto) {
|
||||
return this.alumnoSancionService.create(createAlumnoSancionDto);
|
||||
}
|
||||
|
||||
@@ -24,3 +21,4 @@ export class AlumnoSancionController {
|
||||
return this.alumnoSancionService.findOne(+id);
|
||||
}
|
||||
}
|
||||
//IO
|
||||
@@ -5,10 +5,18 @@ import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { AlumnoSancion } from './entities/alumno_sancion.entity';
|
||||
import { Alumno } from 'src/alumno/entities/student.entity';
|
||||
import { Sancion } from 'src/sancion/entities/sancion.entity';
|
||||
import { AlumnoModule } from 'src/alumno/student.module';
|
||||
import { SancionModule } from 'src/sancion/sancion.module';
|
||||
import { AlumnoService } from 'src/alumno/student.service';
|
||||
|
||||
@Module({
|
||||
imports:[TypeOrmModule.forFeature([AlumnoSancion,Alumno,Sancion])],
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([AlumnoSancion, Alumno, Sancion]),
|
||||
AlumnoModule,
|
||||
SancionModule,
|
||||
],
|
||||
controllers: [AlumnoSancionController],
|
||||
providers: [AlumnoSancionService],
|
||||
})
|
||||
export class AlumnoSancionModule {}
|
||||
//IO
|
||||
|
||||
@@ -3,25 +3,35 @@ import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { AlumnoSancion } from './entities/alumno_sancion.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { CreateAlumnoSancionDto } from './dto/create-alumno_sancion.dto';
|
||||
import { Sancion } from 'src/sancion/entities/sancion.entity';
|
||||
import { AlumnoService } from 'src/alumno/student.service';
|
||||
import { SancionService } from 'src/sancion/sancion.service';
|
||||
|
||||
@Injectable()
|
||||
export class AlumnoSancionService {
|
||||
constructor(
|
||||
@InjectRepository(AlumnoSancion)
|
||||
private readonly alumnosancionRepository: Repository<AlumnoSancion>,
|
||||
|
||||
private readonly alumnoService: AlumnoService,
|
||||
private readonly sancionService: SancionService,
|
||||
) {}
|
||||
|
||||
async create(
|
||||
createAlumnoSancionDto: CreateAlumnoSancionDto,
|
||||
): Promise<AlumnoSancion> {
|
||||
) {
|
||||
const { id_sancion, id_cuenta } = createAlumnoSancionDto;
|
||||
|
||||
// Crear la entidad con relaciones
|
||||
const alumno = await this.alumnoService.findOne(id_cuenta);
|
||||
const sancion = await this.sancionService.findOne(id_sancion);
|
||||
|
||||
if (!alumno)
|
||||
throw new NotFoundException(`Alumno ${id_cuenta} no encontrado`);
|
||||
if (!sancion)
|
||||
throw new NotFoundException(`Sancion ${id_sancion} no encontrada`);
|
||||
|
||||
const alumnosancion = this.alumnosancionRepository.create({
|
||||
id_sancion: { id_sancion }, // se asigna por FK
|
||||
id_cuenta: { id_cuenta }, // se asigna por FK
|
||||
alumno,
|
||||
sancion,
|
||||
});
|
||||
|
||||
return await this.alumnosancionRepository.save(alumnosancion);
|
||||
@@ -33,7 +43,7 @@ export class AlumnoSancionService {
|
||||
|
||||
async findOne(id_cuenta: number): Promise<AlumnoSancion> {
|
||||
const alusancion = await this.alumnosancionRepository.findOne({
|
||||
where: { id_cuenta: { id_cuenta } },
|
||||
where: { alumno: { id_cuenta } },
|
||||
});
|
||||
if (!alusancion) {
|
||||
throw new NotFoundException(`Student with ID ${id_cuenta} not found`);
|
||||
@@ -41,3 +51,4 @@ export class AlumnoSancionService {
|
||||
return alusancion;
|
||||
}
|
||||
}
|
||||
//IO
|
||||
@@ -23,15 +23,15 @@ export class AlumnoSancion {
|
||||
|
||||
@ManyToOne(() => Alumno, (student) => student.sanciones, {
|
||||
eager: true,
|
||||
nullable: true,
|
||||
nullable: false,
|
||||
})
|
||||
@JoinColumn({ name: 'id_cuenta' })
|
||||
id_cuenta: Alumno;
|
||||
alumno: Alumno;
|
||||
|
||||
@ManyToOne(() => Sancion, (sancion) => sancion.sancion, {
|
||||
eager: true,
|
||||
nullable: true,
|
||||
nullable: false,
|
||||
})
|
||||
@JoinColumn({ name: 'id_sancion' })
|
||||
id_sancion: Sancion;
|
||||
sancion: Sancion;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user