-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhoots_blueprint.py
117 lines (109 loc) · 5.17 KB
/
hoots_blueprint.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from flask import Blueprint, jsonify, request, g
import psycopg2, psycopg2.extras
from auth_middleware import token_required
from db_helpers import get_db_connection, consolidate_comments_in_hoots
hoots_blueprint = Blueprint('hoots_blueprint', __name__)
@hoots_blueprint.route('/hoots', methods=['POST'])
@token_required
def create_hoot():
try:
new_hoot = request.json
new_hoot["author"] = g.user["id"]
conection = get_db_connection()
cursor = conection.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("""
INSERT INTO hoots (author, title, text, category)
VALUES (%s, %s, %s, %s)
RETURNING *
""",
(new_hoot['author'], new_hoot['title'], new_hoot['text'], new_hoot['category'])
)
created_hoot = cursor.fetchone()
conection.commit()
conection.close()
return jsonify({"hoot": created_hoot}), 201
except Exception as error:
return jsonify({"error": str(error)}), 500
@hoots_blueprint.route('/hoots', methods=['GET'])
def hoots_index():
try:
conection = get_db_connection()
cursor = conection.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("""SELECT h.id, h.author AS hoot_author_id, h.title, h.text, h.category, u_hoot.username AS author_username, c.id AS comment_id, c.text AS comment_text, u_comment.username AS comment_author_username
FROM hoots h
INNER JOIN users u_hoot ON h.author = u_hoot.id
LEFT JOIN comments c ON h.id = c.hoot
LEFT JOIN users u_comment ON c.author = u_comment.id;
""")
hoots = cursor.fetchall()
# Update:
consolidated_hoots = consolidate_comments_in_hoots(hoots)
conection.commit()
conection.close()
return jsonify({"hoots": consolidated_hoots}), 200
except Exception as error:
return jsonify({"error": str(error)}), 500
@hoots_blueprint.route('/hoots/<hoot_id>', methods=['GET'])
def show_hoot(hoot_id):
try:
connection = get_db_connection()
cursor = connection.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("""
SELECT h.id, h.author AS hoot_author_id, h.title, h.text, h.category, u_hoot.username AS author_username, c.id AS comment_id, c.text AS comment_text, u_comment.username AS comment_author_username
FROM hoots h
INNER JOIN users u_hoot ON h.author = u_hoot.id
LEFT JOIN comments c ON h.id = c.hoot
LEFT JOIN users u_comment ON c.author = u_comment.id
WHERE h.id = %s;""",
(hoot_id,))
unprocessed_hoot = cursor.fetchall()
if unprocessed_hoot is not None :
processed_hoot = consolidate_comments_in_hoots(unprocessed_hoot)[0]
connection.close()
return jsonify({"hoot": processed_hoot}), 200
else:
connection.close()
return jsonify({"error": "Hoot not found"}), 404
except Exception as error:
return jsonify({"error": str(error)}), 500
@hoots_blueprint.route('/hoots/<hoot_id>', methods=['PUT'])
@token_required
def update_hoot(hoot_id):
try:
updated_hoot_data = request.json
connection = get_db_connection()
cursor = connection.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("SELECT * FROM hoots WHERE hoots.id = %s", (hoot_id,))
hoot_to_update = cursor.fetchone()
if hoot_to_update is None:
return jsonify({"error": "hoot not found"}), 404
connection.commit()
if hoot_to_update["author"] is not g.user["id"]:
return jsonify({"error": "Unauthorized"}), 401
cursor.execute("UPDATE hoots SET title = %s, text = %s, category = %s WHERE hoots.id = %s RETURNING *",
(updated_hoot_data["title"], updated_hoot_data["text"], updated_hoot_data["category"], hoot_id))
updated_hoot = cursor.fetchone()
connection.commit()
connection.close()
return jsonify({"hoot": updated_hoot}), 200
except Exception as error:
return jsonify({"error": str(error)}), 500
@hoots_blueprint.route('/hoots/<hoot_id>', methods=['DELETE'])
@token_required
def delete_hoot(hoot_id):
try:
connection = get_db_connection()
cursor = connection.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("SELECT * FROM hoots WHERE hoots.id = %s", (hoot_id,))
hoot_to_update = cursor.fetchone()
if hoot_to_update is None:
return jsonify({"error": "hoot not found"}), 404
connection.commit()
if hoot_to_update["author"] is not g.user["id"]:
return jsonify({"error": "Unauthorized"}), 401
cursor.execute("DELETE FROM hoots WHERE hoots.id = %s", (hoot_id,))
connection.commit()
connection.close()
return jsonify({"message": "hoot deleted successfully"}), 200
except Exception as error:
return jsonify({"error": str(error)}), 500