# FastAPI MySQL Scalable App

This is a scalable FastAPI project with MySQL database integration, featuring complete CRUD operations, authentication, logging, and secure environment configuration.

## Project Structure

```
app/
├── main.py              # FastAPI application entry point
├── core/
│   ├── config.py        # Environment configuration
│   ├── database.py      # Database connection setup
│   └── auth.py          # JWT authentication utilities
├── models/
│   └── user.py          # SQLAlchemy models (software_user_tb)
├── schemas/
│   └── user.py          # Pydantic schemas
├── crud/
│   └── user.py          # Database CRUD operations
├── controllers/
│   └── user.py          # Business logic controllers
├── routes/
│   └── v1/
│       └── user_routes.py  # API route definitions
├── logs/                # Application logs
└── migrations/          # Database migrations
```
uvicorn app.main:app --reload
{
  "email": "string@gmail.com",
  "password": "Aashish@123"
}
## Environment Setup

### 1. Install Dependencies
```bash
pip install -r requirements.txt
```

### 2. Environment Configuration
Create a `.env` file in the root directory:

```env
# Database Configuration
DB_USER=root
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=3306
DB_NAME=cems_db

# Application Configuration
APP_TITLE="FastAPI MySQL Scalable App"
APP_DESCRIPTION="This is a scalable FastAPI project with MySQL and CRUD operations"
APP_VERSION="1.0.0"

# JWT Configuration
SECRET_KEY="your-secret-key-change-in-production"
ALGORITHM="HS256"
ACCESS_TOKEN_EXPIRE_MINUTES=30
```

### 3. Database Setup
- Ensure MySQL server is running
- Create the database: `CREATE DATABASE cems_db;`
- Run migrations: `alembic upgrade head`

### 4. Run the Application
```bash
uvicorn app.main:app --reload --log-config log_config.json
```

### 5. Access the API
- **Swagger UI**: http://127.0.0.1:8000/docs
- **ReDoc**: http://127.0.0.1:8000/redoc
- **API Base URL**: http://127.0.0.1:8000/api/v1

## User Table Structure (software_user_tb)

The user table includes the following fields:

### Personal Information
- `first_name` (required)
- `last_name` (required)
- `middle_name` (optional)
- `gender` (optional)

### Contact Information
- `phone_number` (optional)
- `email` (required, unique)

### File Storage
- `document_file` - Document file name/path
- `phone_file` - Phone-related file name in uploads
- `profile_photo` - Profile photo file name

### Software Access
- `software_user_holder` (optional)
- `has_software_access` (boolean, default: false)

### Authentication
- `normalized_password` - Lowercase password for comparison
- `hashed_password` - Bcrypt hashed password

### Status & Audit
- `is_active` (boolean, default: true)
- `created_by` (optional)
- `created_at` (timestamp)
- `last_updated_by` (optional)
- `last_updated_at` (timestamp, auto-update)

## API Endpoints

### Authentication
- `POST /api/v1/users/login` - User login (returns JWT token)

### Users (Protected - requires JWT token)
- `POST /api/v1/users/` - Create user
- `GET /api/v1/users/` - List all users
- `GET /api/v1/users/{user_id}` - Get user by ID
- `PUT /api/v1/users/{user_id}` - Update user
- `DELETE /api/v1/users/{user_id}` - Delete user
- `PATCH /api/v1/users/{user_id}/status` - Update user active status
- `PATCH /api/v1/users/{user_id}/software-access` - Update software access
- `GET /api/v1/users/profile/me` - Get current user profile

## Security Features

- **JWT Authentication**: Bearer token-based authentication
- **Password Hashing**: Bcrypt password hashing
- **Environment Variables**: Sensitive data stored in `.env` file
- **Git Security**: `.env` files excluded from version control
- **Input Validation**: Pydantic schemas for request/response validation
- **Audit Trail**: Created/updated by and timestamps

## Development

### Running Tests
```bash
pytest
```

### Database Migrations
```bash
# Create new migration
alembic revision --autogenerate -m "Migration message"

# Apply migrations
alembic upgrade head

# Rollback
alembic downgrade -1
```

### Authentication Example
```python
import requests

# Login
response = requests.post("http://127.0.0.1:8000/api/v1/users/login", json={
    "email": "user@example.com",
    "password": "password123"
})
token = response.json()["access_token"]

# Use token for authenticated requests
headers = {"Authorization": f"Bearer {token}"}
response = requests.get("http://127.0.0.1:8000/api/v1/users/", headers=headers)
```

### Logging
- Application logs: `app/logs/app.log`
- Server logs: `app/logs/server.log`
- All logs include timestamps and log levels

## File Upload Handling

The application supports file uploads for:
- Documents (`document_file`)
- Profile photos (`profile_photo`)
- Phone-related files (`phone_file`)

Files should be stored in an `uploads/` directory and only file names/paths are stored in the database.
└── utils/
