def insert_note(self, user_id, title="", note_type="", content=""): db = Database() title = escape(title) note_type = escape(note_type) content = escape(content) try: created_at = datetime.datetime.utcnow() note = { 'user_id': user_id, 'created_at': created_at, 'updated_at': created_at, 'title': title, 'type': note_type, 'content': content } res = db.connection['notes'].insert_one(note) res_user = db.connection['users'].update({'_id': user_id}, {'$inc': { 'score': 3 }}) return res.inserted_id except Exception as e: return str(e)
def get_points(self, _id=None): db = Database() try: user = db.connection['users'].find_one( { "_id": _id } ) return user['score'] except Exception as e: return str(e)
def find_by_email(self, email=None): db = Database() try: user = db.connection['users'].find_one( { "email": email } ) return user except Exception as e: return str(e)
def find_by_id(self, _id=None): db = Database() try: user = db.connection['users'].find_one( { "_id": _id } ) return user except Exception as e: return str(e)
def find_by_username(self, username=""): db = Database() try: user = db.connection['users'].find_one( { "username": username } ) return user except Exception as e: return str(e)
def find_all_users(self): db = Database() try: users = db.connection['users'].find() return users except Exception as e: return str(e)
def find_by_user_id(self, user_id=None): db = Database() try: res = db.connection['notes'].find({"user_id": user_id}) return res except Exception as e: return str(e)
def find_all_notes(self): db = Database() try: res = db.connection['notes'].find() return res except Exception as e: return str(e)
def delete_all_user_notes(self, user_id): db = Database() try: res = db.connection['notes'].delete_many({'user_id': user_id}) return res.deleted_count except Exception as e: return str(e)
def delete_user(self, _id): db = Database() try: res = db.connection['users'].delete_one({ '_id': _id }) return res['nModified'] except Exception as e: return str(e)
def belongs_to_user(self, _id, user_id): db = Database() try: res = db.connection['notes'].find_one({ "_id": _id, "user_id": user_id }) return res except Exception as e: return str(e)
def delete_note(self, _id, user_id): db = Database() try: res = db.connection['notes'].delete_one({ '_id': _id, 'user_id': user_id }) res_user = db.connection['users'].update({'_id': user_id}, {'$inc': { 'score': 1 }}) return res.deleted_count except Exception as e: return str(e)
def update_user(self, _id, email): db = Database() try: res = db.connection['users'].update( {'_id': _id}, { '$set': { "email": escape(email), "updated_at": datetime.datetime.utcnow() } } ) return res['nModified'] except Exception as e: return str(e)
def update_note(self, user_id, _id, title="", content="", note_type=""): db = Database() try: res = db.connection['notes'].update({'_id': _id}, { '$set': { "title": escape(title), "content": escape(content), "type": escape(note_type), "updated_at": datetime.datetime.utcnow() }, }) res_user = db.connection['users'].update({'_id': user_id}, {'$inc': { 'score': 1 }}) return res['nModified'] except Exception as e: return str(e)
def insert_user(self, hashing, username, password): db = Database() username = escape(username) hashed_password = self.encrypt_password(hashing, password) try: created_at = datetime.datetime.utcnow() user = { 'username': username, 'hashed_password': hashed_password, 'created_at': created_at, 'updated_at': created_at, 'score': 1 } db.connection['users'].insert_one(user) return True except Exception as e: return str(e)