Remove tabela CarBrands e inclui rotas para recuperar marcas e modelos

This commit is contained in:
Matheus Albino Brunhara
2022-08-29 01:30:23 -03:00
parent b4998b7c3e
commit 22a1a3dbfa
6 changed files with 1156 additions and 1047 deletions

View File

@@ -17,4 +17,4 @@ export class InsertDataIntoCarModelsTable1653769103891 implements MigrationInter
.from(CarModels)
.execute();
}
}
}

View File

@@ -0,0 +1,15 @@
import {MigrationInterface, QueryRunner} from "typeorm";
export class RenameCarModelsTableToCarBrands1661212542739 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.renameTable('carModels', 'carBrands')
await queryRunner.renameColumn('carBrands', 'id_model', 'id_brand')
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.renameTable('carBrands', 'carModels')
await queryRunner.renameColumn('carBrands', 'id_brand', 'id_model')
}
}

View File

@@ -0,0 +1,28 @@
import {MigrationInterface, QueryRunner, Table} from "typeorm";
export class DropCarBrandsTable1661745915711 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('carBrands')
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: 'carModels',
columns: [
{
name: 'id_brand',
type: 'uuid',
isPrimary: true,
generationStrategy: 'uuid',
default: 'uuid_generate_v4()',
},
{
name: 'name',
type: 'varchar',
}
],
}),
);
}
}

12
src/models/CarBrands.ts Normal file
View File

@@ -0,0 +1,12 @@
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity('carBrands')
class CarBrands {
@PrimaryGeneratedColumn('uuid')
id_brand: string;
@Column()
name: string;
}
export default CarBrands;

View File

@@ -1,4 +1,7 @@
import { Router } from 'express';
import axios from 'axios'
import AppError from '../errors/AppError';
import ensureAuthenticated from '../middlewares/ensureAuthenticated';
@@ -6,12 +9,63 @@ import GetCarModelsService from '../services/GetCarModelsService';
const carsRouter = Router();
carsRouter.get('/list', ensureAuthenticated, async (request, response) => {
const getCarModelsService = new GetCarModelsService();
type Brand = {
codigo: string;
nome: string;
};
const carModels = await getCarModelsService.execute();
type GetBrandsResponse = {
data: Brand[];
};
return response.json({ data: carModels });
type Model = {
modelos: {
codigo: string;
nome: string;
}
};
type GetModelsResponse = {
modelos: Model[];
};
carsRouter.get('/brands/list', async (request, response) => {
// const getCarModelsService = new GetCarModelsService();
// const carModels = await getCarModelsService.execute();
const { data, status } = await axios.get<GetBrandsResponse>(
'https://parallelum.com.br/fipe/api/v1/carros/marcas',
{
headers: {
Accept: 'application/json',
},
},
);
if (status !== 200) {
throw new AppError('Não foi possível recuperar a lista de marcas de veículos.', 200);
}
return response.json({ data: data });
});
carsRouter.get('/models/list/:id', async (request, response) => {
const { id } = request.params;
const { data, status } = await axios.get<GetModelsResponse>(
`https://parallelum.com.br/fipe/api/v1/carros/marcas/${id}/modelos`,
{
headers: {
Accept: 'application/json',
},
},
);
if (status !== 200) {
throw new AppError('Não foi possível recuperar a lista de modelos do veículo informado.', 200);
}
return response.json({ data: data.modelos });
});
export default carsRouter;

2084
yarn.lock

File diff suppressed because it is too large Load Diff