add comic artists
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
from typing import List, AnyStr
|
||||
from typing import List
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
|
||||
from src.apis.utils import SessionDep
|
||||
from src.core.log_conf import logger
|
||||
from src.db.repository.comics.artist import get_artist_details
|
||||
from src.db.repository.comics.comic import list_comics, get_issue_details
|
||||
from src.schema.comics.comic import ComicResponse, ComicDetailsResponse, get_comic_details, get_short_info
|
||||
from src.schema.comics.artist import ArtistCreation, ArtistDetailResponse, ArtistResponse
|
||||
from src.db.repository.comics.comic import get_comic_details, get_short_info, list_comics, get_issue_details
|
||||
from src.schema.comics.artist_details import ArtistDetailResponse
|
||||
from src.schema.comics.comic import ComicResponse
|
||||
from src.schema.comics.artist import ArtistCreation, ArtistResponse
|
||||
from src.db.models.comic import Comic, Artist, Issue
|
||||
from src.schema.comics.comic_details import ComicDetailsResponse
|
||||
from src.schema.comics.issue import IssueDetailsResponse
|
||||
|
||||
router = APIRouter()
|
||||
@@ -23,7 +25,7 @@ def get_all_comics(db: SessionDep) -> List[ComicResponse]:
|
||||
return results
|
||||
|
||||
@router.get("/comics/{comic_id}", response_model=ComicDetailsResponse)
|
||||
def get_comic(comic_id: AnyStr, db: SessionDep) -> ComicDetailsResponse:
|
||||
def get_comic(comic_id: str, db: SessionDep) -> ComicDetailsResponse:
|
||||
comic = db.get(Comic, comic_id)
|
||||
if comic is None:
|
||||
raise HTTPException(status_code=404, detail="Comic could not be found")
|
||||
@@ -41,7 +43,7 @@ def get_all_artists(db: SessionDep) -> List[ArtistResponse]:
|
||||
return results
|
||||
|
||||
@router.get("/artists/{artist_id}", response_model=ArtistDetailResponse)
|
||||
def get_artist(artist_id: AnyStr, db: SessionDep) -> ArtistDetailResponse:
|
||||
def get_artist(artist_id: str, db: SessionDep) -> ArtistDetailResponse:
|
||||
artist = db.get(Artist, artist_id)
|
||||
if artist is None:
|
||||
raise HTTPException(status_code=404, detail="Artist could not be found")
|
||||
@@ -67,4 +69,3 @@ def get_issues(db: SessionDep) -> List[IssueDetailsResponse]:
|
||||
for issue in issues:
|
||||
results.append(get_issue_details(issue))
|
||||
return results
|
||||
|
||||
|
||||
@@ -1,19 +1,29 @@
|
||||
from typing import List
|
||||
from src.db.models.comic import Artist
|
||||
from src.schema.comics.artist import ArtistDetailResponse
|
||||
from src.schema.comics.artist_details import ArtistDetailResponse, ArtistWorktypeComicResponse
|
||||
from src.schema.comics.comic import ComicResponse
|
||||
from src.schema.comics.worktype import WorktypeResponse
|
||||
|
||||
|
||||
def get_artist_details(artist: Artist) -> ArtistDetailResponse:
|
||||
works = {}
|
||||
works: List[ArtistWorktypeComicResponse] = []
|
||||
works_map = {}
|
||||
for work in artist.comic_works:
|
||||
work_type = work.work_type.name
|
||||
comic_title = work.comic.title
|
||||
if work_type in works:
|
||||
works[work_type].append(comic_title)
|
||||
worktype_id = work.work_type.id
|
||||
if worktype_id in works_map:
|
||||
comic = ComicResponse(id=work.comic.id, title=work.comic.title, completed=work.comic.completed)
|
||||
works_map[worktype_id].comics.append(comic)
|
||||
else:
|
||||
works[work_type] = [comic_title]
|
||||
works_map[worktype_id] = ArtistWorktypeComicResponse(
|
||||
worktype=WorktypeResponse(id=worktype_id, name=work.work_type.name),
|
||||
comics=[ComicResponse(id=work.comic.id, title=work.comic.title, completed=work.comic.completed)]
|
||||
)
|
||||
for value in works_map.values():
|
||||
works.append(value)
|
||||
response = ArtistDetailResponse(
|
||||
id=artist.id,
|
||||
name=artist.name,
|
||||
weblink=artist.weblink,
|
||||
works=works
|
||||
)
|
||||
return response
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
from typing import List, Type, AnyStr
|
||||
from typing import Dict, List
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.core.log_conf import logger
|
||||
from src.db.models.comic import Comic, Issue
|
||||
from src.schema.comics.comic import ComicSchema
|
||||
from src.schema.comics.artist import ArtistResponse
|
||||
from src.schema.comics.comic import ComicResponse, ComicSchema
|
||||
from src.schema.comics.comic_details import ComicDetailsResponse, ComicWorktypeArtistResponse
|
||||
from src.schema.comics.issue import IssueDetailsResponse
|
||||
from src.schema.comics.volume import VolumeResponse
|
||||
from src.schema.comics.worktype import WorktypeResponse
|
||||
|
||||
|
||||
def list_comics(db: Session) -> List[Comic]:
|
||||
@@ -25,7 +29,48 @@ def get_issue_details(issue: Issue) -> IssueDetailsResponse:
|
||||
return response
|
||||
|
||||
|
||||
def update_comic(comic: ComicSchema, comic_id: AnyStr, db: Session) -> type[Comic] | None:
|
||||
def update_comic(comic: ComicSchema, comic_id: str, db: Session) -> type[Comic] | None:
|
||||
logger.info(f"update_comic: {comic} with {comic_id}")
|
||||
comic = db.get(Comic, comic_id) # type: ignore
|
||||
return comic # type: ignore
|
||||
|
||||
def get_short_info(comic: Comic) -> ComicResponse:
|
||||
response = ComicResponse(
|
||||
id=comic.id,
|
||||
title=str(comic.title),
|
||||
completed=bool(comic.completed == 1)
|
||||
)
|
||||
return response
|
||||
|
||||
def get_comic_details(comic: Comic) -> ComicDetailsResponse:
|
||||
volumes: List[VolumeResponse] = []
|
||||
for volume in comic.volumes:
|
||||
volumes.append(VolumeResponse(id=volume.id, name=volume.name))
|
||||
works: List[ComicWorktypeArtistResponse] = []
|
||||
works_map: Dict[str, ComicWorktypeArtistResponse] = {}
|
||||
for work in comic.comic_works:
|
||||
worktype_id = work.work_type.id
|
||||
if worktype_id in works_map:
|
||||
artist = ArtistResponse(id=work.artist.id, name=work.artist.name)
|
||||
works_map[worktype_id].artists.append(artist)
|
||||
logger.info(f"add artist to response map: {artist} -> {works_map}")
|
||||
print(f"add artist to response map: {artist} -> {works_map}")
|
||||
else:
|
||||
works_map[worktype_id] = ComicWorktypeArtistResponse(
|
||||
worktype=WorktypeResponse(id=worktype_id, name=work.work_type.name),
|
||||
artists=[ArtistResponse(id=work.artist.id, name=work.artist.name)]
|
||||
)
|
||||
for value in works_map.values():
|
||||
works.append(value)
|
||||
response = ComicDetailsResponse(
|
||||
id=comic.id,
|
||||
created=str(comic.created_date),
|
||||
title=str(comic.title),
|
||||
completed=bool(comic.completed),
|
||||
current_order=bool(comic.current_order),
|
||||
weblink=str(comic.weblink),
|
||||
publisher=comic.publisher.name,
|
||||
volumes=volumes,
|
||||
works=works
|
||||
)
|
||||
return response
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
from typing import List, Dict
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
|
||||
class ArtistCreation(BaseModel):
|
||||
id: str
|
||||
name: str
|
||||
@@ -11,9 +8,3 @@ class ArtistCreation(BaseModel):
|
||||
class ArtistResponse(BaseModel):
|
||||
id: str
|
||||
name: str
|
||||
|
||||
class ArtistDetailResponse(BaseModel):
|
||||
id: str
|
||||
name: str
|
||||
weblink: str
|
||||
works: Dict[str, List[str]]
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
from typing import List
|
||||
from pydantic import BaseModel
|
||||
|
||||
from src.schema.comics.comic import ComicResponse
|
||||
from src.schema.comics.worktype import WorktypeResponse
|
||||
|
||||
|
||||
class ArtistWorktypeComicResponse(BaseModel):
|
||||
worktype: WorktypeResponse
|
||||
comics: List[ComicResponse]
|
||||
|
||||
class ArtistDetailResponse(BaseModel):
|
||||
id: str
|
||||
name: str
|
||||
weblink: str
|
||||
works: List[ArtistWorktypeComicResponse]
|
||||
@@ -1,12 +1,6 @@
|
||||
from typing import List, Dict, Optional
|
||||
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, AnyUrl
|
||||
|
||||
from src.core.log_conf import logger
|
||||
from src.db.models.comic import Comic
|
||||
from src.schema.comics.artist import ArtistResponse
|
||||
from src.schema.comics.volume import VolumeResponse
|
||||
from src.schema.comics.worktype import WorktypeResponse
|
||||
|
||||
|
||||
class ComicResponse(BaseModel):
|
||||
@@ -15,70 +9,9 @@ class ComicResponse(BaseModel):
|
||||
completed: bool
|
||||
|
||||
|
||||
|
||||
class ComicWorktypeArtistResponse(BaseModel):
|
||||
worktype: WorktypeResponse
|
||||
artists: List[ArtistResponse]
|
||||
|
||||
|
||||
class ComicDetailsResponse(BaseModel):
|
||||
id: str
|
||||
created: str
|
||||
title: str
|
||||
completed : bool
|
||||
current_order : bool
|
||||
weblink: str
|
||||
publisher: str
|
||||
volumes: List[VolumeResponse]
|
||||
works: List[ComicWorktypeArtistResponse]
|
||||
|
||||
|
||||
class ComicSchema(BaseModel):
|
||||
id: str
|
||||
title: str
|
||||
weblink: Optional[AnyUrl]
|
||||
completed: Optional[bool]
|
||||
current_order: Optional[bool]
|
||||
|
||||
|
||||
def get_short_info(comic: Comic) -> ComicResponse:
|
||||
response = ComicResponse(
|
||||
id=comic.id,
|
||||
title=str(comic.title),
|
||||
completed=bool(comic.completed == 1)
|
||||
)
|
||||
return response
|
||||
|
||||
|
||||
def get_comic_details(comic: Comic) -> ComicDetailsResponse:
|
||||
volumes: List[VolumeResponse] = []
|
||||
for volume in comic.volumes:
|
||||
volumes.append(VolumeResponse(id=volume.id, name=volume.name))
|
||||
works: List[ComicWorktypeArtistResponse] = []
|
||||
works_map: Dict[str, ComicWorktypeArtistResponse] = {}
|
||||
for work in comic.comic_works:
|
||||
worktype_id = work.work_type.id
|
||||
if worktype_id in works_map:
|
||||
artist = ArtistResponse(id=work.artist.id, name=work.artist.name)
|
||||
works_map[worktype_id].artists.append(artist)
|
||||
logger.info(f"add artist to response map: {artist} -> {works_map}")
|
||||
print(f"add artist to response map: {artist} -> {works_map}")
|
||||
else:
|
||||
works_map[worktype_id] = ComicWorktypeArtistResponse(
|
||||
worktype=WorktypeResponse(id=worktype_id, name=work.work_type.name),
|
||||
artists=[ArtistResponse(id=work.artist.id, name=work.artist.name)]
|
||||
)
|
||||
for value in works_map.values():
|
||||
works.append(value)
|
||||
response = ComicDetailsResponse(
|
||||
id=comic.id,
|
||||
created=str(comic.created_date),
|
||||
title=str(comic.title),
|
||||
completed=bool(comic.completed),
|
||||
current_order=bool(comic.current_order),
|
||||
weblink=str(comic.weblink),
|
||||
publisher=comic.publisher.name,
|
||||
volumes=volumes,
|
||||
works=works
|
||||
)
|
||||
return response
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
from typing import List
|
||||
from pydantic import BaseModel
|
||||
|
||||
from src.schema.comics.artist import ArtistResponse
|
||||
from src.schema.comics.volume import VolumeResponse
|
||||
from src.schema.comics.worktype import WorktypeResponse
|
||||
|
||||
|
||||
class ComicWorktypeArtistResponse(BaseModel):
|
||||
worktype: WorktypeResponse
|
||||
artists: List[ArtistResponse]
|
||||
|
||||
|
||||
class ComicDetailsResponse(BaseModel):
|
||||
id: str
|
||||
created: str
|
||||
title: str
|
||||
completed : bool
|
||||
current_order : bool
|
||||
weblink: str
|
||||
publisher: str
|
||||
volumes: List[VolumeResponse]
|
||||
works: List[ComicWorktypeArtistResponse]
|
||||
Reference in New Issue
Block a user