From 8f480f131726236ae3371ad52ecd1bb5c96fcc77 Mon Sep 17 00:00:00 2001 From: Andres2908 Date: Wed, 20 Apr 2022 18:02:12 -0500 Subject: [PATCH] carrera --- src/carrera/carrera.controller.ts | 11 ++++++++--- src/carrera/carrera.service.ts | 17 +++++++++++++++-- src/carrera/dto/carrera-get-dto.ts | 6 ++++++ src/carrito/dto/carrtio-get-dto.ts | 2 +- 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 src/carrera/dto/carrera-get-dto.ts diff --git a/src/carrera/carrera.controller.ts b/src/carrera/carrera.controller.ts index 61761b0..e363c90 100644 --- a/src/carrera/carrera.controller.ts +++ b/src/carrera/carrera.controller.ts @@ -1,13 +1,18 @@ -import { Controller, Get } from '@nestjs/common'; +import { Controller, Get, Query } from '@nestjs/common'; import { CarreraService } from './carrera.service'; +import {CarritoGetDto} from './dto/carrera-get-dto' @Controller('carrera') export class CarreraController { constructor(private carreraService: CarreraService) {} @Get() - get() {} + get() { + return this.carreraService.findAll() + } @Get("carreras") - carreras() {} + carreras(@Query() query: CarritoGetDto) { + return this.carreraService.findById(Number(query.id_carrera)) + } } diff --git a/src/carrera/carrera.service.ts b/src/carrera/carrera.service.ts index 02628ae..7534f83 100644 --- a/src/carrera/carrera.service.ts +++ b/src/carrera/carrera.service.ts @@ -1,4 +1,6 @@ -import { Injectable } from '@nestjs/common'; +import { ConflictException, + Injectable, + NotFoundException,} from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Carrera } from './entity/carrera.entity'; @@ -7,5 +9,16 @@ import { Carrera } from './entity/carrera.entity'; export class CarreraService { constructor( @InjectRepository(Carrera) private repository: Repository, - ) {} + ) { } + + findAll() { + return this.repository.find(); + } + + findById(id_carrera: number) { + return this.repository.findOne({ id_carrera }).then((carrera) => { + if (!carrera) throw new NotFoundException('No existe esta carrera') + return carrera + }) + } } diff --git a/src/carrera/dto/carrera-get-dto.ts b/src/carrera/dto/carrera-get-dto.ts new file mode 100644 index 0000000..9e0ea33 --- /dev/null +++ b/src/carrera/dto/carrera-get-dto.ts @@ -0,0 +1,6 @@ +import { IsString } from 'class-validator'; + +export class CarritoGetDto { + @IsString() + id_carrera: string; +} diff --git a/src/carrito/dto/carrtio-get-dto.ts b/src/carrito/dto/carrtio-get-dto.ts index 0009b46..bb84fde 100644 --- a/src/carrito/dto/carrtio-get-dto.ts +++ b/src/carrito/dto/carrtio-get-dto.ts @@ -1,4 +1,4 @@ -import { IsBoolean, IsNumber, IsOptional, IsString } from 'class-validator'; +import { IsOptional, IsString } from 'class-validator'; export class CarritoGetDto { @IsString()