-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from christopherfrige/feat/managing-new-manwhas…
…-to-scrape Feat: improve manwha chapters management + global exception handlers
- Loading branch information
Showing
21 changed files
with
188 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class DefaultException(Exception): | ||
def __init__(self, message): | ||
self.message = message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from src.domain.exceptions import DefaultException | ||
|
||
|
||
class BadRequestException(DefaultException): | ||
... | ||
|
||
|
||
class ConflictException(DefaultException): | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
from src.domain.repository import BaseRepository | ||
from src.domain.entities.scraper import ScraperManwha | ||
from src.domain.entities.scraper import ScraperManwha, Reader | ||
from sqlalchemy.orm import Session | ||
|
||
|
||
class ScraperManwhaRepository(BaseRepository): | ||
def __init__(self, session: Session) -> None: | ||
super().__init__(session, ScraperManwha) | ||
|
||
|
||
class ReaderRepository(BaseRepository): | ||
def __init__(self, session: Session) -> None: | ||
super().__init__(session, Reader) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from pydantic import BaseModel | ||
|
||
from src.domain.schemas import Pagination | ||
|
||
|
||
class ReaderData(BaseModel): | ||
id: int | ||
name: str | ||
|
||
|
||
class GetReadersResponse(BaseModel): | ||
records: list[ReaderData] | ||
pagination: Pagination |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
backend/src/domain/use_cases/manwha/create_manwha_to_scrape.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from src.infrastructure.persistence.unit_of_work import UnitOfWork | ||
from src.domain.schemas.manwha import CreateManwhaToScrapeRequest | ||
from src.domain.repository.scraper import ReaderRepository, ScraperManwhaRepository | ||
from src.domain.entities.scraper import ScraperManwha | ||
from src.domain.exceptions.client import BadRequestException, ConflictException | ||
|
||
|
||
class CreateManwhaToScrapeUseCase: | ||
def __init__(self, db: UnitOfWork) -> None: | ||
with db.get_session() as session: | ||
self.session = session | ||
self.reader_repository = ReaderRepository(session) | ||
self.scraper_manwha_repository = ScraperManwhaRepository(session) | ||
|
||
def execute(self, payload: CreateManwhaToScrapeRequest): | ||
reader = self.reader_repository.get("id", payload.reader_id) | ||
if not reader: | ||
raise BadRequestException("The provided reader_id was not found in database") | ||
|
||
manwha = self.scraper_manwha_repository.get("url", payload.url) | ||
if manwha: | ||
raise ConflictException("A manwha is already registered with the provided url") | ||
|
||
self.scraper_manwha_repository.add( | ||
ScraperManwha(reader_id=payload.reader_id, url=payload.url) | ||
) | ||
|
||
self.session.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from src.domain.schemas.scraper import GetReadersResponse, ReaderData | ||
from src.domain.repository.scraper import ReaderRepository | ||
from src.domain.use_cases.pagination.prepare_pagination import PreparePaginationUseCase | ||
from src.infrastructure.persistence.unit_of_work import UnitOfWork | ||
|
||
|
||
class GetReadersUseCase: | ||
def __init__(self, db: UnitOfWork): | ||
with db.get_session() as session: | ||
self.session = session | ||
self.reader_repository = ReaderRepository(session) | ||
self.prepare_pagination = PreparePaginationUseCase().execute | ||
|
||
def execute(self, page, per_page) -> GetReadersResponse: | ||
readers = self.reader_repository.get_all() | ||
|
||
records = [ReaderData(id=reader.id, name=reader.name) for reader in readers] | ||
|
||
return GetReadersResponse( | ||
records=records, | ||
pagination=self.prepare_pagination("/v1/readers", readers, page, per_page), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from fastapi import Request | ||
from fastapi.responses import JSONResponse | ||
|
||
from src.domain.exceptions.client import BadRequestException, ConflictException | ||
from src.infrastructure.log import logger | ||
|
||
|
||
async def bad_request_exception_handler(request: Request, exc: BadRequestException): | ||
return JSONResponse( | ||
status_code=400, | ||
content={"status": 400, "message": exc.message}, | ||
) | ||
|
||
|
||
async def conflict_exception_handler(request: Request, exc: ConflictException): | ||
return JSONResponse( | ||
status_code=409, | ||
content={"status": 409, "message": exc.message}, | ||
) | ||
|
||
|
||
async def general_exception_handler(request: Request, exc: Exception): | ||
logger.error(f"{exc.__class__.__name__}: {exc}") | ||
return JSONResponse( | ||
status_code=500, | ||
content={"status": 500, "message": "Internal Server Error"}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from fastapi import APIRouter, Depends | ||
|
||
from src.domain.schemas.scraper import GetReadersResponse | ||
from src.domain.use_cases.scraper.get_readers import GetReadersUseCase | ||
from src.infrastructure.persistence.unit_of_work import UnitOfWork | ||
|
||
router = APIRouter(prefix="/api/v1/readers", tags=["v1"]) | ||
|
||
|
||
@router.get("/", response_model=GetReadersResponse, status_code=200) | ||
def get_readers(page: int = 1, per_page: int = 20, db=Depends(UnitOfWork)): | ||
return GetReadersUseCase(db).execute(page, per_page) |