Skip to content

Commit bf9be4a

Browse files
committed
mock db and interface
1 parent 0d345e5 commit bf9be4a

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

app.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flask import Flask, jsonify, request
2+
import mockdb.mockdb_interface as db
23

34
app = Flask(__name__)
45

@@ -27,18 +28,17 @@ def create_response(data={}, status=200, message=''):
2728
"""
2829

2930
@app.route('/')
30-
def my_first_route():
31+
def hello_world():
3132
return create_response('hello world!')
3233

3334
@app.route('/mirror/<name>')
34-
def my_second_route(name):
35-
return create_response(name)
36-
37-
@app.route('/users', methods=['GET'])
38-
def get_all_users():
39-
if request.method == 'GET':
40-
data = { 'users': ['aria', 'tim', 'varun', 'alex'] }
41-
return create_response(data)
35+
def mirror(name):
36+
data = {
37+
'name': name
38+
}
39+
return create_response(data)
40+
41+
# TODO: Implement the rest of the API here!
4242

4343
"""
4444
~~~~~~~~~~~~ END API ~~~~~~~~~~~~

mockdb/dummy_data.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
initial_db_state = {
2+
'users': [
3+
{
4+
"id": 1,
5+
"name": "Aria",
6+
"age": 19,
7+
"team": "LWB"
8+
},
9+
{
10+
"id": 2,
11+
"name": "Tim",
12+
"age": 20,
13+
"team": "LWB"
14+
},
15+
{
16+
"id": 3,
17+
"name": "Varun",
18+
"age": 23,
19+
"team": "NNB"
20+
},
21+
{
22+
"id": 4,
23+
"name": "Alex",
24+
"age": 24,
25+
"team": "C2TC"
26+
}
27+
]
28+
}

mockdb/mockdb_interface.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from mockdb.dummy_data import initial_db_state
2+
import json
3+
4+
db_state = initial_db_state
5+
6+
def get(type):
7+
return db_state[type]
8+
9+
def getById(type, id):
10+
queried = [i for i in get(type) if i['id'] == id]
11+
if (len(queried)):
12+
return queried[0]
13+
return None
14+
15+
def create(type, payload):
16+
last_id = max([i['id'] for i in get(type)])
17+
new_id = last_id + 1
18+
payload['id'] = new_id
19+
db_state[type].append(payload)
20+
return payload
21+
22+
def updateById(type, id, update_values):
23+
item = getById(type, id)
24+
if not item:
25+
return None
26+
for k, v in update_values.items():
27+
item[k] = v
28+
return item
29+
30+
def deleteById(type, id):
31+
db_state[type] = [i for i in get(type) if i['id'] != id]

0 commit comments

Comments
 (0)