Merge pull request #3 from Hzin/feature/VAN-6-van-models-table
Remove tabela CarBrands e inclui rotas para recuperar marcas e modelos
This commit is contained in:
@@ -17,4 +17,4 @@ export class InsertDataIntoCarModelsTable1653769103891 implements MigrationInter
|
||||
.from(CarModels)
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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')
|
||||
}
|
||||
}
|
||||
28
src/database/migrations/1661745915711-DropCarBrandsTable.ts
Normal file
28
src/database/migrations/1661745915711-DropCarBrandsTable.ts
Normal 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
12
src/models/CarBrands.ts
Normal 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;
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user