# region_api.py import sqlite3 from threading import Lock db_lock = Lock() class RegionAPI: def __init__(self, db_path): self.db_path = db_path def add_region(self, region_id: int, region_name: str): if not region_id.isdigit() == True: return {"status": "failure", "message": "Region_id must be digit only"} with db_lock, sqlite3.connect(self.db_path) as conn: cursor = conn.cursor() cursor.execute('SELECT COUNT(*) FROM regions WHERE region_id = ?', (region_id,)) count = cursor.fetchone()[0] if count == 0: cursor.execute('INSERT INTO regions (region_id, region_name, active) VALUES (?, ?, 1)', (region_id, region_name)) conn.commit() return {"status": "success", "message": "Region added successfully"} else: return {"status": "error", "message": "Region already exists"} def remove_region(self, region_id): with db_lock, sqlite3.connect(self.db_path) as conn: cursor = conn.cursor() cursor.execute('SELECT COUNT(*) FROM regions WHERE region_id = ?', (region_id,)) count = cursor.fetchone()[0] if count == 0: return {"status": "error", "message": "Region not found"} else: cursor.execute('DELETE FROM regions WHERE region_id = ?', (region_id,)) conn.commit() return {"status": "success", "message": "Region removed successfully"} def get_regions(self): with db_lock, sqlite3.connect(self.db_path) as conn: cursor = conn.cursor() cursor.execute('SELECT region_id, region_name, active FROM regions') regions = cursor.fetchall() return [{"region_id": r[0], "region_name": r[1], "regions_active": r[2]} for r in regions] def change_region_status(self, region_id, active): with db_lock, sqlite3.connect(self.db_path) as conn: cursor = conn.cursor() cursor.execute('SELECT COUNT(*) FROM regions WHERE region_id = ?', (region_id,)) count = cursor.fetchone()[0] if count == 0: return {"status": "error", "message": "Region not found"} else: cursor.execute('UPDATE regions SET active = ? WHERE region_id = ?', (active, region_id)) conn.commit() return {"status": "success", "message": "Region status updated successfully"} def update_region_status(self, region_id, active): with db_lock, sqlite3.connect(self.db_path) as conn: cursor = conn.cursor() # Проверяем существование региона cursor.execute("SELECT region_name FROM regions WHERE region_id = ?", (region_id,)) result = cursor.fetchone() if not result: return {"status": "error", "message": "Регион не найден"} # Обновляем статус активности региона cursor.execute("UPDATE regions SET active = ? WHERE region_id = ?", (int(active), region_id)) conn.commit() action = "Активирован" if active else "Отключён" return {"status": "success", "message": f"Регион {region_id} {action}"}