In this challenge, you will write an API that can be used to manage Cars stored in a Relational Database.
Your assignment page on Canvas should contain instructions for submitting this project. If you are still unsure, reach out to School Staff.
Build a RESTful API for an "cars" resource. The client for this API is a car dealer.
The critical information for each car is the vin, make, model, and mileage. They also track transmission type (manual, automatic...) and status of the title (clean, salvage...), but this information is not always immediately known. Write the "up" and "down" functions inside the data/migrations/01-make_cars_table.js migration file to satisfy the following schema:
| field | data type | metadata |
|---|---|---|
| id | unsigned integer | primary key, auto-increments, generated by database |
| vin | string | required, unique |
| make | string | required |
| model | string | required |
| mileage | numeric | required |
| title | string | optional |
| transmission | string | optional |
-
Write the following db access functions inside
api/cars/cars-model.jsusing Knex:getAllresolves to an array of car records (or an empty array)getByIdresolves to a car record by the given idcreateresolves to the newly created car record
-
Write the following middlewares inside
api/cars/cars-middleware.js:-
checkCarIdreturns a status 404 with a{ message: "car with id <car id> is not found" }if the id inreq.paramsdoes not exist in the database. -
checkCarPayloadreturns a status 400 with a{ message: "<field name> is missing" }if any required field is missing. -
checkVinNumberValidreturns a status 400 with a{ message: "vin <vin number> is invalid" }if the vin number is invalid. -
checkVinNumberUniquereturns a status 400 with a{ message: "vin <vin number> already exists" }if the vin number already exists in the database.
-
-
Write CR (of CRUD) for the
carsresource, using the middleware and model functions described above wherever appropriate insideapi/cars/cars-router.js:[GET] /api/carsreturns an array of cars sorted by id (or an empty array if there aren't any).[GET] /api/cars/:idreturns a car by the given id.[POST] /api/carsreturns the created car.
-
Manually test your endpoints with a REST client like
InsomniaorPostmanto check they are working as expected. -
Test your endpoints automatically by running
npm test.
- Test your work manually using Postman or HTTPie. Run automatic tests by executing
npm test. - You are welcome to create additional files but do not move or rename existing files or folders.
- Do not alter your
package.jsonfile except to install additional libraries or add additional scripts. Do not update existing libs. - In your solution, it is essential that you follow best practices and produce clean and professional results.
- Add seed data to the database using
knex seeds - Add
[PUT]and[DELETE]operations to your API. - Write a schema file for a
salestable. This table should track information on the sale of each car. You may wish to researchforeign keysin order to link each sale to the entry incarswhich sold.