Skip to content

Commit

Permalink
feat: random pic api support uid
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamhunter2333 committed Jul 22, 2024
1 parent ac5eab6 commit 9653e1e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/awsl_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def awsl_list_count(uid: Optional[str] = "") -> int:


@router.get("/v2/random", response_model=str, tags=["AwslV2"])
def awsl_random() -> str:
return DBClientBase.get_client().awsl_random()
def awsl_random(uid: Optional[str] = "") -> str:
return DBClientBase.get_client().awsl_random(uid)


@router.get("/v2/random_json", response_model=BlobItem, tags=["AwslV2"])
def awsl_random_json() -> str:
return DBClientBase.get_client().awsl_random_json()
def awsl_random_json(uid: Optional[str] = "") -> str:
return DBClientBase.get_client().awsl_random_json(uid)
8 changes: 4 additions & 4 deletions src/awsl_pic.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def awsl_pic_list_count(uid: Optional[str] = "") -> int:


@router.get("/awsl_pic/random", response_model=str, tags=["Awsl Pic"])
def awsl_pic_random() -> str:
return DBClientBase.get_client().awsl_pic_random()
def awsl_pic_random(uid: Optional[str] = "") -> str:
return DBClientBase.get_client().awsl_pic_random(uid)


@router.get("/awsl_pic/random_json", response_model=BlobItem, tags=["Awsl Pic"])
def awsl_pic_random_json() -> str:
return DBClientBase.get_client().awsl_pic_random_json()
def awsl_pic_random_json(uid: Optional[str] = "") -> str:
return DBClientBase.get_client().awsl_pic_random_json(uid)
8 changes: 4 additions & 4 deletions src/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ def awsl_list_count(cls, uid: str) -> int:
...

@classmethod
def awsl_random(cls) -> str:
def awsl_random(cls, uid: str) -> str:
...

@classmethod
def awsl_random_json(cls) -> str:
def awsl_random_json(cls, uid: str) -> str:
...

@classmethod
Expand All @@ -73,9 +73,9 @@ def awsl_pic_list_count(cls, uid: str) -> int:
...

@classmethod
def awsl_pic_random(cls) -> str:
def awsl_pic_random(cls, uid: str) -> str:
...

@classmethod
def awsl_pic_random_json(cls) -> str:
def awsl_pic_random_json(cls, uid: str) -> str:
...
36 changes: 28 additions & 8 deletions src/db/mysql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,26 @@ def awsl_list_count(cls, uid: str) -> int:
return int(res[0]) if res else 0

@classmethod
def awsl_random(cls) -> str:
def awsl_random(cls, uid: str) -> str:
with cls.DBSession() as session:
blob = session.query(AwslBlob).order_by(
blob = session.query(AwslBlob).join(Mblog, AwslBlob.awsl_id == Mblog.id).filter(
Mblog.uid == uid
).order_by(
func.rand()
).limit(1).one() if uid else session.query(AwslBlob).order_by(
func.rand()
).limit(1).one()
url_dict = Blobs.model_validate_json(blob.pic_info).blobs
return url_dict["original"].url

@classmethod
def awsl_random_json(cls) -> str:
def awsl_random_json(cls, uid: str) -> str:
with cls.DBSession() as session:
blob = session.query(AwslBlob).order_by(
blob = session.query(AwslBlob).join(Mblog, AwslBlob.awsl_id == Mblog.id).filter(
Mblog.uid == uid
).order_by(
func.rand()
).limit(1).one() if uid else session.query(AwslBlob).order_by(
func.rand()
).limit(1).one()
return BlobItem(
Expand Down Expand Up @@ -206,18 +214,30 @@ def awsl_pic_list_count(cls, uid: str) -> int:
return int(res[0]) if res else 0

@classmethod
def awsl_pic_random(cls) -> str:
def awsl_pic_random(cls, uid: str) -> str:
with cls.DBSession() as session:
pic = session.query(Pic).order_by(
pic = session.query(Pic).join(
Mblog, Pic.awsl_id == Mblog.id
).filter(
Mblog.uid == uid
).order_by(
func.rand()
).limit(1).one() if uid else session.query(Pic).order_by(
func.rand()
).limit(1).one()
url_dict = PicInfo.model_validate_json(pic.pic_info).root
return url_dict["original"].url

@classmethod
def awsl_pic_random_json(cls) -> str:
def awsl_pic_random_json(cls, uid: str) -> str:
with cls.DBSession() as session:
blob = session.query(Pic).order_by(
blob = session.query(Pic).join(
Mblog, Pic.awsl_id == Mblog.id
).filter(
Mblog.uid == uid
).order_by(
func.rand()
).limit(1).one() if uid else session.query(Pic).order_by(
func.rand()
).limit(1).one()
return BlobItem(
Expand Down
56 changes: 38 additions & 18 deletions src/db/tidb_http_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,28 @@ def awsl_list(cls, uid: str, limit: int, offset: int) -> List[BlobItem]:
@classmethod
def awsl_list_count(cls, uid: str) -> int:
res = cls.post_query(
"SELECT count(1) AS count FROM awsl_blob"
"SELECT count(*) AS count FROM awsl_blob"
+ (
" INNER JOIN awsl_mblog ON awsl_blob.awsl_id=awsl_mblog.id"
f" WHERE awsl_mblog.uid = '{sql_escape(uid)}'"
) if uid else ""
+ f" WHERE awsl_mblog.uid = '{sql_escape(uid)}'"
if uid else ""
)
)
if not res or not res[0] or not res[0].get("count"):
return 0
return res[0]["count"]

@classmethod
def awsl_random(cls) -> str:
res = cls.post_query("SELECT pic_info FROM awsl_blob ORDER BY rand() LIMIT 1")
def awsl_random(cls, uid: str) -> str:
res = cls.post_query(
"SELECT pic_info FROM awsl_blob "
+ (
" INNER JOIN awsl_mblog ON awsl_blob.awsl_id=awsl_mblog.id"
f" WHERE awsl_mblog.uid = '{sql_escape(uid)}' "
if uid else ""
)
+ " ORDER BY rand() LIMIT 1"
)
if not res or not res[0]:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
Expand All @@ -187,13 +196,14 @@ def awsl_random(cls) -> str:
return url_dict["original"].url

@classmethod
def awsl_random_json(cls) -> str:
def awsl_random_json(cls, uid: str) -> str:
blobs_json = cls.post_query(
"SELECT awsl_blob.pic_id, awsl_blob.pic_info,"
" awsl_mblog.re_user_id, awsl_mblog.re_mblogid"
" FROM awsl_blob"
" INNER JOIN awsl_mblog ON awsl_blob.awsl_id=awsl_mblog.id"
" ORDER BY rand() LIMIT 1"
+ " awsl_mblog.re_user_id, awsl_mblog.re_mblogid"
+ " FROM awsl_blob"
+ " INNER JOIN awsl_mblog ON awsl_blob.awsl_id=awsl_mblog.id"
+ (f" WHERE awsl_mblog.uid = '{sql_escape(uid)}'" if uid else "")
+ " ORDER BY rand() LIMIT 1"
)
if not blobs_json:
raise HTTPException(
Expand Down Expand Up @@ -238,15 +248,24 @@ def awsl_pic_list_count(cls, uid: str) -> int:
+ (
" INNER JOIN awsl_mblog ON awsl_pic.awsl_id=awsl_mblog.id"
f" WHERE awsl_mblog.uid = '{sql_escape(uid)}'"
) if uid else ""
if uid else ""
)
)
if not res or not res[0] or not res[0].get("count"):
return 0
return res[0]["count"]

@classmethod
def awsl_pic_random(cls) -> str:
res = cls.post_query("SELECT pic_info FROM awsl_pic ORDER BY rand() LIMIT 1")
def awsl_pic_random(cls, uid: str) -> str:
res = cls.post_query(
"SELECT pic_info FROM awsl_pic"
+ (
" INNER JOIN awsl_mblog ON awsl_pic.awsl_id=awsl_mblog.id"
f" WHERE awsl_mblog.uid = '{sql_escape(uid)}'"
if uid else ""
)
+ " ORDER BY rand() LIMIT 1"
)
if not res or not res[0]:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
Expand All @@ -256,13 +275,14 @@ def awsl_pic_random(cls) -> str:
return url_dict["original"].url

@classmethod
def awsl_pic_random_json(cls) -> str:
def awsl_pic_random_json(cls, uid: str) -> str:
res_json = cls.post_query(
"SELECT awsl_pic.pic_id, awsl_pic.pic_info,"
" awsl_mblog.re_user_id, awsl_mblog.re_mblogid"
" FROM awsl_pic"
" INNER JOIN awsl_mblog ON awsl_pic.awsl_id=awsl_mblog.id"
" ORDER BY rand() LIMIT 1"
+ " awsl_mblog.re_user_id, awsl_mblog.re_mblogid"
+ " FROM awsl_pic"
+ " INNER JOIN awsl_mblog ON awsl_pic.awsl_id=awsl_mblog.id"
+ (f" WHERE awsl_mblog.uid = '{sql_escape(uid)}'" if uid else "")
+ " ORDER BY rand() LIMIT 1"
)
if not res_json:
raise HTTPException(
Expand Down

0 comments on commit 9653e1e

Please sign in to comment.