forked from Anubhav-1020/Hacktoberfest-Beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
56 lines (38 loc) · 1.24 KB
/
app.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
from flask import Flask, request, jsonify, abort
app = Flask(__name__)
notes = notes = {1 : {'title':'First Note', 'information':'Initial commit :D'}}
@app.route('/', methods=['GET'])
def index():
return jsonify(notes)
@app.route('/<int:id>', methods=['GET'])
def get_by_id(id):
if notes.get(id):
return jsonify(notes.get(id))
return abort(404)
@app.route('/create', methods=['POST'])
def create():
new_note_info = request.get_json()
title = new_note_info.get('title')
information = new_note_info.get('information')
note_number = len(notes) + 1
new_note = {title : information}
notes[note_number] = new_note
return jsonify(new_note)
@app.route('/delete/<int:id>', methods=['DELETE'])
def delete(id):
if notes.get(id):
notes.pop(id)
return jsonify(notes)
return abort(500)
@app.route('/update/<int:id>', methods=['PUT'])
def update(id):
if notes.get(id):
updated_info = request.get_json()
title = updated_info.get('title')
information = updated_info.get('information')
updated_note = {title : information}
notes[id] = updated_note
return jsonify(updated_note)
return abort(500)
if __name__ == '__main__':
app.run()