add: order paths & mds

This commit is contained in:
h z
2024-12-29 18:53:02 +00:00
parent c10c690149
commit a96e833aa6
5 changed files with 103 additions and 9 deletions

View File

@@ -110,3 +110,40 @@ def delete_path(path_id):
session.commit()
return jsonify({"message": "path deleted"}), 200
@path_bp.route('/move_forward/<int:path_id>', methods=['PATCH'])
@require_auth(roles=['admin'])
@limiter.limit(api.get_rate_limit)
def move_forward(path_id):
with get_db() as session:
path = session.query(Path).get(path_id)
if not path:
return jsonify({"error": "file not found"}), 404
siblings = session.query(Path).filter(Path.parent_id == path.parent_id).order_by(Path.order).all()
current_index = siblings.index(path)
if current_index == 0:
return jsonify({"error": "already at the first position"}), 400
previous_path = siblings[current_index - 1]
path.order, previous_path.order = previous_path.order, path.order
session.commit()
return jsonify(path.to_dict()), 200
@path_bp.route('/move_backward/<int:path_id>', methods=['PATCH'])
@require_auth(roles=['admin'])
@limiter.limit(api.get_rate_limit)
def move_backward(path_id):
with get_db() as session:
path = session.query(Path).get(path_id)
if not path:
return jsonify({"error": "file not found"}), 404
siblings = session.query(Path).filter(Path.parent_id == path.parent_id).order_by(Path.order).all()
current_index = siblings.index(path)
if current_index == len(siblings) - 1:
return jsonify({"error": "already at the last position"}), 400
next_path = siblings[current_index + 1]
path.order, next_path.order = next_path.order, path.order
session.commit()
return jsonify(path.to_dict()), 200