move things around

This commit is contained in:
Tyrel Souza 2022-10-14 14:01:04 -04:00
parent 0e31cd9aec
commit 247e7720d1
No known key found for this signature in database
GPG Key ID: F6582CF1308A2360
5 changed files with 34 additions and 40 deletions

4
app.py
View File

@ -1,9 +1,9 @@
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
import api
from controllers import album
# Initialize the app
app = FastAPI()
app.include_router(api.router)
app.include_router(album.router)

View File

@ -1,9 +1,8 @@
from fastapi import APIRouter, Depends, HTTPException
from fastapi_utils.cbv import cbv
from sqlalchemy.orm import Session
from crud import get_all_albums, create_album, get_album_by_id, update_album, delete_album
from models.album import get_all_albums, create_album, get_album_by_id, update_album, delete_album, AlbumException
from database import get_db
from exceptions import AlbumException
from schemas import Album, CreateAndUpdateAlbum, PaginatedAlbum
router = APIRouter()
@ -22,7 +21,7 @@ class Albums:
return response
# API endpoint to add a album info to the database
# API endpoint to add an album to the database
@router.post("/albums")
def add_album(self, album: CreateAndUpdateAlbum):
@ -36,7 +35,6 @@ class Albums:
# API endpoint to get info of a particular album
@router.get("/albums/{album_id}", response_model=Album)
def get_album(album_id: int, session: Session = Depends(get_db)):
try:
album = get_album_by_id(session, album_id)
return album
@ -47,7 +45,6 @@ def get_album(album_id: int, session: Session = Depends(get_db)):
# API to update a existing album info
@router.put("/albums/{album_id}", response_model=Album)
def update_album(album_id: int, new_info: CreateAndUpdateAlbum, session: Session = Depends(get_db)):
try:
album = update_album(session, album_id, new_info)
return album
@ -55,10 +52,9 @@ def update_album(album_id: int, new_info: CreateAndUpdateAlbum, session: Session
raise HTTPException(**ex.__dict__)
# API to delete a album info from the data base
# API to delete an album info from the database
@router.delete("/albums/{album_id}")
def delete_album(album_id: int, session: Session = Depends(get_db)):
try:
return delete_album(session, album_id)
except AlbumException as ex:

View File

@ -1,15 +0,0 @@
# exceptions.py
class AlbumException(Exception):
...
class AlbumNotFoundError(AlbumException):
def __init__(self):
self.status_code = 404
self.detail = "Album Not Found"
class AlbumAlreadyExistError(AlbumException):
def __init__(self):
self.status_code = 409
self.detail = "Album Already Exists"

View File

@ -1,12 +0,0 @@
from sqlalchemy.schema import Column
from sqlalchemy.types import String, Integer, Float
from database import Base
class Album(Base):
__tablename__ = "albums"
id = Column(Integer, primary_key=True, index=True)
title = Column(String)
artist = Column(String)
price = Column(Float)

View File

@ -1,11 +1,36 @@
# crud.py
from sqlalchemy.schema import Column
from sqlalchemy.types import String, Integer, Float
from database import Base
from typing import List
from sqlalchemy.orm import Session
from exceptions import AlbumAlreadyExistError, AlbumNotFoundError
from models import Album
from schemas import CreateAndUpdateAlbum
class Album(Base):
__tablename__ = "albums"
id = Column(Integer, primary_key=True, index=True)
title = Column(String)
artist = Column(String)
price = Column(Float)
class AlbumException(Exception):
...
class AlbumNotFoundError(AlbumException):
def __init__(self):
self.status_code = 404
self.detail = "Album Not Found"
class AlbumAlreadyExistError(AlbumException):
def __init__(self):
self.status_code = 409
self.detail = "Album Already Exists"
# Function to get list of album info
def get_all_albums(session: Session, limit: int, offset: int) -> List[Album]:
return session.query(Album).offset(offset).limit(limit).all()
@ -51,7 +76,7 @@ def update_album(session: Session, _id: int, info_update: CreateAndUpdateAlbum)
return album
# Function to delete a album info from the db
# Function to delete an album info from the db
def delete_album(session: Session, _id: int):
album = get_album_by_id(session, _id)