Skip to content

Commit

Permalink
[fix] ランキングの修正
Browse files Browse the repository at this point in the history
  • Loading branch information
PigeonsHouse committed Mar 15, 2022
1 parent 74a11b3 commit d04daf6
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions cruds/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime
from dateutil.relativedelta import relativedelta
from fastapi import HTTPException
from sqlalchemy import func
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
Expand Down Expand Up @@ -68,11 +68,13 @@ def get_ranking(db: Session, type: str) -> List[RankingUser]:
now = datetime.now()

if type == 'quantity':
q = db.query(models.ReadPage.user_id, models.User.name, func.sum(models.ReadPage.quantity))
q = db.query(models.User.user_id, models.User.name, func.sum(models.ReadPage.quantity).label('val'))
if type == 'page':
q = db.query(models.ReadPage.user_id, models.User.name, func.sum(models.ReadPage.pages))
q = db.query(models.User.user_id, models.User.name, func.sum(models.ReadPage.pages).label('val'))

q = q.filter(models.ReadPage.year == now.year, models.ReadPage.month == now.month).group_by(models.ReadPage.user_id, models.User.name).limit(5)
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:
Expand Down

0 comments on commit d04daf6

Please sign in to comment.