database in service
This commit is contained in:
47
tools/ws/heroes_mock.py
Normal file
47
tools/ws/heroes_mock.py
Normal file
@ -0,0 +1,47 @@
|
||||
HEROES = [
|
||||
{ "id": 1, "name": "Wolfgang" },
|
||||
{ "id": 2, "name": "Andreas" },
|
||||
{ "id": 3, "name": "Frank" },
|
||||
{ "id": 4, "name": "Thomas" },
|
||||
{ "id": 5, "name": "Barbara" },
|
||||
{ "id": 6, "name": "Robert" },
|
||||
{ "id": 7, "name": "Raphael" },
|
||||
{ "id": 8, "name": "Lucia" },
|
||||
{ "id": 9, "name": "Gregor" },
|
||||
]
|
||||
|
||||
|
||||
def get_heroes():
|
||||
if HEROES:
|
||||
return HEROES
|
||||
else:
|
||||
return 'No heroes available', 404
|
||||
|
||||
def get_hero(id=None):
|
||||
try:
|
||||
hero = next(x for x in HEROES if x["id"] == id)
|
||||
return { "id": hero["id"], "name": hero["name"] }
|
||||
except StopIteration:
|
||||
return 'Hero not found', 404
|
||||
|
||||
def put_hero(id=None, hero=None):
|
||||
try:
|
||||
heroToUpdate = next(x for x in HEROES if x["id"] == id)
|
||||
checkNewName = next((x for x in HEROES if x["name"] == hero["name"]), None)
|
||||
if (checkNewName):
|
||||
return 'Duplicate name', 403
|
||||
heroToUpdate["name"] = hero["name"]
|
||||
return 'Hero updated', 200
|
||||
except StopIteration:
|
||||
return 'Hero not found', 404
|
||||
except Exception:
|
||||
return 'Some error', 403
|
||||
|
||||
def post_hero(hero=None):
|
||||
try:
|
||||
newHeroId = len(HEROES)
|
||||
hero["id"] = newHeroId
|
||||
HEROES.append(hero)
|
||||
return 'Hero inserted', 201
|
||||
except Exception:
|
||||
return 'Some error', 403
|
Reference in New Issue
Block a user