Add tag suggestions
This commit is contained in:
33
back/main.py
33
back/main.py
@@ -706,6 +706,39 @@ def get_hot_topics():
|
||||
return jsonify({"code": 1000, "data": {"list": data}})
|
||||
except Exception as e:
|
||||
return jsonify({"code": 2003, "data": str(e)})
|
||||
|
||||
@app.route('/api/tag_suggest', methods=['GET'])
|
||||
def tag_suggest():
|
||||
try:
|
||||
prefix = request.args.get("prefix", "")
|
||||
if prefix is None:
|
||||
prefix = ""
|
||||
prefix = str(prefix).strip().lstrip('#')
|
||||
if not prefix:
|
||||
return jsonify({"code": 1000, "data": {"list": []}})
|
||||
|
||||
limit = request.args.get("limit", 5, type=int)
|
||||
if limit < 1:
|
||||
limit = 1
|
||||
if limit > 10:
|
||||
limit = 10
|
||||
|
||||
rows = db.session.query(
|
||||
Hashtag.name,
|
||||
db.func.count(Hashtag.name).label('count')
|
||||
).filter(
|
||||
Hashtag.name.like(f"{prefix}%")
|
||||
).group_by(
|
||||
Hashtag.name
|
||||
).order_by(
|
||||
db.func.count(Hashtag.name).desc(),
|
||||
Hashtag.name.asc()
|
||||
).limit(limit).all()
|
||||
|
||||
data = [name for name, _ in rows]
|
||||
return jsonify({"code": 1000, "data": {"list": data}})
|
||||
except Exception as e:
|
||||
return jsonify({"code": 2003, "data": str(e)})
|
||||
|
||||
# --- 彩蛋 ---
|
||||
@app.route('/api/teapot', methods=['GET'])
|
||||
|
||||
Reference in New Issue
Block a user