Skip to content

Conversation

@Yevhen-Okolielov
Copy link
Collaborator

Inventory Management System

Inventory Management CRUD application built with FastAPI, Asyncpg (Raw SQL), and PostgreSQL. It includes a REST API, a CLI tool, and a Dockerized database environment.

Quick Start

1. Prerequisites

  • Docker & Docker Compose
  • Python 3.9+

2. Start the Database

The project uses a Dockerized PostgreSQL instance.

# Start the database container
docker compose up --build -d

# Verify it is healthy
docker compose ps

API URL: http://127.0.0.1:8000

Swagger UI: http://127.0.0.1:8000/docs

Database Port: 5432 (Exposed locally for debugging)

Default credentials: Defined in .env

Init: Schema is automatically applied from init.sql.

Usage Examples

Below are full examples of how to interact with the API using standard curl commands or the included Python CLI tool.

1. Create a Server

python3 app/cli.py create web-01 192.168.1.10 --state active
curl -X POST http://127.0.0.1:8000/servers \
  -H "Content-Type: application/json" \
  -d '{"hostname": "web-01", "ip_address": "192.168.1.10", "state": "active"}'

2. List All Servers

python3 app/cli.py list
curl -s http://127.0.0.1:8000/servers

3. Get Single Server Details

python3 app/cli.py get 1
curl -s http://127.0.0.1:8000/servers/1

4. Update a Server

python3 app/cli.py update 1 --ip 10.0.0.50 --state offline
curl -X PUT http://127.0.0.1:8000/servers/1 \
  -H "Content-Type: application/json" \
  -d '{"ip_address": "10.0.0.50", "state": "offline"}'

5. Delete a Server

python3 app/cli.py delete 1
curl -X DELETE http://127.0.0.1:8000/servers/1

CLI Specification

The command-line interface (app/cli.py) interacts directly with the running API.

Usage: python3 app/cli.py [COMMAND] [ARGS]

Command Arguments Description Example
list None Lists all servers in a formatted table. python3 app/cli.py list
get ID Retrieves details of a single server by ID. python3 app/cli.py get 1
create HOSTNAME IP [--state] Creates a new server. Default state is active. python3 app/cli.py create web-01 10.0.0.5 --state offline
update ID [--hostname] [--ip] [--state] Updates specific fields of an existing server. python3 app/cli.py update 1 --state retired
delete ID Deletes a server permanently. python3 app/cli.py delete 1

API Specification

The API follows RESTful standards. All request and response bodies are JSON.

Base URL: http://127.0.0.1:8000

Endpoints

1. List Servers

  • Method: GET
  • Endpoint: /servers
  • Description: Retrieve a list of all servers.
  • Response: 200 OK (Array of Server Objects)

2. Create Server

  • Method: POST
  • Endpoint: /servers
  • Body:
    {
      "hostname": "string (unique)",
      "ip_address": "string (IPv4/IPv6)",
      "state": "active | offline | retired (optional, default: active)"
    }
  • Response: 201 Created

3. Get Server

  • Method: GET
  • Endpoint: /servers/{id}
  • Description: Retrieve a single server by its numeric ID.
  • Response: 200 OK or 404 Not Found

4. Update Server

  • Method: PUT
  • Endpoint: /servers/{id}
  • Body: (Partial updates allowed)
    {
      "hostname": "new-name",
      "state": "retired"
    }
  • Response: 200 OK

5. Delete Server

  • Method: DELETE
  • Endpoint: /servers/{id}
  • Description: Delete a server by ID.
  • Response: 204 No Content

Validation Rules

  • Hostname: Must be non-empty and unique across the database.
  • IP Address: Must be a valid standard IPv4 or IPv6 address.
  • State: Strictly limited to active, offline, or retired.

Testing

The project includes a pytest suite that mocks the database connection, ensuring logic is tested without requiring a running DB.

# Run all tests
python3 -m pytest tests/test_main.py -v

Start the API Server locally

(http://127.0.0.1:8080)

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

# Run the API
uvicorn app.main:app --reload --port 8080

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants