-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
49 lines (32 loc) · 1.29 KB
/
app.py
File metadata and controls
49 lines (32 loc) · 1.29 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
#this is our app.py
from flask import Flask, request, jsonify
def create_app(test_config=None):
app = Flask(__name__)
if test_config:
app.config.update(test_config) #testing = true... #database memory
app.posts = [] #to store all blog posts as list of dic
app.next_id = 1 # to keep track of next post ID # home route (greeting function - default)
@app.route("/")
def home():
return jsonify(message= "Welcome to our Blog for testing"), 200
#list posts ->> to show all the post
@app.route("/posts")
def list_posts():
return jsonify(posts=app.posts), 200
#check ->>> create new posts with validation testing
@app.route("/posts", methods=["POST"])
def create_post():
data = request.get_json()
title = (data.get("title")or "").strip()
body = (data.get("body")or "").strip()
#validation
if not title:
return jsonify(error="Title Required"), 400
if not body:
return jsonify(error="Body Required"), 400
#create post
post = {"id": app.next_id, "title": title, "body": body}
app.next_id += 1
app.posts.append(post)
return jsonify(post=post), 201
return app