From 247e7720d1323d93df063aee031747630b32a56a Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Fri, 14 Oct 2022 14:01:04 -0400 Subject: [PATCH] move things around --- app.py | 4 ++-- api.py => controllers/album.py | 10 +++------- exceptions.py | 15 --------------- models.py | 12 ------------ crud.py => models/album.py | 33 +++++++++++++++++++++++++++++---- 5 files changed, 34 insertions(+), 40 deletions(-) rename api.py => controllers/album.py (88%) delete mode 100644 models.py rename crud.py => models/album.py (69%) diff --git a/app.py b/app.py index 53e4b98..d8bdb06 100644 --- a/app.py +++ b/app.py @@ -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) diff --git a/api.py b/controllers/album.py similarity index 88% rename from api.py rename to controllers/album.py index 066518b..464b2ec 100644 --- a/api.py +++ b/controllers/album.py @@ -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: diff --git a/exceptions.py b/exceptions.py index 37ad34f..e69de29 100644 --- a/exceptions.py +++ b/exceptions.py @@ -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" diff --git a/models.py b/models.py deleted file mode 100644 index f9a7c3a..0000000 --- a/models.py +++ /dev/null @@ -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) diff --git a/crud.py b/models/album.py similarity index 69% rename from crud.py rename to models/album.py index b133825..b663487 100644 --- a/crud.py +++ b/models/album.py @@ -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)