-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
65 lines (49 loc) · 1.38 KB
/
app.py
File metadata and controls
65 lines (49 loc) · 1.38 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
from flask import Flask, jsonify, request
app = Flask(__name__)
games = [
{
'id': 1,
'title': 'Red Dead Redemption',
'developer': 'Rockstar'
},
{
'id': 2,
'title': 'The Witcher 3',
'developer': 'CD Projekt Red'
},
{
'id': 3,
'title': 'Ghost of Tsushima',
'developer': 'Sucker Punch'
}
]
@app.route('/', methods=['GET'])
def start():
return jsonify({"welcome": "to Neo's Favorite Games Python API"})
@app.route('/games', methods=['GET'])
def get_games():
return jsonify(games)
@app.route('/games/<int:id>', methods=['GET'])
def get_game_by_id(id):
for game in games:
if game.get('id') == id:
return jsonify(game)
@app.route('/games/<int:id>', methods=['PUT'])
def edit_game_by_id(id):
altered_game = request.get_json()
for index, game in enumerate(games):
if game.get('id') == id:
games[index].update(altered_game)
return jsonify(games[index])
@app.route('/games/', methods=['POST'])
def add_new_game():
new_game = request.get_json()
games.append(new_game)
return jsonify(games)
@app.route('/games/<int:id>', methods=['DELETE'])
def delete_game(id):
for index, game in enumerate(games):
if game.get('id') == id:
del games[index]
return jsonify(games)
app.run(port=5000, host='localhost', debug=True)