-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
345e799
commit 206bbe2
Showing
5 changed files
with
80 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from config.database import get_connection, release_connection | ||
from schemas.user_schema import UserSchema | ||
import psycopg2 | ||
from psycopg2 import DatabaseError | ||
|
||
class UserDAL: | ||
def __init__(self): | ||
self.conn = get_connection() | ||
self.cursor = self.conn.cursor() | ||
|
||
def verify_user(self, user: UserSchema): | ||
try: | ||
if not user.username or not user.password: | ||
raise ValueError("Username and password must not be empty.") | ||
|
||
self.cursor.execute(""" | ||
SELECT EXISTS ( | ||
SELECT 1 FROM users WHERE username = %s AND password = %s | ||
); | ||
""", (user.username, user.password)) | ||
|
||
return self.cursor.fetchone()[0] # Returns True or False | ||
|
||
except (psycopg2.Error, DatabaseError) as db_error: | ||
self.conn.rollback() | ||
print(f"Database error: {db_error}") | ||
return {"status": "error", "error": str(db_error)} | ||
|
||
except ValueError as val_error: | ||
print(f"Input error: {val_error}") | ||
return {"status": "error", "error": str(val_error)} | ||
|
||
except Exception as e: | ||
self.conn.rollback() | ||
print(f"Unexpected error: {e}") | ||
return {"status": "error", "error": str(e)} | ||
|
||
def __del__(self): | ||
""" Ensure the connection is closed when the object is destroyed. """ | ||
if self.conn: | ||
release_connection(self.conn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from dal.user_dal import UserDAL | ||
from schemas.user_schema import UserSchema | ||
|
||
class UserRepository: | ||
def __init__(self, dal: UserDAL): | ||
self.dal = dal | ||
|
||
def verify_user(self, user: UserSchema): | ||
return self.dal.verify_user(user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from pydantic import BaseModel | ||
|
||
class UserSchema(BaseModel): | ||
username: str | ||
password: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from repository.user_repository import UserRepository | ||
from dal.user_dal import UserDAL | ||
from schemas.user_schema import UserSchema | ||
|
||
class UserService: | ||
def __init__(self, repository: UserRepository): | ||
self.repository = repository | ||
|
||
def verify_user(self, user: UserSchema): | ||
return self.repository.verify_user(user) | ||
|
||
def get_user_service(): | ||
dal = UserDAL() | ||
repository = UserRepository(dal) | ||
return UserService(repository) |