-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
113 lines (84 loc) · 3.45 KB
/
app.py
File metadata and controls
113 lines (84 loc) · 3.45 KB
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
from flask import Flask,jsonify,request
from flask_sqlalchemy import SQLAlchemy
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='postgresql://postgres:password123@localhost/knowledge_portal'
app.config['SQLAlCHEMY_TRACK_MODIFICATIONS']=False
db=SQLAlchemy(app)
class Topic(db.Model):
__tablename__ = 'topic'
id = db.Column(db.Integer, primary_key = True)
topic_title = db. Column(db.String(100), nullable = False)
topic_description = db.Column(db.String(500), nullable = False)
posts = db.relationship('Post', backref='topic', lazy='dynamic')
class Post(db.Model):
__tablename__ = 'post'
id = db.Column(db.Integer, primary_key = True)
topic_id = db.Column(db.Integer, db.ForeignKey('topic.id'))
post_title = db. Column(db.String(100), nullable = False)
post_description = db.Column(db.String(500), nullable = False)
@app.route('/topics', methods = ['GET'])
def gettopics():
all_topics = []
topics = Topic.query.all()
for topic in topics:
results = {
"topic_id":topic.id,
"topic_title":topic.topic_title,
"topic_description":topic.topic_description, }
all_topics.append(results)
return jsonify(
{
"success": True,
"topics": all_topics,
}
)
@app.route('/post', methods = ['POST'])
def create_post():
post_data = request.json
topic_id= db.session.query(Topic).filter_by(topic_title=post_data['topic_title']).first().id
post_title = post_data['post_title']
post_description = post_data['post_description']
post = Post(topic_id=topic_id,post_title =post_title ,post_description =post_description )
db.session.add(post)
db.session.commit()
return jsonify({"success": True,"response":"Post added"})
@app.route('/topics/<int:topic_id>', methods = ['GET'])
def getposts_under_specific_topic(topic_id):
all_posts = []
posts = db.session.query(Post).filter_by(topic_id=topic_id).all()
for post in posts:
results = {
"post_id":post.id,
"post_title":post.post_title,
"post_description":post.post_description, }
all_posts.append(results)
return jsonify(
{
"success": True,
"posts": all_posts,
}
)
@app.route('/topics/<int:topic_id>/<int:post_id>', methods = ['PUT'])
def updatepost_under_specific_topic(topic_id,post_id):
posts = db.session.query(Post).filter_by(topic_id=topic_id).all()
for post in posts:
if post.id==post_id:
post_to_be_update=post
post_title = request.json['post_title']
post_description = request.json['post_description']
post_to_be_update.post_title = post_title
post_to_be_update.post_description = post_description
db.session.add(post_to_be_update)
db.session.commit()
return jsonify({"success": True,"response":"Post Updated"})
@app.route('/topics/<int:topic_id>/<int:post_id>', methods = ['DELETE'])
def deletepost_under_specific_topic(topic_id,post_id):
posts = db.session.query(Post).filter_by(topic_id=topic_id).all()
for post in posts:
if post.id==post_id:
post_to_be_delete=post
db.session.delete(post_to_be_delete)
db.session.commit()
return jsonify({"success": True,"response":"Post Deleted"})
if __name__=='__main__':
app.run(debug=True)