carrito y status
This commit is contained in:
parent
bce48bf1c6
commit
3bffa52124
@ -1,22 +1,38 @@
|
||||
import { Controller, Get, Post, Put } from '@nestjs/common';
|
||||
import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common';
|
||||
import { CarritoService } from './carrito.service';
|
||||
import { CarritoCreateDto } from './dto/carrito-create.dto'
|
||||
import { CarritoUpdateDto } from './dto/carrito-update.dto'
|
||||
import { CarritoGetDto} from './dto/carrtio-get-dto'
|
||||
|
||||
@Controller('carrito')
|
||||
export class CarritoController {
|
||||
constructor(private carritoService: CarritoService) {}
|
||||
|
||||
@Post()
|
||||
create() {}
|
||||
create(@Body() body: CarritoCreateDto) {
|
||||
return this.carritoService.create(body.id_tipo_carrito, body.id_modulo, body.carrito)
|
||||
}
|
||||
|
||||
@Get()
|
||||
get() {}
|
||||
get() {
|
||||
return this.carritoService.findAll();
|
||||
}
|
||||
|
||||
@Get('carrito')
|
||||
carrito() {}
|
||||
carrito(@Query() query: CarritoGetDto) {
|
||||
return this.carritoService.findById(Number(query.id_carrito))
|
||||
}
|
||||
|
||||
@Get('carritos')
|
||||
carritos() {}
|
||||
carritos(@Query() query: CarritoGetDto) {
|
||||
return this.carritoService.findByIdModulo(Number(query.pagina), Number(query.id_modulo), Number(query.id_tipo_carrito), query.activo === "true")
|
||||
}
|
||||
|
||||
@Get('carritoInstitucion')
|
||||
carritosInstitucion() { }
|
||||
|
||||
@Put()
|
||||
update() {}
|
||||
update(@Body() body: CarritoUpdateDto) {
|
||||
return this.carritoService.update(body)
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,70 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { Repository } from 'typeorm';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Carrito } from './entity/carrito.entity';
|
||||
import { ModuloService } from '../modulo/modulo.service'
|
||||
import {InstitucionTipoCarritoService } from '../institucion-tipo-carrito/institucion-tipo-carrito.service'
|
||||
|
||||
@Injectable()
|
||||
export class CarritoService {
|
||||
constructor(
|
||||
@InjectRepository(Carrito) private repository: Repository<Carrito>,
|
||||
private moduloService: ModuloService,
|
||||
private institucionTipoCarritoService: InstitucionTipoCarritoService
|
||||
) { }
|
||||
|
||||
async create(id_tipo_carrito: number, id_modulo: number, carrito: string) {
|
||||
const modulo = await this.moduloService.findById(id_modulo)
|
||||
const tipoCarrito = await this.institucionTipoCarritoService.findByIdTipoCarito(id_tipo_carrito)
|
||||
const nuevoCarrito = this.repository.create({
|
||||
tipoCarrito,
|
||||
modulo,
|
||||
carrito
|
||||
});
|
||||
|
||||
return this.repository
|
||||
.findOne({ carrito, modulo })
|
||||
.then((existeCarrito) => {
|
||||
if (existeCarrito)
|
||||
throw new ConflictException('Este carrito ya existe en este modulo')
|
||||
return this.repository.save(nuevoCarrito);
|
||||
})
|
||||
.then(()=>({message: 'Se creo correctamente el carrito en el modulo.'}))
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return this.repository.find()
|
||||
}
|
||||
|
||||
findById(id_carrito: number) {
|
||||
return this.repository.findOne({ id_carrito }).then((carrito) => {
|
||||
if (!carrito) throw new NotFoundException('No existe este carrito');
|
||||
return carrito
|
||||
})
|
||||
}
|
||||
|
||||
findByIdModulo(pagina: number, id_modulo: number, id_tipo_carrito: number, activo: boolean) {
|
||||
return this.repository.findOne( id_modulo ).then((modulo) => {
|
||||
if (!modulo) throw new NotFoundException('No existen carritos es este modulo');
|
||||
return modulo
|
||||
})
|
||||
}
|
||||
|
||||
async update(attrs: Partial<Carrito>) {
|
||||
const carrito = await this.findById(attrs.id_carrito);
|
||||
|
||||
return this.repository
|
||||
.findOne({ carrito: attrs.carrito, modulo: carrito.modulo })
|
||||
.then((existeCarrito) => {
|
||||
if (existeCarrito)
|
||||
throw new ConflictException(
|
||||
'Ya existe un carrito con este número.'
|
||||
);
|
||||
Object.assign(carrito, attrs);
|
||||
return this.repository.save(carrito);
|
||||
})
|
||||
.then((_) => ({
|
||||
message: 'Se actualizo correctamente la información del carrito.',
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
12
src/carrito/dto/carrito-create.dto.ts
Normal file
12
src/carrito/dto/carrito-create.dto.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { IsNumber } from 'class-validator';
|
||||
|
||||
export class CarritoCreateDto {
|
||||
@IsNumber()
|
||||
id_tipo_carrito: number;
|
||||
|
||||
@IsNumber()
|
||||
id_modulo: number;
|
||||
|
||||
@IsNumber()
|
||||
carrito: string;
|
||||
}
|
14
src/carrito/dto/carrito-update.dto.ts
Normal file
14
src/carrito/dto/carrito-update.dto.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { IsBoolean, IsNumber, IsOptional, IsString } from 'class-validator';
|
||||
|
||||
export class CarritoUpdateDto {
|
||||
@IsNumber()
|
||||
id_carrito: number;
|
||||
|
||||
@IsNumber()
|
||||
@IsOptional()
|
||||
carrito?: string;
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
activo?: boolean;
|
||||
}
|
19
src/carrito/dto/carrtio-get-dto.ts
Normal file
19
src/carrito/dto/carrtio-get-dto.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { IsBoolean, IsNumber, IsOptional, IsString } from 'class-validator';
|
||||
|
||||
export class CarritoGetDto {
|
||||
@IsString()
|
||||
pagina: string;
|
||||
|
||||
@IsString()
|
||||
id_carrito: string;
|
||||
|
||||
@IsString()
|
||||
id_tipo_carrito: string;
|
||||
|
||||
@IsString()
|
||||
id_modulo: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
activo?: string;
|
||||
}
|
@ -90,6 +90,18 @@ export class InstitucionTipoCarritoService {
|
||||
});
|
||||
}
|
||||
|
||||
findByIdTipoCarito(id_tipo_carrito: number) {
|
||||
return this.repositoryTipoCarrito
|
||||
.findOne({ id_tipo_carrito })
|
||||
.then((tipoCarrito) => {
|
||||
if (!tipoCarrito)
|
||||
throw new NotFoundException(
|
||||
'No existe este tipo carrito.',
|
||||
);
|
||||
return tipoCarrito;
|
||||
})
|
||||
}
|
||||
|
||||
update(attrs: Partial<InstitucionTipoCarrito>) {
|
||||
return this.findById(attrs.id_institucion_tipo_carrito)
|
||||
.then((institucionTipoCarrito) => {
|
||||
|
@ -6,5 +6,7 @@ export class StatusController {
|
||||
constructor(private statusService: StatusService) {}
|
||||
|
||||
@Get()
|
||||
get() {}
|
||||
get() {
|
||||
return this.statusService.findAll();
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConflictException, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Status } from './entity/status.entity';
|
||||
@ -8,4 +8,8 @@ export class StatusService {
|
||||
constructor(
|
||||
@InjectRepository(Status) private repository: Repository<Status>,
|
||||
) {}
|
||||
|
||||
findAll() {
|
||||
return this.repository.find()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user