MissKatyPyro/database/notes_db.py
2022-12-08 11:56:03 +07:00

50 lines
1.2 KiB
Python

from database import dbname
from typing import Dict, Union, List
notesdb = dbname.notes
async def _get_notes(chat_id: int) -> Dict[str, int]:
_notes = await notesdb.find_one({"chat_id": chat_id})
if not _notes:
return {}
return _notes["notes"]
async def delete_note(chat_id: int, name: str) -> bool:
notesd = await _get_notes(chat_id)
name = name.lower().strip()
if name in notesd:
del notesd[name]
await notesdb.update_one(
{"chat_id": chat_id},
{"$set": {"notes": notesd}},
upsert=True,
)
return True
return False
async def get_note(chat_id: int, name: str) -> Union[bool, dict]:
name = name.lower().strip()
_notes = await _get_notes(chat_id)
if name in _notes:
return _notes[name]
return False
async def get_note_names(chat_id: int) -> List[str]:
_notes = []
for note in await _get_notes(chat_id):
_notes.append(note)
return _notes
async def save_note(chat_id: int, name: str, note: dict):
name = name.lower().strip()
_notes = await _get_notes(chat_id)
_notes[name] = note
await notesdb.update_one(
{"chat_id": chat_id}, {"$set": {"notes": _notes}}, upsert=True
)