Skip to content

Sweep76/fastapi-song-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🎵 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

git clone https://github.com/Sweep76/fastapi-song-api.git
cd fastapi-song-api

2. Create and activate a virtual environment

python -m venv venv
venv\Scripts\activate  # Windows
# OR
source venv/bin/activate  # macOS/Linux

3. Install dependencies

pip install -r requirements.txt

4. Start the development server

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:

pytest

Sample JSON Payloads

Create Song

POST /songs
{
  "title": "Sample Song",
  "length": 240,
  "date_released": "2024-01-01T00:00:00",
  "price": 5.99
}

Purchase Songs

POST /purchase
{
  "song_ids": [1, 2, 3]
}

Create Playlist

POST /playlists
{
  "name": "My Favorite Tracks",
  "song_ids": [1, 2, 3]
}

Shuffle Playlist

POST /playlists/{playlist_id}/shuffle

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages