diff --git a/backend/routes.py b/backend/routes.py index f6b6538..fcb1aaa 100644 --- a/backend/routes.py +++ b/backend/routes.py @@ -11,7 +11,6 @@ # RETURN HEALTH OF THE APP ###################################################################### - @app.route("/health") def health(): return jsonify(dict(status="OK")), 200 @@ -35,7 +34,7 @@ def count(): ###################################################################### @app.route("/picture", methods=["GET"]) def get_pictures(): - pass + return jsonify(data) ###################################################################### # GET A PICTURE @@ -44,7 +43,10 @@ def get_pictures(): @app.route("/picture/", methods=["GET"]) def get_picture_by_id(id): - pass + for picture in data: + if picture["id"] == id: + return picture + return {"message": "picture not found"}, 404 ###################################################################### @@ -52,7 +54,17 @@ def get_picture_by_id(id): ###################################################################### @app.route("/picture", methods=["POST"]) def create_picture(): - pass + # get data from the json body + picture_in = request.json + print(picture_in) + # if the id is already there, return 303 with the URL for the resource + for picture in data: + if picture_in["id"] == picture["id"]: + return { + "Message": f"picture with id {picture_in['id']} already present" + }, 302 + data.append(picture_in) + return picture_in, 201 ###################################################################### # UPDATE A PICTURE @@ -61,11 +73,21 @@ def create_picture(): @app.route("/picture/", methods=["PUT"]) def update_picture(id): - pass + # get data from the json body + picture_in = request.json + for index, picture in enumerate(data): + if picture["id"] == id: + data[index] = picture_in + return picture, 201 + return {"message": "picture not found"}, 404 ###################################################################### # DELETE A PICTURE ###################################################################### @app.route("/picture/", methods=["DELETE"]) def delete_picture(id): - pass + for picture in data: + if picture["id"] == id: + data.remove(picture) + return "", 204 + return {"message": "picture not found"}, 404