From da8646cae90cba48d714364a25784aa6890f4875 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 2 Apr 2024 09:27:35 -0700 Subject: [PATCH] refac --- backend/apps/web/models/auths.py | 16 ---------------- backend/apps/web/models/users.py | 16 ++++++++++++++++ backend/apps/web/routers/auths.py | 6 +++--- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/backend/apps/web/models/auths.py b/backend/apps/web/models/auths.py index 1d8549d35..069865036 100644 --- a/backend/apps/web/models/auths.py +++ b/backend/apps/web/models/auths.py @@ -167,22 +167,6 @@ class AuthsTable: except: return False - def update_api_key_by_id(self, id: str, api_key: str) -> str: - try: - query = Auth.update(api_key=api_key).where(Auth.id == id) - result = query.execute() - - return True if result == 1 else False - except: - return False - - def get_api_key_by_id(self, id: str) -> Optional[str]: - try: - auth = Auth.get(Auth.id == id) - return auth.api_key - except: - return None - def delete_auth_by_id(self, id: str) -> bool: try: # Delete User diff --git a/backend/apps/web/models/users.py b/backend/apps/web/models/users.py index d89b548da..a01e595e5 100644 --- a/backend/apps/web/models/users.py +++ b/backend/apps/web/models/users.py @@ -158,5 +158,21 @@ class UsersTable: except: return False + def update_user_api_key_by_id(self, id: str, api_key: str) -> str: + try: + query = User.update(api_key=api_key).where(User.id == id) + result = query.execute() + + return True if result == 1 else False + except: + return False + + def get_user_api_key_by_id(self, id: str) -> Optional[str]: + try: + user = User.get(User.id == id) + return user.api_key + except: + return None + Users = UsersTable(DB) diff --git a/backend/apps/web/routers/auths.py b/backend/apps/web/routers/auths.py index 6e03be437..293cb55b8 100644 --- a/backend/apps/web/routers/auths.py +++ b/backend/apps/web/routers/auths.py @@ -277,7 +277,7 @@ async def update_token_expires_duration( @router.post("/api_key", response_model=ApiKey) async def create_api_key_(user=Depends(get_current_user)): api_key = create_api_key() - success = Auths.update_api_key_by_id(user.id, api_key) + success = Users.update_user_api_key_by_id(user.id, api_key) if success: return { "api_key": api_key, @@ -289,14 +289,14 @@ async def create_api_key_(user=Depends(get_current_user)): # delete api key @router.delete("/api_key", response_model=bool) async def delete_api_key(user=Depends(get_current_user)): - success = Auths.update_api_key_by_id(user.id, None) + success = Users.update_user_api_key_by_id(user.id, None) return success # get api key @router.get("/api_key", response_model=ApiKey) async def get_api_key(user=Depends(get_current_user)): - api_key = Auths.get_api_key_by_id(user.id, None) + api_key = Users.get_user_api_key_by_id(user.id) if api_key: return { "api_key": api_key,