hero put
This commit is contained in:
@ -12,12 +12,11 @@ HEROES = [
|
||||
|
||||
|
||||
def get_hero(id=None):
|
||||
if id:
|
||||
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
|
||||
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 get_heroes():
|
||||
@ -25,5 +24,19 @@ def get_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
|
||||
newHeroId = len(HEROES)
|
||||
hero["id"] = newHeroId
|
||||
HEROES.append(hero)
|
||||
return 'Hero inserted', 201
|
||||
|
Reference in New Issue
Block a user