-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (52 loc) · 1.83 KB
/
main.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
from flask import Flask, request, jsonify
from models import Post
from db_operations import add, init, update, delete, get_all, get_specific
from datetime import datetime
app = Flask(__name__)
init()
@app.route('/')
def test():
return "<h2>Server is running</h2>"
@app.post('/add')
def create_post():
data = request.get_json()
if not data:
return jsonify({"error":"No JSON Provided"}), 400
try:
add(title = data['title'], content = data['content'], category = data['category'], tags = data['tags'], createdAt =datetime.now(), updatedAt= datetime.now())
except Exception as e:
return jsonify({'error': str(e)}), 500
return jsonify("Post Created"), 201
@app.put('/update/<int:id>')
def update_post(id):
data = request.get_json()
if not data:
return jsonify({"error":"No data provided"}), 400
try:
update(title = data['title'], content = data['content'], category = data['category'], tags = data['tags'], updatedAt=datetime.now(), id = id)
except Exception as e:
return jsonify({"error": str(e)}), 500
return jsonify("Post Updated"), 201
@app.delete('/delete/<int:id>')
def delete_post(id):
if not id:
return jsonify({"error": "No id was provided"}), 400
try:
delete(id = id)
except Exception as e:
return jsonify({"error": str(e)}), 500
return jsonify("Post Deleted"), 201
@app.get('/getposts')
def get_all_posts():
try:
posts, status_code=get_all()
return jsonify(posts)
except Exception as e:
return jsonify(str(e)), 500
@app.get('/posts?term=<term>')
def get_specific_posts(term):
try:
posts,statuscode = get_specific(term)
return jsonify(posts)
except Exception as e:
return jsonify(str(e)), 500