48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
"""
|
|
关注列表 API 路由(纯数据库版)
|
|
"""
|
|
from flask import Blueprint, request, jsonify
|
|
from db import (
|
|
login_required, get_current_user_id,
|
|
db_get_watchlist, db_add_to_watchlist, db_remove_from_watchlist
|
|
)
|
|
|
|
bp = Blueprint('watchlist', __name__, url_prefix='/api')
|
|
|
|
|
|
@bp.route('/watchlist', methods=['GET'])
|
|
@login_required
|
|
def get_watchlist():
|
|
"""获取关注列表"""
|
|
user_id = get_current_user_id()
|
|
watchlist = db_get_watchlist(user_id)
|
|
return jsonify({'success': True, 'watchlist': watchlist})
|
|
|
|
|
|
@bp.route('/watchlist', methods=['POST'])
|
|
@login_required
|
|
def add_to_watchlist():
|
|
"""添加到关注列表"""
|
|
try:
|
|
user_id = get_current_user_id()
|
|
data = request.get_json()
|
|
code = data.get('code')
|
|
name = data.get('name', f'股票{code}')
|
|
|
|
watchlist, error = db_add_to_watchlist(user_id, code, name)
|
|
if error:
|
|
return jsonify({'success': False, 'error': error}), 400
|
|
|
|
return jsonify({'success': True, 'watchlist': watchlist})
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 400
|
|
|
|
|
|
@bp.route('/watchlist/<stock_code>', methods=['DELETE'])
|
|
@login_required
|
|
def remove_from_watchlist(stock_code):
|
|
"""从关注列表移除"""
|
|
user_id = get_current_user_id()
|
|
watchlist = db_remove_from_watchlist(user_id, stock_code)
|
|
return jsonify({'success': True, 'watchlist': watchlist or []})
|