mirror of
https://github.com/flibusta-apps/telegram_files_cache_server.git
synced 2025-12-06 14:45:36 +01:00
Make update if CachedFileExists
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException, status
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
|
|
||||||
|
from asyncpg import exceptions
|
||||||
|
|
||||||
from app.depends import check_token
|
from app.depends import check_token
|
||||||
from app.models import CachedFile as CachedFileDB
|
from app.models import CachedFile as CachedFileDB
|
||||||
from app.serializers import CachedFile, CreateCachedFile
|
from app.serializers import CachedFile, CreateCachedFile
|
||||||
@@ -41,8 +43,18 @@ async def delete_cached_file(object_id: int, object_type: str):
|
|||||||
|
|
||||||
|
|
||||||
@router.post("/", response_model=CachedFile)
|
@router.post("/", response_model=CachedFile)
|
||||||
async def create_cached_file(data: CreateCachedFile):
|
async def create_or_update_cached_file(data: CreateCachedFile):
|
||||||
|
try:
|
||||||
return await CachedFileDB.objects.create(**data.dict())
|
return await CachedFileDB.objects.create(**data.dict())
|
||||||
|
except exceptions.UniqueViolationError:
|
||||||
|
data_dict = data.dict()
|
||||||
|
object_id = data_dict.pop("object_id")
|
||||||
|
object_type = data_dict.pop("object_type")
|
||||||
|
cached_file = await CachedFileDB.objects.get(
|
||||||
|
object_id=object_id, object_type=object_type
|
||||||
|
)
|
||||||
|
cached_file.update_from_dict(data_dict)
|
||||||
|
return await cached_file.update()
|
||||||
|
|
||||||
|
|
||||||
@router.post("/update_cache")
|
@router.post("/update_cache")
|
||||||
|
|||||||
Reference in New Issue
Block a user