Skip to content

Commit

Permalink
Add vehicle endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kk5190 committed Oct 4, 2022
1 parent a53c51c commit 79d2387
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require('./modules/sports/api/sports-routes')(app); // Sports
require('./modules/users/api/user-routes')(app); //Users
require('./modules/music/api/music-routes')(app); // Music
require('./modules/quotes/api/qoutes-routes')(app); // Quotes
require('./modules/vehicles/api/vehicles-routes')(app); // Vehicles

// Add an healthcheck endpoint
app.get('/status', (req, res) => {
Expand Down
13 changes: 13 additions & 0 deletions modules/vehicles/api/vehicles-routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Request, Response } from 'express';
import * as core from 'express-serve-static-core';
import { getQtyFromRequest } from '../../../utils/route-utils';
import getRandomVehicles from '../utils/getRandomVehicles';

module.exports = function (app: core.Express) {
//Get a randomly generated user
app.get('/vehicles/:qty?', (req: Request, res: Response) => {
const qty = getQtyFromRequest(req, 1);
const users = getRandomVehicles(qty);
res.json(users);
});
};
12 changes: 12 additions & 0 deletions modules/vehicles/consts/Vehicle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type Vehicle = {
name: string;
type: string;
color: string;
fuel: string;
manufacturer: string;
model: string;
vin: string;
vrm: string;
};

export default Vehicle;
23 changes: 23 additions & 0 deletions modules/vehicles/utils/getRandomVehicles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Vehicle from '../consts/Vehicle';
import { faker } from '@faker-js/faker';

const getRandomVehicles = (qty: number) => {
const vehicles: Vehicle[] = [];

Array.from({ length: qty }).forEach(() => {
vehicles.push({
name: faker.vehicle.vehicle(),
type: faker.vehicle.type(),
color: faker.vehicle.color(),
fuel: faker.vehicle.fuel(),
manufacturer: faker.vehicle.manufacturer(),
model: faker.vehicle.model(),
vin: faker.vehicle.vin(),
vrm: faker.vehicle.vrm(),
});
});

return vehicles;
};

export default getRandomVehicles;
66 changes: 66 additions & 0 deletions swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
{
"name": "Sports",
"description": "A set of endpoints to get random sports data"
},
{
"name": "Vehicles",
"description": "A set of endpoints to get random vehicle data"
}
],
"paths": {
Expand Down Expand Up @@ -1932,6 +1936,28 @@
}
}
}
},
"/vehicles": {
"get": {
"tags": ["Vehicles"],
"summary": "Get a list of all vehicle from the database",
"parameters": [
{
"in": "path",
"name": "qty",
"default": 1,
"description": "The amount of results you would like returned"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/MockVehicles"
}
}
}
}
}
},
"definitions": {
Expand Down Expand Up @@ -2171,6 +2197,46 @@
}
}
}
},
"MockVehicles": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"example": "Jeep Model X"
},
"type": {
"type": "string",
"example": "Minivan"
},
"color": {
"type": "string",
"example": "tan"
},
"fuel": {
"type": "string",
"example": "Hybrid"
},
"manufacturer": {
"type": "string",
"example": "Jeep"
},
"model": {
"type": "string",
"example": "A8"
},
"vin": {
"type": "string",
"example": "NP1WJW8L8WWM52689"
},
"vrm": {
"type": "string",
"example": "JX30UEU"
}
}
}
}
}
}

0 comments on commit 79d2387

Please sign in to comment.