"""Service for managing list positioning and reordering""" from app import db from app.models import List class ListPositionService: """Service for handling list position management""" @staticmethod def reorder_lists(board_id: int, moved_list_id: int, new_position: float) -> None: """ Reorder all lists in a board when one list is moved to a new position. Args: board_id: The ID of board containing lists moved_list_id: The ID of list being moved new_position: The new position for moved list """ # Get all lists in board, ordered by their current position all_lists = List.query.filter_by(board_id=board_id).order_by(List.pos).all() # Find moved list in board moved_list = None other_lists = [] for lst in all_lists: if lst.id == moved_list_id: moved_list = lst else: other_lists.append(lst) if not moved_list: return # List not found in this board # Insert moved list at the new position other_lists.insert(int(new_position), moved_list) # Update positions for all lists to ensure unique, sequential positions for index, lst in enumerate(other_lists): lst.pos = float(index) db.session.commit() @staticmethod def get_next_position(board_id: int) -> float: """ Get next available position in a board. Args: board_id: The ID of board Returns: The next available position (float) """ last_list = ( List.query.filter_by(board_id=board_id).order_by(List.pos.desc()).first() ) return float(last_list.pos + 1) if last_list else 0.0 @staticmethod def ensure_unique_positions(board_id: int) -> None: """ Ensure all lists in a board have unique, sequential positions. Useful for data cleanup. Args: board_id: The ID of board to fix """ lists = List.query.filter_by(board_id=board_id).order_by(List.pos).all() for index, lst in enumerate(lists): lst.pos = float(index) db.session.commit()