first commit

This commit is contained in:
2026-04-07 16:05:05 +08:00
commit 9d9bdbb1ce
136 changed files with 5103 additions and 0 deletions
+71
View File
@@ -0,0 +1,71 @@
from flask import jsonify, request
from flask_jwt_extended import jwt_required
from . import api_bp
from ..models import Channel
from ..schemas import ChannelSchema, ChannelCreateSchema, ChannelUpdateSchema
from ..services import ChannelService
channel_schema = ChannelSchema()
channel_create_schema = ChannelCreateSchema()
channel_update_schema = ChannelUpdateSchema()
@api_bp.route("/channels", methods=["GET"])
@jwt_required()
def get_channels():
channels = Channel.query.all()
return jsonify(channel_schema.dump(channels, many=True))
@api_bp.route("/channels", methods=["POST"])
@jwt_required()
def create_channel():
data = request.get_json()
errors = channel_create_schema.validate(data)
if errors:
return jsonify({"error": errors}), 400
try:
channel = ChannelService.create_channel(data)
return jsonify(channel_schema.dump(channel)), 201
except ValueError as e:
return jsonify({"error": str(e)}), 400
@api_bp.route("/channels/<int:channel_id>", methods=["GET"])
@jwt_required()
def get_channel(channel_id):
channel = Channel.query.get(channel_id)
if not channel:
return jsonify({"error": "渠道不存在"}), 404
return jsonify(channel_schema.dump(channel))
@api_bp.route("/channels/<int:channel_id>", methods=["PUT"])
@jwt_required()
def update_channel(channel_id):
channel = Channel.query.get(channel_id)
if not channel:
return jsonify({"error": "渠道不存在"}), 404
data = request.get_json()
errors = channel_update_schema.validate(data)
if errors:
return jsonify({"error": errors}), 400
try:
updated_channel = ChannelService.update_channel(channel, data)
return jsonify(channel_schema.dump(updated_channel))
except ValueError as e:
return jsonify({"error": str(e)}), 400
@api_bp.route("/channels/<int:channel_id>", methods=["DELETE"])
@jwt_required()
def delete_channel(channel_id):
try:
ChannelService.delete_channel(channel_id)
return jsonify({"message": "渠道已删除"})
except ValueError as e:
return jsonify({"error": str(e)}), 400