xbackend/crud/user_label.py

36 lines
1.2 KiB
Python

from motor.motor_asyncio import AsyncIOMotorDatabase
import time, random
import schemas
from crud.base import CRUDBase
__all__ = 'user_label',
def get_uid():
return hex(int(time.time() * 10 ** 7) + random.randint(0, 10000))[2:]
class CRUDUserLabel(CRUDBase):
async def save(self, db: AsyncIOMotorDatabase, data_in: schemas.UserLabelSave, act_name, game):
where = {'cluster_name': data_in.cluster_name, 'game': game}
is_exists = await self.find_one(db, where)
data = data_in.dict(skip_defaults=True)
data['act_name'] = act_name
if not is_exists:
data = {'$set': {**data, '_id': get_uid()}}
return await self.update_one(db, where, data, upsert=True)
return await self.update_one(db, where, {'$set': data}, upsert=True)
async def read(self, db: AsyncIOMotorDatabase, data_in: schemas.UserLabelRead):
where = data_in.dict(skip_defaults=True)
res = await self.find_many(db, where)
return res
async def get_list(self, db: AsyncIOMotorDatabase, game: str):
where = {'game': game}
res = await self.find_many(db, where, {'qp': 0})
return res
user_label = CRUDUserLabel('user_label')