-
Notifications
You must be signed in to change notification settings - Fork 116
/
database.py
33 lines (29 loc) · 1.03 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import datetime
import os
import motor.motor_asyncio # pylint: disable=import-error
from config import Config
DB_NAME = os.environ.get("DB_NAME", "NAZRIYASONG")
class Database:
def __init__(self):
self._client = motor.motor_asyncio.AsyncIOMotorClient(Config.DATABASE)
self.db = self._client[DB_NAME]
self.dcol = self.db.users
def new_user(self, id):
return dict(
id = id,
join_date = datetime.date.today().isoformat()
)
async def add_user(self, id):
user = self.new_user(id)
await self.dcol.insert_one(user)
async def is_user_exist(self, id):
user = await self.dcol.find_one({'id':int(id)})
return True if user else False
async def total_users_count(self):
count = await self.dcol.count_documents({})
return count
async def get_all_users(self):
all_users = self.dcol.find({})
return all_users
async def delete_user(self, user_id):
await self.dcol.delete_many({'id': int(user_id)})