Skip to content

sh1vam31/Sales_Insight_Backend

Repository files navigation

Sales Insights Backend

Backend service for managing sales data and generating insights, based on the project PRD (Sales Insights Backend PRD.pdf).

Live Deployment

Production API: https://sales-insight-backend-7j96.onrender.com

Tech Stack

  • Framework: FastAPI
  • ORM: SQLAlchemy 2.0 (async)
  • Validation: Pydantic v2
  • Database:
    • Development: SQLite (via aiosqlite)
    • Production: PostgreSQL (via asyncpg)

Project Structure

  • app/main.py – FastAPI application instance and root route
  • app/database.py – SQLAlchemy async engine and session factory
  • app/models/ – ORM models (Sale model implemented)
  • app/schemas/ – Pydantic schemas for validation and serialization
  • app/routes/ – API routers for sales CRUD operations
  • app/services/ – Business logic and database operations
  • app/utils/ – Shared utility functions

Installation

  1. Navigate to project directory:
cd sales_insight
  1. Create virtual environment (optional but recommended):
python3 -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt

Running the Server

Start the development server:

uvicorn app.main:app --reload

The server will start on http://127.0.0.1:8000

Testing the Application

1. Health Check Endpoint

Local Development:

curl http://localhost:8000/

Production:

curl https://sales-insight-backend-7j96.onrender.com/

Expected response:

{"message": "Sales Insights Backend Running"}

2. Interactive API Documentation (Swagger UI)

FastAPI automatically generates interactive API documentation:

Local Development:

Production:

These pages show all available endpoints, request/response schemas, and allow you to test APIs directly from the browser.

3. Test API Endpoints

Create a Sale (Production):

curl -X POST https://sales-insight-backend-7j96.onrender.com/api/sales \
  -H "Content-Type: application/json" \
  -d '{
    "product_name": "Laptop",
    "quantity": 2,
    "price": 999.99,
    "sale_date": "2024-11-27"
  }'

List All Sales:

curl https://sales-insight-backend-7j96.onrender.com/api/sales

4. Verify Database Creation (Local Development)

The database file sales_insights.db will be created automatically when the server starts. You can verify it exists:

ls -lh sales_insights.db

To inspect the database structure (optional):

sqlite3 sales_insights.db ".schema sales"

Current Status

Completed:

  • Project structure setup
  • Database configuration (SQLAlchemy 2.0 async) with PostgreSQL support
  • Sale model with all fields, constraints, and indexes
  • Pydantic schemas (SaleCreate, SaleUpdate, SaleResponse)
  • Complete CRUD API routes for sales management
  • Service layer for business logic
  • Health check endpoint
  • Deployed to Render - Production API live at https://sales-insight-backend-7j96.onrender.com

API Endpoints

All endpoints are prefixed with /api/sales:

  • POST /api/sales - Create a new sale
  • GET /api/sales - List all sales (with optional filters: start_date, end_date, product_name)
  • GET /api/sales/{sale_id} - Get a specific sale by ID
  • PUT /api/sales/{sale_id} - Update a sale
  • DELETE /api/sales/{sale_id} - Delete a sale

Deployment

This application is deployed on Render:

For detailed deployment instructions, see DEPLOYMENT.md.

Development Tips

  • The --reload flag enables auto-reload on code changes
  • Check the terminal for startup logs and any errors
  • Database tables are created automatically on first startup
  • Use the /docs endpoint to explore and test APIs interactively
  • For production, the app automatically uses PostgreSQL when DATABASE_URL is set

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages