Skip to content

Commit

Permalink
Create initial learning schemas/models
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronWChen committed Aug 3, 2024
1 parent 1e9217a commit 8bdcdb8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
25 changes: 11 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
from enum import Enum
from fastapi import FastAPI, HTTPException
from schemas import GenreURLChoices, Band

# use localhost:{port} in browser
# use localhost:{port}/docs to look at the interactive, automatically created documentation

app = FastAPI()


class GenreURLChoices(Enum):
ROCK = "rock"
ELECTRONIC = "electronic"
METAL = "metal"
HIP_HOP = "hip-hop"
SHOEGAZE = "shoegaze"


BANDS = [
{"id": 1, "name": "The Kinks", "genre": "Rock"},
{"id": 2, "name": "Aphex Twin", "genre": "Electronic"},
{"id": 3, "name": "Slowdive", "genre": "Shoegaze"},
{"id": 4, "name": "Wu-Tang Clan", "genre": "Hip-Hop"},
{"id": 5, "name": "Black Sabbath", "genre": "Metal"},
{
"id": 5,
"name": "Black Sabbath",
"genre": "Metal",
"albums": [{"title": "Master of Reality", "release_date": "1971-07-21"}],
},
]


@app.get("/bands")
async def bands() -> list[dict]:
return BANDS
async def bands() -> list[Band]:
return [Band(**b) for b in BANDS]


@app.get("/bands/{band_id}")
async def band(band_id: int) -> dict:
band = next((b for b in BANDS if b["id"] == band_id), None)
async def band(band_id: int) -> Band:
band = next((Band(**b) for b in BANDS if b["id"] == band_id), None)
# Aaron: I'm a little confused, could we use `get` instead?

if band is None:
Expand Down
24 changes: 24 additions & 0 deletions schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# BugBytes instructor likes to use the schemas.py file and add all the Pydantic schemas to that file
from datetime import date
from enum import Enum
from pydantic import BaseModel


class GenreURLChoices(Enum):
ROCK = "rock"
ELECTRONIC = "electronic"
METAL = "metal"
HIP_HOP = "hip-hop"
SHOEGAZE = "shoegaze"


class Album(BaseModel):
title: str
release_date: date


class Band(BaseModel):
id: int
name: str
genre: str
albums: list[Album] = []

0 comments on commit 8bdcdb8

Please sign in to comment.