removed many unneeded async definitions. The main controller remains async
This commit is contained in:
@@ -21,7 +21,7 @@ class BaseController:
|
||||
self.ALL_FILES = ALLOWED_FILES_TYPES
|
||||
self.MAX_FILE_SIZE = ONE_MB
|
||||
|
||||
async def verify_image(self, file: UploadFile) -> bytes:
|
||||
def verify_image(self, file: UploadFile) -> bytes:
|
||||
"""Verifies that the file is an image and is within the maximum file size.
|
||||
|
||||
Args:
|
||||
@@ -38,7 +38,8 @@ class BaseController:
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"File type {file.content_type} not allowed. Allowed file types are {self.ALL_FILES}",
|
||||
)
|
||||
image_file = await file.read()
|
||||
with file.file as f:
|
||||
image_file = f.read()
|
||||
if len(image_file) > self.MAX_FILE_SIZE:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
|
||||
@@ -19,6 +19,8 @@ class MainController:
|
||||
All methods are either pass-throughs to the appropriate controller or
|
||||
are used to coordinate multiple controllers.
|
||||
|
||||
All methods are asynchronous to facilitate asynchronous calls from the Router layer.
|
||||
|
||||
token-based authentication is handled here as needed per the nature of the data being accessed.
|
||||
"""
|
||||
|
||||
@@ -29,10 +31,10 @@ class MainController:
|
||||
self.group_controller = GroupController()
|
||||
|
||||
async def get_musicians(self) -> list[Musician]:
|
||||
return await self.musician_controller.get_musicians()
|
||||
return self.musician_controller.get_musicians()
|
||||
|
||||
async def get_musician(self, id: int) -> Musician:
|
||||
return await self.musician_controller.get_musician(id)
|
||||
return self.musician_controller.get_musician(id)
|
||||
|
||||
async def update_musician(
|
||||
self,
|
||||
@@ -48,60 +50,60 @@ class MainController:
|
||||
detail="ID in URL does not match ID in request body",
|
||||
)
|
||||
_, sub = oauth_token.email_and_sub(token)
|
||||
await self.user_controller.get_user_by_sub(sub)
|
||||
return await self.musician_controller.update_musician(
|
||||
self.user_controller.get_user_by_sub(sub)
|
||||
return self.musician_controller.update_musician(
|
||||
musician_id=musician.id,
|
||||
new_bio=musician.bio,
|
||||
file=file,
|
||||
)
|
||||
|
||||
async def get_events(self) -> list[EventSeries]:
|
||||
return await self.event_controller.get_all_series()
|
||||
return self.event_controller.get_all_series()
|
||||
|
||||
async def get_event(self, id: int) -> EventSeries:
|
||||
return await self.event_controller.get_one_series_by_id(id)
|
||||
return self.event_controller.get_one_series_by_id(id)
|
||||
|
||||
async def create_event(
|
||||
self, series: NewEventSeries, token: HTTPAuthorizationCredentials
|
||||
) -> EventSeries:
|
||||
_, sub = oauth_token.email_and_sub(token)
|
||||
await self.user_controller.get_user_by_sub(sub)
|
||||
return await self.event_controller.create_series(series)
|
||||
self.user_controller.get_user_by_sub(sub)
|
||||
return self.event_controller.create_series(series)
|
||||
|
||||
async def add_series_poster(
|
||||
self, series_id: int, poster: UploadFile, token: HTTPAuthorizationCredentials
|
||||
) -> EventSeries:
|
||||
_, sub = oauth_token.email_and_sub(token)
|
||||
await self.user_controller.get_user_by_sub(sub)
|
||||
return await self.event_controller.add_series_poster(series_id, poster)
|
||||
self.user_controller.get_user_by_sub(sub)
|
||||
return self.event_controller.add_series_poster(series_id, poster)
|
||||
|
||||
async def delete_series(self, id: int, token: HTTPAuthorizationCredentials) -> None:
|
||||
_, sub = oauth_token.email_and_sub(token)
|
||||
await self.user_controller.get_user_by_sub(sub)
|
||||
await self.event_controller.delete_series(id)
|
||||
self.user_controller.get_user_by_sub(sub)
|
||||
self.event_controller.delete_series(id)
|
||||
|
||||
async def update_series(
|
||||
self, route_id: int, series: EventSeries, token: HTTPAuthorizationCredentials
|
||||
) -> EventSeries:
|
||||
_, sub = oauth_token.email_and_sub(token)
|
||||
await self.user_controller.get_user_by_sub(sub)
|
||||
return await self.event_controller.update_series(route_id, series)
|
||||
self.user_controller.get_user_by_sub(sub)
|
||||
return self.event_controller.update_series(route_id, series)
|
||||
|
||||
async def get_users(self) -> list[User]:
|
||||
return await self.user_controller.get_users()
|
||||
return self.user_controller.get_users()
|
||||
|
||||
async def get_user(self, id: int) -> User:
|
||||
return await self.user_controller.get_user_by_id(id)
|
||||
return self.user_controller.get_user_by_id(id)
|
||||
|
||||
async def create_user(self, token: HTTPAuthorizationCredentials) -> User:
|
||||
return await self.user_controller.create_user(token)
|
||||
return self.user_controller.create_user(token)
|
||||
|
||||
async def get_group(self) -> Group:
|
||||
return await self.group_controller.get_group()
|
||||
return self.group_controller.get_group()
|
||||
|
||||
async def update_group_bio(
|
||||
self, bio: str, token: HTTPAuthorizationCredentials
|
||||
) -> Group:
|
||||
_, sub = oauth_token.email_and_sub(token)
|
||||
await self.user_controller.get_user_by_sub(sub)
|
||||
return await self.group_controller.update_group_bio(bio)
|
||||
self.user_controller.get_user_by_sub(sub)
|
||||
return self.group_controller.update_group_bio(bio)
|
||||
|
||||
@@ -43,7 +43,7 @@ class EventController(BaseController):
|
||||
|
||||
return all_series
|
||||
|
||||
async def get_all_series(self) -> list[EventSeries]:
|
||||
def get_all_series(self) -> list[EventSeries]:
|
||||
"""Retrieves all EventSeries objects from the database and returns them as a list.
|
||||
|
||||
Raises:
|
||||
@@ -52,7 +52,7 @@ class EventController(BaseController):
|
||||
Returns:
|
||||
list[EventSeries]: A list of EventSeries objects which are suitable for a response body
|
||||
"""
|
||||
series_data = await self.db.select_all_series()
|
||||
series_data = self.db.select_all_series()
|
||||
|
||||
try:
|
||||
return [series for series in self._all_series(series_data).values()]
|
||||
@@ -63,7 +63,7 @@ class EventController(BaseController):
|
||||
detail=f"Error retrieving event objects: {e}",
|
||||
)
|
||||
|
||||
async def get_one_series_by_id(self, series_id: int) -> EventSeries:
|
||||
def get_one_series_by_id(self, series_id: int) -> EventSeries:
|
||||
"""Builds and returns a single EventSeries object by its numeric ID.
|
||||
|
||||
Args:
|
||||
@@ -76,7 +76,7 @@ class EventController(BaseController):
|
||||
Returns:
|
||||
EventSeries: A single EventSeries object
|
||||
"""
|
||||
if not (rows := await self.db.select_one_by_id(series_id)):
|
||||
if not (rows := self.db.select_one_by_id(series_id)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Event not found"
|
||||
)
|
||||
@@ -90,7 +90,7 @@ class EventController(BaseController):
|
||||
detail=f"Error creating event object: {e}",
|
||||
)
|
||||
|
||||
async def create_series(self, series: NewEventSeries) -> EventSeries:
|
||||
def create_series(self, series: NewEventSeries) -> EventSeries:
|
||||
"""Takes a NewEventSeries object and passes it to the database layer for insertion.
|
||||
|
||||
Args:
|
||||
@@ -103,19 +103,17 @@ class EventController(BaseController):
|
||||
EventSeries: The newly created EventSeries object with an ID
|
||||
"""
|
||||
try:
|
||||
inserted_id = await self.db.insert_one_series(series)
|
||||
inserted_id = self.db.insert_one_series(series)
|
||||
for new_event in series.events:
|
||||
await self.db.insert_one_event(new_event, inserted_id)
|
||||
return await self.get_one_series_by_id(inserted_id)
|
||||
self.db.insert_one_event(new_event, inserted_id)
|
||||
return self.get_one_series_by_id(inserted_id)
|
||||
except IntegrityError as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"Series name already exists. Each series must have a unique name.\n{e}",
|
||||
)
|
||||
|
||||
async def add_series_poster(
|
||||
self, series_id: int, poster: UploadFile
|
||||
) -> EventSeries:
|
||||
def add_series_poster(self, series_id: int, poster: UploadFile) -> EventSeries:
|
||||
"""Adds (or updates) a poster image to a series.
|
||||
|
||||
Args:
|
||||
@@ -125,12 +123,12 @@ class EventController(BaseController):
|
||||
Returns:
|
||||
EventSeries: The updated EventSeries object
|
||||
"""
|
||||
series = await self.get_one_series_by_id(series_id)
|
||||
series.poster_id = await self._upload_poster(poster)
|
||||
await self.db.update_series_poster(series)
|
||||
return await self.get_one_series_by_id(series.series_id)
|
||||
series = self.get_one_series_by_id(series_id)
|
||||
series.poster_id = self._upload_poster(poster)
|
||||
self.db.update_series_poster(series)
|
||||
return self.get_one_series_by_id(series.series_id)
|
||||
|
||||
async def _upload_poster(self, poster: UploadFile) -> str:
|
||||
def _upload_poster(self, poster: UploadFile) -> str:
|
||||
"""Uploads a poster image to Cloudinary and returns the public ID for storage in the database.
|
||||
Should only be used internally.
|
||||
|
||||
@@ -143,7 +141,7 @@ class EventController(BaseController):
|
||||
Returns:
|
||||
str: The public ID of the uploaded image
|
||||
"""
|
||||
image_file = await self.verify_image(poster)
|
||||
image_file = self.verify_image(poster)
|
||||
try:
|
||||
data = uploader.upload(image_file)
|
||||
return data.get("public_id")
|
||||
@@ -153,16 +151,16 @@ class EventController(BaseController):
|
||||
detail=f"Error uploading image: {e}",
|
||||
)
|
||||
|
||||
async def delete_series(self, id: int) -> None:
|
||||
def delete_series(self, id: int) -> None:
|
||||
"""Ensures an EventSeries object exists and then deletes it from the database.
|
||||
|
||||
Args:
|
||||
id (int): The numeric ID of the series to delete
|
||||
"""
|
||||
series = await self.get_one_series_by_id(id)
|
||||
await self.db.delete_one_series(series)
|
||||
series = self.get_one_series_by_id(id)
|
||||
self.db.delete_one_series(series)
|
||||
|
||||
async def update_series(self, route_id: int, series: EventSeries) -> EventSeries:
|
||||
def update_series(self, route_id: int, series: EventSeries) -> EventSeries:
|
||||
"""Updates an existing EventSeries object in the database.
|
||||
|
||||
Args:
|
||||
@@ -181,14 +179,14 @@ class EventController(BaseController):
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="ID in URL does not match ID in request body",
|
||||
)
|
||||
prev_series = await self.get_one_series_by_id(series.series_id)
|
||||
prev_series = self.get_one_series_by_id(series.series_id)
|
||||
if series.poster_id != prev_series.poster_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Poster ID cannot be updated directly. Use the /poster endpoint instead.",
|
||||
)
|
||||
await self.db.delete_events_by_series(series)
|
||||
await self.db.replace_series(series)
|
||||
self.db.delete_events_by_series(series)
|
||||
self.db.replace_series(series)
|
||||
for event in series.events:
|
||||
await self.db.insert_one_event(event, series.series_id)
|
||||
return await self.get_one_series_by_id(series.series_id)
|
||||
self.db.insert_one_event(event, series.series_id)
|
||||
return self.get_one_series_by_id(series.series_id)
|
||||
|
||||
@@ -11,8 +11,8 @@ class GroupController(BaseController):
|
||||
super().__init__()
|
||||
self.db: GroupQueries = group_queries
|
||||
|
||||
async def get_group(self) -> Group:
|
||||
if (data := await self.db.select_one_by_id()) is None:
|
||||
def get_group(self) -> Group:
|
||||
if (data := self.db.select_one_by_id()) is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Group not found"
|
||||
)
|
||||
@@ -24,12 +24,12 @@ class GroupController(BaseController):
|
||||
detail=f"Error creating group object: {e}",
|
||||
)
|
||||
|
||||
async def update_group_bio(self, bio: str) -> Group:
|
||||
def update_group_bio(self, bio: str) -> Group:
|
||||
try:
|
||||
await self.db.update_group_bio(bio)
|
||||
self.db.update_group_bio(bio)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Error updating group bio: {e}",
|
||||
)
|
||||
return await self.get_group()
|
||||
return self.get_group()
|
||||
|
||||
@@ -21,7 +21,7 @@ class MusicianController(BaseController):
|
||||
super().__init__()
|
||||
self.db: MusicianQueries = musician_queries
|
||||
|
||||
async def get_musicians(self) -> list[Musician]:
|
||||
def get_musicians(self) -> list[Musician]:
|
||||
"""Retrieves all musicians from the database and returns them as a list of Musician objects.
|
||||
|
||||
Raises:
|
||||
@@ -30,7 +30,7 @@ class MusicianController(BaseController):
|
||||
Returns:
|
||||
list[Musician]: A list of Musician objects which are suitable for a response body
|
||||
"""
|
||||
data = await self.db.select_all_series()
|
||||
data = self.db.select_all_series()
|
||||
try:
|
||||
return [Musician(**m) for m in data]
|
||||
except Exception as e:
|
||||
@@ -39,7 +39,7 @@ class MusicianController(BaseController):
|
||||
detail=f"Error creating musician objects: {e}",
|
||||
)
|
||||
|
||||
async def get_musician(self, musician_id: int) -> Musician:
|
||||
def get_musician(self, musician_id: int) -> Musician:
|
||||
"""Retrieves a single musician from the database and returns it as a Musician object.
|
||||
|
||||
Args:
|
||||
@@ -52,7 +52,7 @@ class MusicianController(BaseController):
|
||||
Returns:
|
||||
Musician: A Musician object which is suitable for a response body
|
||||
"""
|
||||
if (data := await self.db.select_one_by_id(musician_id)) is None:
|
||||
if (data := self.db.select_one_by_id(musician_id)) is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Musician not found"
|
||||
)
|
||||
@@ -64,7 +64,7 @@ class MusicianController(BaseController):
|
||||
detail=f"Error creating musician object: {e}",
|
||||
)
|
||||
|
||||
async def update_musician(
|
||||
def update_musician(
|
||||
self,
|
||||
musician_id: int,
|
||||
new_bio: str,
|
||||
@@ -83,18 +83,18 @@ class MusicianController(BaseController):
|
||||
Returns:
|
||||
Musician: The updated Musician object
|
||||
"""
|
||||
musician = await self.get_musician(musician_id)
|
||||
musician = self.get_musician(musician_id)
|
||||
if new_bio != musician.bio:
|
||||
return await self._update_musician_bio(musician, new_bio)
|
||||
return self._update_musician_bio(musician, new_bio)
|
||||
if file is not None:
|
||||
return await self._upload_headshot(musician, file)
|
||||
return self._upload_headshot(musician, file)
|
||||
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Update operation not implemented. Neither the bio or headshot was updated.",
|
||||
)
|
||||
|
||||
async def _update_musician_headshot(
|
||||
def _update_musician_headshot(
|
||||
self, musician: Musician, headshot_id: str
|
||||
) -> Musician:
|
||||
"""Updates a musician's headshot in the database.
|
||||
@@ -110,15 +110,15 @@ class MusicianController(BaseController):
|
||||
Musician: The updated Musician object
|
||||
"""
|
||||
try:
|
||||
await self.db.update_headshot(musician, headshot_id)
|
||||
self.db.update_headshot(musician, headshot_id)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Error updating musician headshot: {e}",
|
||||
)
|
||||
return await self.get_musician(musician.id)
|
||||
return self.get_musician(musician.id)
|
||||
|
||||
async def _update_musician_bio(self, musician: Musician, bio: str) -> Musician:
|
||||
def _update_musician_bio(self, musician: Musician, bio: str) -> Musician:
|
||||
"""Updates a musician's bio in the database.
|
||||
|
||||
Args:
|
||||
@@ -132,15 +132,15 @@ class MusicianController(BaseController):
|
||||
Musician: The updated Musician object
|
||||
"""
|
||||
try:
|
||||
await self.db.update_bio(musician, bio)
|
||||
self.db.update_bio(musician, bio)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Error updating musician bio: {e}",
|
||||
)
|
||||
return await self.get_musician(musician.id)
|
||||
return self.get_musician(musician.id)
|
||||
|
||||
async def _upload_headshot(self, musician: Musician, file: UploadFile) -> Musician:
|
||||
def _upload_headshot(self, musician: Musician, file: UploadFile) -> Musician:
|
||||
"""Uploads a new headshot for a musician and updates the database with the new public ID.
|
||||
|
||||
Args:
|
||||
@@ -153,7 +153,7 @@ class MusicianController(BaseController):
|
||||
Returns:
|
||||
Musician: The updated Musician object
|
||||
"""
|
||||
image_file = await self.verify_image(file)
|
||||
image_file = self.verify_image(file)
|
||||
data = uploader.upload(image_file)
|
||||
public_id = data.get("public_id")
|
||||
if public_id is None:
|
||||
@@ -161,6 +161,6 @@ class MusicianController(BaseController):
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to upload image",
|
||||
)
|
||||
await self._update_musician_headshot(musician, public_id)
|
||||
self._update_musician_headshot(musician, public_id)
|
||||
|
||||
return await self.get_musician(musician.id)
|
||||
return self.get_musician(musician.id)
|
||||
|
||||
@@ -13,8 +13,8 @@ class UserController(BaseController):
|
||||
super().__init__()
|
||||
self.db: UserQueries = user_queries
|
||||
|
||||
async def get_users(self) -> list[User]:
|
||||
data = await self.db.select_all_series()
|
||||
def get_users(self) -> list[User]:
|
||||
data = self.db.select_all_series()
|
||||
try:
|
||||
return [User(**e) for e in data]
|
||||
except Exception as e:
|
||||
@@ -23,8 +23,8 @@ class UserController(BaseController):
|
||||
detail=f"Error creating user objects: {e}",
|
||||
)
|
||||
|
||||
async def get_user_by_id(self, id: int) -> User:
|
||||
if (data := await self.db.select_one_by_id(id)) is None:
|
||||
def get_user_by_id(self, id: int) -> User:
|
||||
if (data := self.db.select_one_by_id(id)) is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|
||||
)
|
||||
@@ -36,8 +36,8 @@ class UserController(BaseController):
|
||||
detail=f"Error creating user object: {e}",
|
||||
)
|
||||
|
||||
async def get_user_by_email(self, email: str) -> User:
|
||||
if (data := await self.db.get_one_by_email(email)) is None:
|
||||
def get_user_by_email(self, email: str) -> User:
|
||||
if (data := self.db.get_one_by_email(email)) is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="User does not exist"
|
||||
)
|
||||
@@ -49,8 +49,8 @@ class UserController(BaseController):
|
||||
detail=f"Error creating user object: {e}",
|
||||
)
|
||||
|
||||
async def get_user_by_sub(self, sub: str) -> User:
|
||||
if (data := await self.db.get_one_by_sub(sub)) is None:
|
||||
def get_user_by_sub(self, sub: str) -> User:
|
||||
if (data := self.db.get_one_by_sub(sub)) is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|
||||
)
|
||||
@@ -62,9 +62,9 @@ class UserController(BaseController):
|
||||
detail=f"Error creating user object: {e}",
|
||||
)
|
||||
|
||||
async def create_user(self, token: HTTPAuthorizationCredentials) -> User:
|
||||
def create_user(self, token: HTTPAuthorizationCredentials) -> User:
|
||||
email, sub = oauth_token.email_and_sub(token)
|
||||
user: User = await self.get_user_by_email(email)
|
||||
user: User = self.get_user_by_email(email)
|
||||
if user.sub is None:
|
||||
await self.db.update_sub(user.email, sub)
|
||||
return await self.get_user_by_sub(sub)
|
||||
self.db.update_sub(user.email, sub)
|
||||
return self.get_user_by_sub(sub)
|
||||
|
||||
Reference in New Issue
Block a user