https://github.com/sweep76/fastapi-song-api
https://github.com/sweep76/fastapi-song-api
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/sweep76/fastapi-song-api
- Owner: Sweep76
- Created: 2025-07-02T04:54:44.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-07-02T05:20:54.000Z (about 1 year ago)
- Last Synced: 2025-07-02T05:33:35.694Z (about 1 year ago)
- Language: Python
- Size: 0 Bytes
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 🎵 FastAPI Song API
A backend application built using **FastAPI** and **SQLAlchemy** that allows users to manage songs, simulate purchases with gateway logic, and create/shuffle playlists.
> 📌 Submitted as part of the OpsWerks Academy backend technical assessment.
---
## 🚀 Features
### Song API
- Create, retrieve, update, and delete songs
- Each song includes:
- `title` (string)
- `length` (int in seconds)
- `date_released` (datetime)
- `price` (positive decimal)
### 💳 Purchase Endpoint
- Accepts a list of song IDs
- Determines the payment gateway based on total price:
- **CheapPaymentGateway** if total < 10
- **ExpensivePaymentGateway** if total ≥ 10
- Returns a success message after simulated payment
### 🎶 Playlist API _(Bonus)_
- Create a playlist containing multiple songs
- Shuffle the order of songs within the playlist
---
## 🧰 Tech Stack
- Python 3.11+
- FastAPI
- SQLAlchemy
- Pydantic v2
- SQLite (dev environment)
- Pytest (testing framework)
---
## 📁 Project Structure
├── app/
├── main.py
├── app/
│ ├── main.py
│ ├── models.py
│ ├── schemas.py
│ ├── database.py
│ └── routers/
│ ├── songs.py
│ ├── purchase.py
│ └── playlist.py
├── tests/
│ ├── test_songs.py
│ ├── test_purchase.py
│ └── test_playlist.py
├── requirements.txt
└── README.md
## 🛠 How to Run Locally
### 1. Clone the repository
```bash
git clone https://github.com/Sweep76/fastapi-song-api.git
cd fastapi-song-api
```
### 2. Create and activate a virtual environment
```bash
python -m venv venv
venv\Scripts\activate # Windows
# OR
source venv/bin/activate # macOS/Linux
```
### 3. Install dependencies
```bash
pip install -r requirements.txt
```
### 4. Start the development server
```bash
uvicorn app.main:app --reload
```
### 5. Open the Swagger UI
Visit: http://127.0.0.1:8000/docs
## Running Tests
Make sure you're in the root folder and run:
```bash
pytest
```
## Sample JSON Payloads
### Create Song
```json
POST /songs
{
"title": "Sample Song",
"length": 240,
"date_released": "2024-01-01T00:00:00",
"price": 5.99
}
```
### Purchase Songs
```json
POST /purchase
{
"song_ids": [1, 2, 3]
}
```
### Create Playlist
```json
POST /playlists
{
"name": "My Favorite Tracks",
"song_ids": [1, 2, 3]
}
```
### Shuffle Playlist
```bash
POST /playlists/{playlist_id}/shuffle
```