listo marcas
This commit is contained in:
+7
-3
@@ -48,6 +48,7 @@ import { InstitucionPrograma } from './institucion-programa/entity/institucion-p
|
||||
import { InstitucionTipoCarrito } from './institucion-tipo-carrito/entity/institucion-tipo-carrito.entity';
|
||||
import { InstitucionTipoEntrada } from './institucion-tipo-entrada/entity/institucion-tipo-entrada.entity';
|
||||
import { InstitucionUsuario } from './institucion-usuario/entity/institucion-usuario.entity';
|
||||
import { Marca } from './marca/entity/marca.entity';
|
||||
import { Modelo } from './modelo/entity/modelo.entity';
|
||||
import { Modulo } from './modulo/entity/modulo.entity';
|
||||
import { Motivo } from './motivo/entity/motivo.entity';
|
||||
@@ -62,6 +63,7 @@ import { TipoEntrada } from './institucion-tipo-entrada/entity/tipo-entrada.enti
|
||||
import { TipoUsuario } from './tipo-usuario/entity/tipo-usuario.entity';
|
||||
import { Usuario } from './usuario/entity/usuario.entity';
|
||||
import { SoapClientModule } from './soap-client/soap-client.module';
|
||||
import { MarcaModule } from './marca/marca.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -95,6 +97,7 @@ import { SoapClientModule } from './soap-client/soap-client.module';
|
||||
InstitucionTipoCarrito,
|
||||
InstitucionTipoEntrada,
|
||||
InstitucionUsuario,
|
||||
Marca,
|
||||
Modelo,
|
||||
Modulo,
|
||||
Motivo,
|
||||
@@ -128,19 +131,20 @@ import { SoapClientModule } from './soap-client/soap-client.module';
|
||||
InstitucionProgramaModule,
|
||||
InstitucionTipoCarritoModule,
|
||||
InstitucionTipoEntradaModule,
|
||||
InstitucionUsuarioModule,
|
||||
MarcaModule,
|
||||
ModeloModule,
|
||||
ModuloModule,
|
||||
MotivoModule,
|
||||
MultaModule,
|
||||
NodemailerModule,
|
||||
OperadorModule,
|
||||
PrestamoModule,
|
||||
SoapClientModule,
|
||||
StatusModule,
|
||||
TipoUsuarioModule,
|
||||
UploadFileModule,
|
||||
UsuarioModule,
|
||||
SoapClientModule,
|
||||
InstitucionUsuarioModule,
|
||||
ModeloModule,
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { IsString } from 'class-validator';
|
||||
|
||||
export class CreateMarcaDto {
|
||||
@IsString()
|
||||
marca;
|
||||
|
||||
@IsString()
|
||||
tipo;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { IsString } from 'class-validator';
|
||||
|
||||
export class MarcaDto {
|
||||
@IsString()
|
||||
tipo;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Expose } from 'class-transformer';
|
||||
|
||||
export class MarcaOutputDto {
|
||||
@Expose()
|
||||
id_marca;
|
||||
|
||||
@Expose()
|
||||
marca;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
|
||||
// import { Carrito } from '../../carrito/entity/carrito.entity';
|
||||
// import { Equipo } from '../../equipo/entity/equipo.entity';
|
||||
|
||||
@Entity()
|
||||
export class Marca {
|
||||
@PrimaryGeneratedColumn()
|
||||
id_marca: number;
|
||||
|
||||
@Column({ type: String, nullable: false, length: 20 })
|
||||
marca: string;
|
||||
|
||||
@Column({ type: String, nullable: false, length: 1 })
|
||||
tipo: string;
|
||||
|
||||
// @OneToMany(() => Carrito, (carrito) => carrito.marca)
|
||||
// carritos: Carrito[];
|
||||
|
||||
// @OneToMany(() => Equipo, (equipo) => equipo.marca)
|
||||
// equipos: Equipo[];
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { MarcaController } from './marca.controller';
|
||||
|
||||
describe('MarcaController', () => {
|
||||
let controller: MarcaController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [MarcaController],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<MarcaController>(MarcaController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
import { Body, Controller, Get, Post, Query, UseGuards } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import {
|
||||
ApiBearerAuth,
|
||||
ApiBody,
|
||||
ApiOperation,
|
||||
ApiQuery,
|
||||
ApiTags,
|
||||
} from '@nestjs/swagger';
|
||||
import { Serealize } from '../interceptors/serialize.interceptor';
|
||||
import { MarcaService } from './marca.service';
|
||||
import { CreateMarcaDto } from './dto/input/create.dto';
|
||||
import { MarcaDto } from './dto/input/marca.dto';
|
||||
import { MarcaOutputDto } from './dto/output/marca.dto';
|
||||
|
||||
@Controller('marca')
|
||||
@ApiTags('marca')
|
||||
export class MarcaController {
|
||||
constructor(private marcaService: MarcaService) {}
|
||||
|
||||
@Serealize(MarcaOutputDto)
|
||||
@Get()
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@ApiOperation({ description: 'Endpoint que retorna todass las marcas.' })
|
||||
@ApiBearerAuth('jwt')
|
||||
@ApiQuery({
|
||||
description: 'Tipo de marca que se busca.',
|
||||
name: 'tipo',
|
||||
type: 'text',
|
||||
})
|
||||
get(@Query() query: MarcaDto) {
|
||||
return this.marcaService.findAll(query.tipo);
|
||||
}
|
||||
|
||||
@Post()
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@ApiOperation({ description: 'Endpoint que crea una nueva marca.' })
|
||||
@ApiBearerAuth('jwt')
|
||||
@ApiBody({
|
||||
description: 'Todas las variables son obligatorias.',
|
||||
examples: { ejemplo: { value: { marca: '', tipo: '' } } },
|
||||
})
|
||||
create(@Body() body: CreateMarcaDto) {
|
||||
return this.marcaService.create(body.marca, body.tipo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { PassportModule } from '@nestjs/passport';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { MarcaController } from './marca.controller';
|
||||
import { MarcaService } from './marca.service';
|
||||
import { Marca } from './entity/marca.entity';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
PassportModule.register({ defaultStrategy: 'jwt' }),
|
||||
TypeOrmModule.forFeature([Marca]),
|
||||
],
|
||||
controllers: [MarcaController],
|
||||
providers: [MarcaService],
|
||||
exports: [MarcaService],
|
||||
})
|
||||
export class MarcaModule {}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { MarcaService } from './marca.service';
|
||||
|
||||
describe('MarcaService', () => {
|
||||
let service: MarcaService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [MarcaService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<MarcaService>(MarcaService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
import { ConflictException, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Marca } from './entity/marca.entity';
|
||||
|
||||
@Injectable()
|
||||
export class MarcaService {
|
||||
constructor(@InjectRepository(Marca) private repository: Repository<Marca>) {}
|
||||
|
||||
create(marca: string, tipo: string) {
|
||||
return this.findByMarca(marca, tipo, false)
|
||||
.then((existeMarca) => {
|
||||
if (existeMarca) throw new ConflictException('Ya existe esta marca.');
|
||||
return this.repository.save(this.repository.create({ marca, tipo }));
|
||||
})
|
||||
.then((_) => ({
|
||||
message: 'Se creó correctamente una nueva marca.',
|
||||
}));
|
||||
}
|
||||
|
||||
findAll(tipo: string) {
|
||||
return this.repository.find({ where: { tipo }, order: { tipo: 'ASC' } });
|
||||
}
|
||||
|
||||
findByMarca(marca: string, tipo: string, validarExistencia = true) {
|
||||
return this.repository.findOne({ marca, tipo }).then((marca) => {
|
||||
if (validarExistencia && !marca)
|
||||
throw new ConflictException('No existe esta marca.');
|
||||
return marca;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3,4 +3,7 @@ import { IsString } from 'class-validator';
|
||||
export class CreateModeloDto {
|
||||
@IsString()
|
||||
modelo;
|
||||
|
||||
@IsString()
|
||||
tipo;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import { IsString } from 'class-validator';
|
||||
|
||||
export class ModeloDto {
|
||||
@IsString()
|
||||
tipo;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
|
||||
import { Carrito } from '../../carrito/entity/carrito.entity';
|
||||
// import { Carrito } from '../../carrito/entity/carrito.entity';
|
||||
// import { Equipo } from '../../equipo/entity/equipo.entity';
|
||||
|
||||
@Entity()
|
||||
export class Modelo {
|
||||
@@ -9,6 +10,12 @@ export class Modelo {
|
||||
@Column({ type: String, nullable: false, length: 20 })
|
||||
modelo: string;
|
||||
|
||||
@OneToMany(() => Carrito, (carrito) => carrito.modelo)
|
||||
carritos: Carrito[];
|
||||
@Column({ type: String, nullable: false, length: 1 })
|
||||
tipo: string;
|
||||
|
||||
// @OneToMany(() => Carrito, (carrito) => carrito.modelo)
|
||||
// carritos: Carrito[];
|
||||
|
||||
// @OneToMany(() => Equipo, (equipo) => equipo.modelo)
|
||||
// equipos: Equipo[];
|
||||
}
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
|
||||
import { Body, Controller, Get, Post, Query, UseGuards } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import {
|
||||
ApiBearerAuth,
|
||||
ApiBody,
|
||||
ApiOperation,
|
||||
ApiQuery,
|
||||
ApiTags,
|
||||
} from '@nestjs/swagger';
|
||||
import { Serealize } from '../interceptors/serialize.interceptor';
|
||||
import { ModeloService } from './modelo.service';
|
||||
import { ModeloOutputDto } from './dto/output/modelo.dto';
|
||||
import { CreateModeloDto } from './dto/input/create.dto';
|
||||
import { ModeloDto } from './dto/input/modelo.dto';
|
||||
import { ModeloOutputDto } from './dto/output/modelo.dto';
|
||||
|
||||
@Controller('modelo')
|
||||
@ApiTags('modelo')
|
||||
@@ -16,15 +23,24 @@ export class ModeloController {
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@ApiOperation({ description: 'Endpoint que retorna todos los modelos.' })
|
||||
@ApiBearerAuth('jwt')
|
||||
get() {
|
||||
return this.modeloService.findAll();
|
||||
@ApiQuery({
|
||||
description: 'Tipo de marca que se busca.',
|
||||
name: 'tipo',
|
||||
type: 'text',
|
||||
})
|
||||
get(@Query() query: ModeloDto) {
|
||||
return this.modeloService.findAll(query.tipo);
|
||||
}
|
||||
|
||||
@Post()
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@ApiOperation({ description: 'Endpoint que crea un nuevo modelo.' })
|
||||
@ApiBearerAuth('jwt')
|
||||
@ApiBody({
|
||||
description: 'Todas las variables son obligatorias.',
|
||||
examples: { ejemplo: { value: { modelo: '', tipo: '' } } },
|
||||
})
|
||||
create(@Body() body: CreateModeloDto) {
|
||||
return this.modeloService.create(body.modelo);
|
||||
return this.modeloService.create(body.modelo, body.tipo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,23 +9,23 @@ export class ModeloService {
|
||||
@InjectRepository(Modelo) private repository: Repository<Modelo>,
|
||||
) {}
|
||||
|
||||
create(modelo: string) {
|
||||
return this.findByModelo(modelo, false)
|
||||
create(modelo: string, tipo: string) {
|
||||
return this.findByModelo(modelo, tipo, false)
|
||||
.then((existeModelo) => {
|
||||
if (existeModelo) throw new ConflictException('Ya existe este modelo.');
|
||||
return this.repository.save(this.repository.create({ modelo }));
|
||||
return this.repository.save(this.repository.create({ modelo, tipo }));
|
||||
})
|
||||
.then((_) => ({
|
||||
message: 'Se creó correctamente un nuevo modelo.',
|
||||
}));
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return this.repository.find();
|
||||
findAll(tipo: string) {
|
||||
return this.repository.find({ where: { tipo }, order: { tipo: 'ASC' } });
|
||||
}
|
||||
|
||||
findByModelo(modelo: string, validarExistencia = true) {
|
||||
return this.repository.findOne({ modelo }).then((modelo) => {
|
||||
findByModelo(modelo: string, tipo: string, validarExistencia = true) {
|
||||
return this.repository.findOne({ modelo, tipo }).then((modelo) => {
|
||||
if (validarExistencia && !modelo)
|
||||
throw new ConflictException('No existe este modelo.');
|
||||
return modelo;
|
||||
|
||||
Reference in New Issue
Block a user