from flask import Blueprint, jsonify, request
from database.db import connect, r
from time import time
import os

api = Blueprint("api", __name__, static_folder=None, template_folder=None)

class ERRORS():
    """400 Errors"""
    BAD_CREDS = ('{"error":"Bad credentials"}', 400)
    BAD_REQ = ('{"error":"Bad request"})', 400)
    BAD_AUTH = ('{"error":"Bad authentication"}', 400)

#API TEST
@api.route("/test", methods=["GET"])
def test():
    return jsonify({ "test":"yes" }), 200

#Post Creation
# Requires the following arguments:
#   title
#   description
@api.route("/make-post", methods=["POST"])
def make_post(req=None):
    if(req is None):
        req = request.json
    
    with connect() as cn:
        posts = r.table("posts")
        
        inserted = posts.insert({
            "title":req["title"],
            "description":req["description"],
            "score":1,
            "timeCreated":time(),
            "comments":None,
            "weight":100 #To be changed
        }).run(cn)

    return jsonify({ "post-created":True })

#return posts from database
@api.route("/posts", methods=["GET"])
def post():
    return jsonify(
        {"posts":[{"title":"No More Vegetarian Food", "description":"we need normal people food!","score":1000, "id":69},
        {"title":"Vote Cole", "description":"Cole for prez!","score":345, "id":420}]} # dummy values
    )

#404
@api.route("/<path:path>")
def not_found(path):
    return jsonify({ "Not Found":path }), 404
@@ -1124,20 +1124,26 @@
 def 
+list_
 post
+s
 ():%0A    
 retu
@@ -1138,31 +1138,37 @@
 ():%0A    
-return jsonify(
+with connect() as cn:
 %0A       
@@ -1172,227 +1172,170 @@
     
-%7B%22
 posts
-%22:%5B%7B%22title%22:%22No More Vegetarian Food%22, %22description%22:%22we need normal people food!%22,%22score%22:1000, %22id%22:69%7D,%0A        %7B%22title%22:%22Vote Cole%22, %22description%22:%22Cole for prez!%22,%22score%22:345, %22id%22:420%7D%5D%7D # dummy values%0A    
+ = r.table(%22posts%22).run(cn)%0A%0A        postsList = %5B%5D%0A        for post in posts:%0A            postsList.append(post)%0A%0A        return jsonify(%7B%22posts%22:postsList%7D
 )%0A%0A#

Return all posts