2021-02-28 12:01:11 -05:00
|
|
|
import json
|
|
|
|
from flask import Blueprint, request, jsonify
|
|
|
|
|
2021-02-28 15:25:12 -05:00
|
|
|
with open("data.json", "r", encoding="utf8") as read_data:
|
2021-02-28 12:01:11 -05:00
|
|
|
channels = json.load(read_data)
|
|
|
|
|
|
|
|
api_channels = Blueprint('api_channels', __name__)
|
|
|
|
|
|
|
|
|
|
|
|
@api_channels.route("/channels/all")
|
|
|
|
def list_channels():
|
|
|
|
""" Lists all channels in the database. """
|
|
|
|
|
|
|
|
return jsonify(channels)
|
|
|
|
|
|
|
|
|
2021-02-28 15:25:12 -05:00
|
|
|
@api_channels.route("/channels")
|
2021-02-28 12:01:11 -05:00
|
|
|
def get_channel():
|
|
|
|
"""
|
|
|
|
Opens the confirmation form that sends the vote
|
|
|
|
to the database corresponding the channel selected.
|
|
|
|
"""
|
|
|
|
|
2021-02-28 15:25:12 -05:00
|
|
|
if "name" in request.args and "vote" in request.args:
|
2021-02-28 12:01:11 -05:00
|
|
|
name = str(request.args["name"])
|
2021-02-28 15:25:12 -05:00
|
|
|
vote = str(request.args["vote"])
|
2021-02-28 12:01:11 -05:00
|
|
|
else:
|
2021-02-28 15:25:12 -05:00
|
|
|
return "No name of channel and vote provided."
|
2021-02-28 12:01:11 -05:00
|
|
|
|
|
|
|
if name in channels:
|
2021-02-28 15:25:12 -05:00
|
|
|
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}."
|
2021-02-28 12:01:11 -05:00
|
|
|
else:
|
|
|
|
return "The name specified is not a channel on the list."
|