Skip to content

Commit

Permalink
code reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
do-ko committed Jan 20, 2024
1 parent 17644c2 commit 8d07d35
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 60 deletions.
4 changes: 1 addition & 3 deletions src/api/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,4 @@
v1_router.include_router(
user_audiobook.router, prefix="/user_audiobook", tags=["user_audiobook"]
)
v1_router.include_router(
review.router, prefix="/reviews", tags=["reviews"]
)
v1_router.include_router(review.router, prefix="/reviews", tags=["reviews"])
23 changes: 11 additions & 12 deletions src/api/v1/endpoints/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
from src.db import get_async_session
from src.repo.account import AccountRepo
from src.schemas.account import AccountSchema
from src.schemas.review import ReviewSchema

router = APIRouter()


@router.get("/", response_model=list[AccountSchema])
async def get_users(
request: Request,
session: AsyncSession = Depends(get_async_session), # Getting a database session
request: Request,
session: AsyncSession = Depends(get_async_session), # Getting a database session
):
auth_header = request.headers.get("Authorization")
authorize_user(auth_header)
Expand All @@ -22,9 +21,9 @@ async def get_users(

@router.get("/{account_id}", response_model=AccountSchema)
async def get_user(
request: Request,
account_id: int,
session: AsyncSession = Depends(get_async_session), # Getting a database session
request: Request,
account_id: int,
session: AsyncSession = Depends(get_async_session), # Getting a database session
):
auth_header = request.headers.get("Authorization")
authorize_user(auth_header)
Expand All @@ -33,11 +32,11 @@ async def get_user(

@router.post("/", response_model=AccountSchema)
async def create_account(
request: Request,
account_id: int,
username: str,
is_active: bool,
session: AsyncSession = Depends(get_async_session),
request: Request,
account_id: int,
username: str,
is_active: bool,
session: AsyncSession = Depends(get_async_session),
):
auth_header = request.headers.get("Authorization")
authorize_user(auth_header)
Expand All @@ -51,5 +50,5 @@ async def create_account(
username=username,
is_active=is_active,
created_at=created_at,
updated_at=updated_at
updated_at=updated_at,
)
60 changes: 32 additions & 28 deletions src/api/v1/endpoints/audiobook.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

@router.get("/", response_model=list[AudiobookSchema])
async def get_audiobooks(
request: Request,
category_id: int = None,
session: AsyncSession = Depends(get_async_session),
request: Request,
category_id: int = None,
session: AsyncSession = Depends(get_async_session),
):
auth_header = request.headers.get("Authorization")
authorize_user(auth_header)
Expand All @@ -23,9 +23,9 @@ async def get_audiobooks(

@router.get("/{audiobook_id}", response_model=AudiobookSchema)
async def get_audiobooks(
request: Request,
audiobook_id: int = None,
session: AsyncSession = Depends(get_async_session),
request: Request,
audiobook_id: int = None,
session: AsyncSession = Depends(get_async_session),
):
auth_header = request.headers.get("Authorization")
authorize_user(auth_header)
Expand All @@ -34,31 +34,35 @@ async def get_audiobooks(

@router.post("/", response_model=AudiobookSchema)
async def create_audiobook(
request: Request,
audiobook_id: int,
category_id: int,
title: str,
author: str,
description: str,
duration: int,
cover_image: str,
listened_times: int = 0,
rating: float = 0,
session: AsyncSession = Depends(get_async_session),
request: Request,
audiobook_id: int,
category_id: int,
title: str,
author: str,
description: str,
duration: int,
cover_image: str,
listened_times: int = 0,
rating: float = 0,
session: AsyncSession = Depends(get_async_session),
):
auth_header = request.headers.get("Authorization")
authorize_user(auth_header)

if not await CategoryRepo.exists(session, category_id=category_id):
raise HTTPException(status_code=404, detail=f'No category with ip {category_id} was found!')
raise HTTPException(
status_code=404, detail=f"No category with ip {category_id} was found!"
)

return await AudiobookRepo.create(session,
audiobook_id=audiobook_id,
category_id=category_id,
title=title,
author=author,
description=description,
duration=duration,
cover_image=cover_image,
listened_times=listened_times,
rating=rating)
return await AudiobookRepo.create(
session,
audiobook_id=audiobook_id,
category_id=category_id,
title=title,
author=author,
description=description,
duration=duration,
cover_image=cover_image,
listened_times=listened_times,
rating=rating,
)
12 changes: 7 additions & 5 deletions src/api/v1/endpoints/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ async def create_category(
authorize_user(auth_header)

if await CategoryRepo.exists(session, category_id=category_id):
raise HTTPException(status_code=409, detail=f'Category with ip {category_id} already exists!')
raise HTTPException(
status_code=409, detail=f"Category with ip {category_id} already exists!"
)

if await CategoryRepo.exists(session, name=name):
raise HTTPException(status_code=409, detail=f'Category with name {name} already exists!')
raise HTTPException(
status_code=409, detail=f"Category with name {name} already exists!"
)

return await CategoryRepo.create(session,
category_id=category_id,
name=name)
return await CategoryRepo.create(session, category_id=category_id, name=name)
20 changes: 14 additions & 6 deletions src/api/v1/endpoints/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from datetime import datetime
from src.repo.account import AccountRepo
from src.repo.audiobook import AudiobookRepo
from typing import Literal

router = APIRouter()

Expand Down Expand Up @@ -62,13 +61,22 @@ async def create_review(
updated_at = datetime.now()

if not await AccountRepo.exists(session, account_id=account_id):
raise HTTPException(status_code=404, detail=f'No account with ip {account_id} was found!')
raise HTTPException(
status_code=404, detail=f"No account with ip {account_id} was found!"
)

if not await AudiobookRepo.exists(session, audiobook_id=audiobook_id):
raise HTTPException(status_code=404, detail=f'No audiobook with ip {audiobook_id} was found!')
raise HTTPException(
status_code=404, detail=f"No audiobook with ip {audiobook_id} was found!"
)

if await ReviewRepo.exists(session, account_id=account_id, audiobook_id=audiobook_id):
raise HTTPException(status_code=409, detail=f'User with id {account_id} has already reviewed audiobook with id {audiobook_id}')
if await ReviewRepo.exists(
session, account_id=account_id, audiobook_id=audiobook_id
):
raise HTTPException(
status_code=409,
detail=f"User with id {account_id} has already reviewed audiobook with id {audiobook_id}",
)

review = await ReviewRepo.create(
session,
Expand All @@ -78,7 +86,7 @@ async def create_review(
rating_date=rating_date.replace(tzinfo=None),
review_content=review_content,
created_at=created_at,
updated_at=updated_at
updated_at=updated_at,
)

return review
6 changes: 1 addition & 5 deletions src/repo/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ class ReviewRepo(BaseRepo):
validation_schema = ReviewSchema

@classmethod
async def get_by_attribute(
cls,
session: AsyncSession,
**kwargs
):
async def get_by_attribute(cls, session: AsyncSession, **kwargs):
query = select(cls.model).filter_by(**kwargs)
result = await session.scalars(query)
if cls.validation_schema:
Expand Down
1 change: 0 additions & 1 deletion src/schemas/review.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from pydantic import ConfigDict
from datetime import datetime
from typing import Literal

from src.schemas.base import SchemaBase

Expand Down

0 comments on commit 8d07d35

Please sign in to comment.