A RESTful API service for managing vehicle data using Node.js, Express, and PostgreSQL.
- Node.js (v14 or higher)
- PostgreSQL (v12 or higher)
- npm (Node Package Manager)
- Clone the repository:
git clone https://github.com/sennetsen/vehicle-service.git- Install the necessary dependencies (based on the entities listed in package.json):
npm install- Install homebrew (if not already installed) at https://brew.sh/
- Install Node.js using homebrew:
brew install node - Install PostgreSQL using homebrew:
brew install postgresqlOR download from https://www.postgresql.org/download/ - If using homebrew: start PostgreSQL service:
brew services start postgresql - Run
psql -U postgresto access the psql shell. This will open a new shell to be used for initializing the database.
- Create the PostgreSQL database as follows in the psql shell:
CREATE DATABASE vehicle_service;-
Press
\qto exit the psql shell. Now runpsql -U postgres -d vehicle_serviceto access the vehicle_service database. -
Create the vehicle table in the vehicle_service database:
CREATE EXTENSION IF NOT EXISTS citext;
CREATE TABLE vehicle (
vin CITEXT PRIMARY KEY,
manufacturer_name VARCHAR NOT NULL,
description VARCHAR NOT NULL,
horse_power INTEGER NOT NULL,
model_name VARCHAR NOT NULL,
model_year INTEGER NOT NULL,
purchase_price DECIMAL(1000, 2) NOT NULL,
fuel_type VARCHAR NOT NULL
);-
Press
\qto exit the psql shell. -
Create a
.envfile in the project directory with the following environment variables by runningnano .env:
DB_USER=postgres
DB_HOST=localhost
DB_NAME=vehicle_service
DB_PASSWORD=your_password
DB_PORT=5432- Note: the default username and password for PostgreSQL is usually
postgres.
- Now the server is ready to start. Start it with the following command:
npm startTo run the test suite, run the following command in the terminal:
npm testReturns a list of all vehicles in the database.
Returns a specific vehicle by searching for its VIN.
Creates a new vehicle object. Required fields include:
- vin (case-insensitive string)
- manufacturer_name (string)
- description (string)
- horse_power (integer)
- model_name (string)
- model_year (integer)
- purchase_price (decimal)
- fuel_type (string)
Updates an existing vehicle. It requires the same fields as POST.
Deletes a vehicle by searching for its VIN.
The API returns the following HTTP status codes, as appropriate:
- 200: Success
- 201: Vehicle object created
- 400: Bad/misformed request
- 404: Vehicle object not found
- 422: Validation error
- 500: Internal server error
