2 changed files with 77 additions and 17 deletions
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
#! /usr/bin/python3 |
||||
# -*- coding:utf-8 -*- |
||||
import tinydb as d |
||||
import os |
||||
from . import CURRENT_DIR |
||||
|
||||
db = d.TinyDB(os.path.join(CURRENT_DIR, "lottery.db")) |
||||
t_users = db.table('users') |
||||
t_awards = db.table('awards') |
||||
|
||||
|
||||
def server_resp(status, msg, data=None): |
||||
return {"status": status, "msg": msg, "data": data} |
||||
|
||||
|
||||
def reset(users_db, awards_db): |
||||
db.purge_tables() |
||||
for u in users_db: |
||||
t_users.insert(u) |
||||
for a in awards_db: |
||||
t_awards.insert(a) |
||||
return server_resp(200, "success") |
||||
|
||||
|
||||
def users(): |
||||
t_users.clear_cache() |
||||
return server_resp(200, "success", t_users.all()) |
||||
|
||||
|
||||
def luckless_users(): |
||||
t_users.clear_cache() |
||||
user = d.Query() |
||||
return server_resp(200, "success", t_users.search( |
||||
(~user.award_id.exists()) | (user.award_id is None))) |
||||
|
||||
|
||||
def lucky_users(): |
||||
t_users.clear_cache() |
||||
user = d.Query() |
||||
return server_resp(200, "success", t_users.search( |
||||
user.award_id is not None)) |
||||
|
||||
|
||||
def awards(): |
||||
t_awards.clear_cache() |
||||
return server_resp(200, "success", t_awards.all()) |
||||
|
||||
|
||||
def update(uid, award_id): |
||||
t_users.clear_cache() |
||||
t_awards.clear_cache() |
||||
if not t_awards.get(d.Query().award_id == award_id): |
||||
return server_resp(400, "不存在此奖品") |
||||
if len(t_users.update({'award_id': award_id}, d.Query().uid == uid)) == 0: |
||||
return server_resp(401, "不存在此用户") |
||||
else: |
||||
return server_resp(200, "success") |
||||
Loading…
Reference in new issue