From f9c62c24202939b0dbee2c6044c37795d6974769 Mon Sep 17 00:00:00 2001 From: Jose De Freitas Date: Sun, 28 Feb 2021 15:25:12 -0500 Subject: [PATCH] Add update database system --- voter/api/api.py | 18 +++++++++++++----- voter/data.json | 4 ++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/voter/api/api.py b/voter/api/api.py index 035d9bb..d7a0c1c 100644 --- a/voter/api/api.py +++ b/voter/api/api.py @@ -1,7 +1,7 @@ import json from flask import Blueprint, request, jsonify -with open("data.json", "r") as read_data: +with open("data.json", "r", encoding="utf8") as read_data: channels = json.load(read_data) api_channels = Blueprint('api_channels', __name__) @@ -14,19 +14,27 @@ def list_channels(): return jsonify(channels) -@api_channels.route("/channels", methods=["GET", "POST"]) +@api_channels.route("/channels") def get_channel(): """ Opens the confirmation form that sends the vote to the database corresponding the channel selected. """ - if "name" in request.args: + if "name" in request.args and "vote" in request.args: name = str(request.args["name"]) + vote = str(request.args["vote"]) else: - return "No name of channel provided." + return "No name of channel and vote provided." if name in channels: - return "Good!" + if vote == "upvote": + channels[name] += 1 + else: + channels[name] -= 1 + + with open("data.json", "w", encoding="utf8") as write_data: + json.dump(channels, write_data, indent=4) + return f"You {vote}d successfully the channel {name}." else: return "The name specified is not a channel on the list." diff --git a/voter/data.json b/voter/data.json index 459d1e0..8342a5b 100644 --- a/voter/data.json +++ b/voter/data.json @@ -1,6 +1,6 @@ { - "Tech With Tim": 0, + "Tech With Tim": 1, "Derek Banas": 0, "Don Jones": 0, - "LearningLad": 0 + "LearningLad": -1 } \ No newline at end of file