This commit is contained in:
2021-01-14 21:52:56 +01:00
parent 521e916bbd
commit 9717d171bf
3 changed files with 20 additions and 103 deletions

View File

@ -11,6 +11,12 @@ HEROES = [
]
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)
@ -18,25 +24,21 @@ def get_hero(id=None):
except StopIteration:
return 'Hero not found', 404
def get_heroes():
if HEROES:
return HEROES
else:
return 'No heroes available', 404
def put_hero(id=None, hero=None):
if id:
# update
try:
heroToUpdate = next(x for x in HEROES if x["id"] == id)
heroToUpdate["name"] = hero["name"]
return 'Hero updated', 200
except StopIteration:
return 'Hero not found', 404
else:
# insert
try:
heroToUpdate = next(x for x in HEROES if x["id"] == id)
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