import mlab
from post import Post

#1. Connect
mlab.connect()

# #2. Creat data
# p = Post(title="C4E21", author="Quan", content="Sap den project roi", likes=15)
# print(p.title)
# print(p.content)
# print(p.likes)
# print(p.author)

# #3. Write data
# p.save()

# #find mongoengine

def test_load_data():
    #2. Load all documents
    all_posts = Post.objects()
    
    #3. Print all document
    for post in all_posts:
        print(post.title)
        print(post.content)
        print(post.author)
        print("----------------------")

def test_load_one_data(post_id):
    post = Post.objects().with_id(post_id)
    if post == None:
        print("Not found")
    else:
        print(post.title)
        print(post.author)
        print(post.content)

def delete_one_data(post_id):
    #1. Retrive document
    post = Post.objects().with_id(post_id)

    #2. Delete document
    if post is None:
        print("Post not found")
    else:
        post.delete()

def update_one(post_id, new_title):
    #1. Retrive post
    post = Post.objects().with_id(post_id)

    #2. Update
    # Slug
    post.update(set__title=new_title)

update_one("5b9cd3dfc608e30fbcdad656", "Di xe may")