/melp_restaurant_api

Technical Test - A CRUD RestFul API written in FastAPI, using techonlogies like PostgreSQL and PostGIS for spacial queries.

Primary LanguagePython

Melp Restaurants API

Restful API for managing restaurants. Written in Python using FastAPI.

Live Demo

https://melp-restaurants-api.onrender.com


Table of Content 📑



Project Folder Structure 📂


.
├── alembic             # Migration tool.
│   └── versions        # Migrations made.
├── app                 # Main app folder.
│   ├── api             # Api related folder (routes, deps, versioning, etc.).
│   │   └── v1
│   │       └── routes  # Route folder where are all routes located for current version.
│   ├── core            # Core where locate all app configs,
│   │   └── settings    # like app settings, logging, etc.
│   ├── infrastructure  # Infrastructure related configs like databases, external resources, etc.
│   ├── models          # Entities representation of the data.
│   ├── repositories    # Code with database interaction (CRUD).
│   ├── schemas         # Pydantic schemas for validating and deserializing data.
│   ├── services        # Business logic like use cases.
│   └── utils           # Utils used in the app.
└── tests               # Test cases for the app.

Features 🚀


  1. All endpoints created for implementing CRUD (Create, Read, Update, Delete) operations.
  2. Specific business logic use case for retrieving the following fields:
    • count: Count of restaurants that fall inside the circle with center [x,y] and radius z.s
    • avg: Average rating of restaurant inside the circle.
    • std: Standard deviation of rating of restaurants inside the circle.

Endpoints 🌐

GET /

Retrieves a list of all restaurants.

GET /statistics?latitude=x&longitude=y&radius=z

Retrieves the count, average of rating and standard deviation of restaurants within a given radius of a location. Parameters include:

latitude: float - the latitude of the location
longitude: float - the longitude of the location
radius: int - the radius (in meters) to search for restaurants

GET /{id}

Retrieves a specific restaurant by ID.

POST /

Creates a new restaurant. The request body should include a RestaurantCreate model.

POST /bulk_create

Creates multiple new restaurants. The request body should include a list of RestaurantCreate models.

POST /bulk_create_from_csv

Creates multiple new restaurants from a CSV file. The file should be included in the request body as a UploadFile model.

PUT /{id}

Updates an existing restaurant. The request body should include a RestaurantUpdate model.

DELETE /{id}

Deletes an existing restaurant.

Error Handling

In case of error, an HTTPException will be raised with a message and status code.


How to run the code 🏃‍♂️


Installation 🔧

You will need to install Poetry to install project's dependencies

$ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3 -

Locate where Poetry is installed

$ whereis poetry

Copy and replace poetry's path to this line and added it at the end of the .bashrc file

$ export PATH="$HOME/.poetry/bin:$PATH"

Installing project's dependencies 📚


Clone the repository

$ git clone https://github.com/Arkemix30/melp_restaurant_api

Enter into project's root folder and run:

$ poetry install

It should create a .venv folder, generating a virtual enviroment with all project's dependencies


How to run locally ⚙️


  • To run the project, you need to activate the virtual environment. For that, you can run this command:

    $ poetry shell
  • And finally, to run the server:

    $ uvicorn main:app --reload

Built with 🛠️

  • FastAPI - The framework used
  • Uvicorn - The light-fast ASGI server
  • Pydantic - Data Validator using Python type annotations
  • SQLModel - Database ORM based in SQLAlchemy and Pydantic
  • GeoAlchemy2 - Provides extensions to SQLAlchemy for working with spatial databases.
  • Alembic - Database migration tool

README ⌨️ with ❤️ by Arkemix30 😊