diff --git a/backend/routes.py b/backend/routes.py index 616e26f..2e509c3 100644 --- a/backend/routes.py +++ b/backend/routes.py @@ -51,3 +51,76 @@ def parse_json(data): ###################################################################### # INSERT CODE HERE ###################################################################### +@app.route("/health") +def healthz(): + return jsonify(dict(status="OK")), 200 + +@app.route("/count") +def count(): + """return length of data""" + count = db.songs.count_documents({}) + + return {"count": count}, 200 + +@app.route("/song", methods=["GET"]) +def songs(): + # docker run -d --name mongodb-test -e MONGO_INITDB_ROOT_USERNAME=user + # -e MONGO_INITDB_ROOT_PASSWORD=password -e MONGO_INITDB_DATABASE=collection mongo + results = list(db.songs.find({})) + print(results[0]) + return {"songs": parse_json(results)}, 200 + +@app.route("/song/", methods=["GET"]) +def get_song_by_id(id): + song = db.songs.find_one({"id": id}) + if not song: + return {"message": f"song with id {id} not found"}, 404 + return parse_json(song), 200 + +@app.route("/song", methods=["POST"]) +def create_song(): + # get data from the json body + song_in = request.json + + print(song_in["id"]) + + # if the id is already there, return 303 with the URL for the resource + song = db.songs.find_one({"id": song_in["id"]}) + if song: + return { + "Message": f"song with id {song_in['id']} already present" + }, 302 + + insert_id: InsertOneResult = db.songs.insert_one(song_in) + + return {"inserted id": parse_json(insert_id.inserted_id)}, 201 + +@app.route("/song/", methods=["PUT"]) +def update_song(id): + + # get data from the json body + song_in = request.json + + song = db.songs.find_one({"id": id}) + + if song == None: + return {"message": "song not found"}, 404 + + updated_data = {"$set": song_in} + + result = db.songs.update_one({"id": id}, updated_data) + + if result.modified_count == 0: + return {"message": "song found, but nothing updated"}, 200 + else: + return parse_json(db.songs.find_one({"id": id})), 201 + +@app.route("/song/", methods=["DELETE"]) +def delete_song(id): + + result = db.songs.delete_one({"id": id}) + if result.deleted_count == 0: + return {"message": "song not found"}, 404 + else: + return "", 204 + \ No newline at end of file