Skip to content

baky0905/rental-inspection-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Vehicle Rental Inspection Data Modelling and REST API



Report Bug · Request Feature

What is a vehicle inspection app and how could it look like?

image

Vehicle inspection app is an application where drivers would "rent" cars from their car fleet, but before they can checkout the car, they need to preform an inspection , i.e. go through a set of questions based on the vehicle category type, answer them, take photos and comment.

This is my idea of the user flow:

user finds the right vehicle --> inspect the vehicle --> check-out the vehicle:

User arrives at a place where he/she either chooses a dropdown/seachbar to find the right vehicle that he is going to inspect OR he uses the QR code to scan for the right vehicle. After the right vehicle has been selected, the vehicle’s photo and summary appears as a QC of the right selection. In addition, it shows if the vehicle has been inspected within its frequncy rules. Need inspection (red notification shows up next to the inspection button) or doesn’t need inspection. If there is a need for inspection, user must perform an inspecton. Otherwise, if someone else has done an inspection already, user can choose if he wishes to checkout a vehicle + some notes.

User starts inspection of the vehicle. Goes though a checklist that is characteristic for a vehicle type, answers if it is ok or not ok, and if not ok comments it together with a photo of an issue area on the vehicle. After the inspection is complete, user goes to the checkout-out page.

User sees a summary of the inspection on aggregated cards - number of issues, number of photos, number of comments, and finally a signature box with a submit a check-out button.

About The Project

In this project, I developed a backend for the vehicle inspection web application (web application will not be a part of this project).

The project included tasks as follows:

  • Data modelling for the vehicle inspection process.
  • Implementation of the data model into a PostgreSQL relational database, initialized with mandatory data tables for a functional application.
  • Development of data REST API, using Python and FastAPI framework on top of the PostgreSQL database, enabling relevant CRUD operations for the vehicle inspection application.

The end result can be visualised and tested via API documentation locally when the stack is running (more info on how to later in the readme).

All API endpoints need a JWT token for authorized accessed. For the time being, there is a dummy admin user who has privileges to get authorized, and therefore able to request resources from all available endpoints.

Below, there is an example of a crud operation wherein the inspection process, inspector (user) can answer the inspection question vai the POST answer endpoint.

Backend Requirements

Note that I am developing in WSL2 (Windows Subsystem for Linux), Ubuntu 20.04 distribution, using my favourite code editor - Visual Studio Code, and its set of extensions from which I would especially recommend Docker extension for easy creation, management and debugging of containerized applications.

Backend - local development

  • Start the stack with Docker Compose:
docker-compose up -d

Now you can open your browser and interact with these URLs:

Since the app is for learning purposes, .env with all the passwords and secrets has been version controlled. Do not do this in real life :)

To check the logs, run:

docker-compose logs

To check the logs of a specific service, add the name of the service, for example to check the server(api), run:

docker-compose logs server

If your Docker is not running in localhost (the URLs above wouldn't work).

Note: The first time you start your stack, it might take a minute for it to be ready. While the server waits for the database to be ready and configures everything, you can check the logs to monitor it.

Getting Started

To start the stack of services, database, pgadmin and server(api), run:

$ docker-compose up -d

and then exec inside the running container:

$ docker-compose exec server bash

You should see an output like:

root@7f2607af31c3:/app#

I personally, like to user VS Code's Docker extension and if yo uare doing the same, you can just right-click on the docker-compose.yml and command Compose Up.

Project Structure

Below is a structure of the project. I will briefly describe most important python files under the app directory:

.
├── README.md
├── backend
│   ├── Dockerfile
│   ├── __init__.py
│   ├── app
│   │   ├── __init__.py
│   │   ├── crud.py
│   │   ├── database.py
│   │   ├── load.py
│   │   ├── main.py
│   │   ├── models.py
│   │   ├── routes
│   │   ├   └──v1.py
│   │   ├── schemas.py
│   │   └── utils
│   ├── entrypoint.sh
│   ├── requirements.txt
│   └── venv
├── db_init
│   ├── __init__.py
│   ├── data
│   │   ├── answer.csv
│   │   ├── category.csv
│   │   ├── category_question.csv
│   │   ├── check_log.csv
│   │   ├── driver.csv
│   │   ├── question.csv
│   │   ├── signature.csv
│   │   └── vehicle.csv
│   ├── db-drop-create-load-csvs.py
│   └── sql_queries
│       ├── create-tables.sql
│       ├── drop-tables.sql
│       ├── example_query.sql
│       └── sql_queries.py
├── docker-compose.yml
├── img
└── servers.json
  • database.py
    • imports sqlalchemy part and engine, SessionLocal and Base as declarative_base()
  • models.py
    • Creates SQLAlchemy models from the Base class
    • These classes are SQLAlchemy models
    • The tablename attribute tells SQLAlchemy the name of the table to use in the database for each of these models.
    • Creates model attributes/columns
    • Creates the relationships via relationship provided by SQLAlchemy ORM.
  • schemas.py
    • These Pydantic models define more or less a "schema" (a valid data shape).
  • crud.py
    • In this file, we will have reusable functions to interact with the data in the database.
      • CRUD comes from: Create, Read, Update, and Delete.
  • routes/v1.py
    • contains all the api endpoints for the api version 1
  • main.py
    • integrates and uses all the other parts from above
    • contains middleware with login endpoints, all endpoints require jwt token
  • utils/security.py
    • authentication, jwt token creation and verification related functions.

Entity Relationshop Diagram (ERD)

ERD has been designed in https://dbdiagram.io/, and I highly recommend it. Schema can be found in the local folder, and diagram can easily be reconstructed from it via the website.

Initialization of the Database

By default, the dependencies are managed with requirements.txt and pip.

Open your editor at ./backend/ (instead of the project root: ./). Create a virtual environment while in ./backend folder.

Run following command:

python3 -m venv venv

and activate the env:

python3 venv/bin/activate

Install packages from requirements.txt with pip3:

pip3 install -r requirements.txt

Finally, run a python script db-drop-create-load-csvs.py located in db_init folder:

python3 db_init/db-drop-create-load-csvs.py 

The db-drop-create-load-csvs.py script will do the following:

  • drops all tables in postgres database - script
  • creates all tables in postgres database - script
  • populates created tables with data / CSVs, located in data_init folder

Available REST API endpoints

These are the available API endpoints:

Roadmap

See the open issues for a list of proposed features (and known issues).

To do:

  • validate with data types, regex and enums at the API layer
  • replace dummy admin user with users in the databases, with respective roles
  • cache with redis
  • test endpoints
  • load testing
  • deploy API to production environment
  • load balancers
  • get a domain, reverse proxy, https, Auth 2.0

License

Distributed under the MIT License.

Contact

Kristijan Bakaric - twitter.com/kbakaric1

About

Vehicle Rental Inspection Data Modelling and REST API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published