Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions backend/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:id>", 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/<int:id>", 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/<int:id>", 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