mirror of
https://github.com/StepanovPlaton/torrent_backend.git
synced 2026-04-03 12:20:38 +04:00
Add audiobooks
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
from .games import games_router as games_router
|
||||
from .movies import movies_router as movies_router
|
||||
from .audiobooks import audiobooks_router as audiobooks_router
|
||||
from .files import files_router as files_router
|
||||
from .startup import startup_router as startup_router
|
||||
from .auth import auth_router as auth_router
|
||||
|
||||
61
routes/audiobooks.py
Normal file
61
routes/audiobooks.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
|
||||
import database as db
|
||||
from file_handler import *
|
||||
from routes.auth import get_user
|
||||
|
||||
audiobooks_router = APIRouter(prefix="/audiobooks", tags=["Audiobooks"])
|
||||
|
||||
|
||||
@audiobooks_router.get("", response_model=list[db.Audiobook])
|
||||
async def get_audiobooks(db_session: AsyncSession = Depends(db.get_session)):
|
||||
return await db.get_audiobooks(db_session)
|
||||
|
||||
|
||||
@audiobooks_router.post("", response_model=db.Audiobook)
|
||||
async def add_audiobook(audiobook: db.AudiobookCreate,
|
||||
user: db.User = Depends(get_user),
|
||||
db_session: AsyncSession = Depends(db.get_session)):
|
||||
return await db.add_audiobook(db_session, audiobook, user.id)
|
||||
|
||||
|
||||
@audiobooks_router.get("/cards", response_model=list[db.AudiobookCard])
|
||||
async def get_audiobooks_cards(db_session: AsyncSession = Depends(db.get_session)):
|
||||
return await db.get_audiobooks(db_session)
|
||||
|
||||
|
||||
@audiobooks_router.get("/{audiobook_id}", response_model=db.Audiobook)
|
||||
async def get_audiobook(audiobook_id: int, db_session: AsyncSession = Depends(db.get_session)):
|
||||
return await db.get_audiobook(db_session, audiobook_id)
|
||||
|
||||
|
||||
@audiobooks_router.put("/{audiobook_id}", response_model=db.Audiobook)
|
||||
async def edit_audiobook(audiobook_id: int,
|
||||
audiobook: db.AudiobookCreate,
|
||||
user: db.User = Depends(get_user),
|
||||
db_session: AsyncSession = Depends(db.get_session)):
|
||||
audiobook_db = await db.get_audiobook(db_session, audiobook_id)
|
||||
if (audiobook_db is None):
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Audiobook with id={audiobook_id} not found")
|
||||
if (user.id != audiobook_db.owner_id):
|
||||
raise HTTPException(status.HTTP_401_UNAUTHORIZED,
|
||||
detail=f"Audiobook can only be edited "
|
||||
"by the owner (creator)")
|
||||
return await db.edit_audiobook(db_session, audiobook_id, audiobook)
|
||||
|
||||
|
||||
@audiobooks_router.delete("/{audiobook_id}", response_model=db.Audiobook)
|
||||
async def delete_audiobook(audiobook_id: int,
|
||||
user: db.User = Depends(get_user),
|
||||
db_session: AsyncSession = Depends(db.get_session)):
|
||||
audiobook_db = await db.get_audiobook(db_session, audiobook_id)
|
||||
if (audiobook_db is None):
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Audiobook with id={audiobook_id} not found")
|
||||
if (user.id != audiobook_db.owner_id):
|
||||
raise HTTPException(status.HTTP_401_UNAUTHORIZED,
|
||||
detail=f"Audiobook can only be deleted "
|
||||
"by the owner (creator)")
|
||||
return await db.delete_audiobook(db_session, audiobook_id)
|
||||
@@ -12,7 +12,7 @@ async def upload_torrent(torrent: UploadFile):
|
||||
return await save_torrent_file(torrent)
|
||||
except Exception as ex:
|
||||
print(ex)
|
||||
raise HTTPException(500)
|
||||
raise HTTPException(500, detail=str(ex))
|
||||
|
||||
|
||||
@files_router.post("/cover", response_model=str)
|
||||
@@ -21,4 +21,13 @@ async def upload_cover(cover: UploadFile):
|
||||
return await save_image(cover, "cover")
|
||||
except Exception as ex:
|
||||
print(ex)
|
||||
raise HTTPException(500)
|
||||
raise HTTPException(500, detail=str(ex))
|
||||
|
||||
|
||||
@files_router.post("/audio", response_model=str)
|
||||
async def upload_audio_fragment(fragment: UploadFile):
|
||||
try:
|
||||
return await save_audio_fragment(fragment)
|
||||
except Exception as ex:
|
||||
print(ex)
|
||||
raise HTTPException(500, detail=str(ex))
|
||||
|
||||
@@ -51,7 +51,6 @@ async def delete_game(game_id: int,
|
||||
user: db.User = Depends(get_user),
|
||||
db_session: AsyncSession = Depends(db.get_session)):
|
||||
game_db = await db.get_game(db_session, game_id)
|
||||
print(game_db)
|
||||
if (game_db is None):
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Game with id={game_id} not found")
|
||||
|
||||
61
routes/movies.py
Normal file
61
routes/movies.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
|
||||
import database as db
|
||||
from file_handler import *
|
||||
from routes.auth import get_user
|
||||
|
||||
movies_router = APIRouter(prefix="/movies", tags=["Movies"])
|
||||
|
||||
|
||||
@movies_router.get("", response_model=list[db.Movie])
|
||||
async def get_movies(db_session: AsyncSession = Depends(db.get_session)):
|
||||
return await db.get_movies(db_session)
|
||||
|
||||
|
||||
@movies_router.post("", response_model=db.Movie)
|
||||
async def add_movie(movie: db.MovieCreate,
|
||||
user: db.User = Depends(get_user),
|
||||
db_session: AsyncSession = Depends(db.get_session)):
|
||||
return await db.add_movie(db_session, movie, user.id)
|
||||
|
||||
|
||||
@movies_router.get("/cards", response_model=list[db.MovieCard])
|
||||
async def get_movies_cards(db_session: AsyncSession = Depends(db.get_session)):
|
||||
return await db.get_movies(db_session)
|
||||
|
||||
|
||||
@movies_router.get("/{movie_id}", response_model=db.Movie)
|
||||
async def get_movie(movie_id: int, db_session: AsyncSession = Depends(db.get_session)):
|
||||
return await db.get_movie(db_session, movie_id)
|
||||
|
||||
|
||||
@movies_router.put("/{movie_id}", response_model=db.Movie)
|
||||
async def edit_movie(movie_id: int,
|
||||
movie: db.MovieCreate,
|
||||
user: db.User = Depends(get_user),
|
||||
db_session: AsyncSession = Depends(db.get_session)):
|
||||
movie_db = await db.get_movie(db_session, movie_id)
|
||||
if (movie_db is None):
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Movie with id={movie_id} not found")
|
||||
if (user.id != movie_db.owner_id):
|
||||
raise HTTPException(status.HTTP_401_UNAUTHORIZED,
|
||||
detail=f"Movie can only be edited "
|
||||
"by the owner (creator)")
|
||||
return await db.edit_movie(db_session, movie_id, movie)
|
||||
|
||||
|
||||
@movies_router.delete("/{movie_id}", response_model=db.Movie)
|
||||
async def delete_movie(movie_id: int,
|
||||
user: db.User = Depends(get_user),
|
||||
db_session: AsyncSession = Depends(db.get_session)):
|
||||
movie_db = await db.get_movie(db_session, movie_id)
|
||||
if (movie_db is None):
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Movie with id={movie_id} not found")
|
||||
if (user.id != movie_db.owner_id):
|
||||
raise HTTPException(status.HTTP_401_UNAUTHORIZED,
|
||||
detail=f"Movie can only be deleted "
|
||||
"by the owner (creator)")
|
||||
return await db.delete_movie(db_session, movie_id)
|
||||
@@ -12,7 +12,8 @@ def create_folders():
|
||||
Path() / "content" / "images" / "cover" / "preview",
|
||||
Path() / "content" / "images" / "screenshot" / "full_size",
|
||||
Path() / "content" / "images" / "screenshot" / "preview",
|
||||
Path() / "content" / "torrent"
|
||||
Path() / "content" / "torrent",
|
||||
Path() / "content" / "audio"
|
||||
]
|
||||
for path in need_paths:
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
Reference in New Issue
Block a user