Skip to content

Commit

Permalink
[add] 総読破量,総読破ページ数のエンドポイント作成
Browse files Browse the repository at this point in the history
  • Loading branch information
PigeonsHouse committed Mar 15, 2022
1 parent d04daf6 commit add726a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
12 changes: 9 additions & 3 deletions cruds/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy import desc, func
from db import models
from sqlalchemy.orm.session import Session
from schemas.api import RankingUser, Book, Report, User as UserSchema
from schemas.api import RankingUser, Book, Report, TotalInfo, User as UserSchema

def get_books_order_by_query(db: Session, user_id: str, order_by: str) -> List[Book]:

Expand Down Expand Up @@ -74,8 +74,6 @@ def get_ranking(db: Session, type: str) -> List[RankingUser]:

q = q.filter(models.ReadPage.year == now.year, models.ReadPage.month == now.month, models.User.user_id == models.ReadPage.user_id).group_by(models.User.user_id, models.User.name).order_by(desc('val'))

print(q)

ranking = []
for uid, name, val in q:
ranking.append(RankingUser(
Expand All @@ -85,3 +83,11 @@ def get_ranking(db: Session, type: str) -> List[RankingUser]:
))

return ranking

def get_total_info(db: Session, user_id: str) -> TotalInfo:
quantity, page = db.query(func.count(models.Book.isbn), func.sum(models.Book.page)).filter(models.Book.isbn == models.Read.isbn, models.Read.user_id == user_id).first()
return TotalInfo(
user_id = user_id,
quantity = quantity,
page = page
)
9 changes: 7 additions & 2 deletions routers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from fastapi import APIRouter
from fastapi.params import Depends
from db.main import get_db
from cruds.api import create_report, get_ranking, get_books_order_by_query
from schemas.api import Book, RankingUser, Report
from cruds.api import create_report, get_ranking, get_books_order_by_query, get_total_info
from schemas.api import Book, RankingUser, Report, TotalInfo
from fastapi_pagination import paginate
from fastapi_pagination import Page, paginate

Expand All @@ -21,6 +21,11 @@ class Type(str, Enum):

router = APIRouter()

@router.get('/api/books/total/{user_id}', response_model=TotalInfo)
async def get_total(user_id: str, db: Session = Depends(get_db)):
total_info = get_total_info(db, user_id)
return total_info

@router.get('/api/books/{user_id}', response_model=Page[Book])
async def books(user_id: str, order_by: ModelName, db: Session = Depends(get_db)):
books = get_books_order_by_query(db, user_id, order_by.value)
Expand Down
6 changes: 6 additions & 0 deletions schemas/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ class RankingUser(BaseModel):
class Report(BaseModel):
months: List[int]
data: List[int]

class TotalInfo(BaseModel):
user_id: str
quantity: int
page: int

0 comments on commit add726a

Please sign in to comment.