This commit is contained in:
KienVT9 2025-05-22 19:04:57 +07:00
parent ac32800ba7
commit 7cc42ee6a1
7 changed files with 82 additions and 82 deletions

View file

@ -15,21 +15,21 @@ def list_users():
accounts = json.load(f)
return accounts
except Exception as e:
raise HTTPException(status_code=500, detail=f"Lỗi đọc file accounts: {e}")
raise HTTPException(status_code=500, detail=f"Error reading accounts file: {e}")
# API thêm user
@router.post("/users", tags=["User"])
def add_user(
id: str = Body(..., description="ID user"),
id: str = Body(..., description="User ID"),
bybit_api_key: str = Body(..., description="Bybit API Key"),
bybit_api_secret: str = Body(..., description="Bybit API Secret"),
user_name: str = Body(..., description="Tên user")
user_name: str = Body(..., description="User name")
):
try:
with open(ACCOUNTS_FILE, 'r+', encoding='utf-8') as f:
accounts = json.load(f)
if any(acc['id'] == id for acc in accounts):
raise HTTPException(status_code=400, detail="ID đã tồn tại")
raise HTTPException(status_code=400, detail="ID already exists")
new_user = {
"id": id,
"bybit_api_key": bybit_api_key,
@ -40,11 +40,11 @@ def add_user(
f.seek(0)
json.dump(accounts, f, ensure_ascii=False, indent=2)
f.truncate()
return {"message": "Thêm user thành công"}
return {"message": "User added successfully"}
except HTTPException as e:
raise e
except Exception as e:
raise HTTPException(status_code=500, detail=f"Lỗi ghi file accounts: {e}")
raise HTTPException(status_code=500, detail=f"Error writing accounts file: {e}")
# API sửa user
@router.put("/users/{user_id}", tags=["User"])
@ -68,12 +68,12 @@ def update_user(
f.seek(0)
json.dump(accounts, f, ensure_ascii=False, indent=2)
f.truncate()
return {"message": "Cập nhật user thành công"}
raise HTTPException(status_code=404, detail="Không tìm thấy userId")
return {"message": "User updated successfully"}
raise HTTPException(status_code=404, detail="User ID not found")
except HTTPException as e:
raise e
except Exception as e:
raise HTTPException(status_code=500, detail=f"Lỗi ghi file accounts: {e}")
raise HTTPException(status_code=500, detail=f"Error writing accounts file: {e}")
# API xóa user
@router.delete("/users/{user_id}", tags=["User"])
@ -83,12 +83,12 @@ def delete_user(user_id: str):
accounts = json.load(f)
new_accounts = [acc for acc in accounts if acc['id'] != user_id]
if len(new_accounts) == len(accounts):
raise HTTPException(status_code=404, detail="Không tìm thấy userId")
raise HTTPException(status_code=404, detail="User ID not found")
f.seek(0)
json.dump(new_accounts, f, ensure_ascii=False, indent=2)
f.truncate()
return {"message": "Xóa user thành công"}
return {"message": "User deleted successfully"}
except HTTPException as e:
raise e
except Exception as e:
raise HTTPException(status_code=500, detail=f"Lỗi ghi file accounts: {e}")
raise HTTPException(status_code=500, detail=f"Error writing accounts file: {e}")