| 247 | |
| 248 | |
| 249 | def find_collection_item(config, key, item_id): |
| 250 | items = config.get(key, []) |
| 251 | if item_id.isdigit(): |
| 252 | index = int(item_id) |
| 253 | if 0 <= index < len(items): |
| 254 | return index, items[index] |
| 255 | return -1, None |
| 256 | id_field = COLLECTIONS[key]["id_field"] |
| 257 | matches = [(index, item) for index, item in enumerate(items) if str(item.get(id_field, "")) == item_id] |
| 258 | if len(matches) > 1: |
| 259 | raise ApiError(409, f"{key} has duplicate {id_field}; use numeric index instead", {"id": item_id}) |
| 260 | if matches: |
| 261 | return matches[0] |
| 262 | return -1, None |
| 263 | |
| 264 | |
| 265 | def collection_routes(): |