From 3be4556844ce9bc7f34ad9bf24e47909952dc4ae Mon Sep 17 00:00:00 2001 From: Kita Trofimov <144846094+NiTro005@users.noreply.github.com> Date: Sun, 26 Oct 2025 13:14:51 +0300 Subject: [PATCH] feat(database/repositories/user_repository.py): Added user repository --- database/connection.py | 3 + database/repositories/__init__.py | 3 + database/repositories/user_repository.py | 122 +++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 database/repositories/user_repository.py diff --git a/database/connection.py b/database/connection.py index 934624b..cf575d4 100644 --- a/database/connection.py +++ b/database/connection.py @@ -52,3 +52,6 @@ def test_connection() -> bool: except Exception as e: print(f"Тест подключения бд провален: {e}") return False + + +print(test_connection()) \ No newline at end of file diff --git a/database/repositories/__init__.py b/database/repositories/__init__.py index e69de29..24fcbf5 100644 --- a/database/repositories/__init__.py +++ b/database/repositories/__init__.py @@ -0,0 +1,3 @@ +from .user_repository import UserRepository + +__all__ = ['UserRepository'] diff --git a/database/repositories/user_repository.py b/database/repositories/user_repository.py new file mode 100644 index 0000000..35f24a5 --- /dev/null +++ b/database/repositories/user_repository.py @@ -0,0 +1,122 @@ +from typing import List, Optional +from model.user import User +from database.connection import get_connection + + +class UserRepository: + def get_all(self) -> List[User]: + with get_connection() as conn: + with conn.cursor() as cur: + cur.execute("SELECT * FROM users ORDER BY id") + return [ + User( + id=row[0], + email=row[1], + password_hash=row[2], + name=row[3], + role=row[4], + created_at=row[5] + ) for row in cur.fetchall() + ] + + def get_by_id(self, user_id: int) -> Optional[User]: + with get_connection() as conn: + with conn.cursor() as cur: + cur.execute("SELECT * FROM users WHERE id = %s", (user_id,)) + row = cur.fetchone() + if row: + return User(*row) + return None + + def get_by_email(self, email: str) -> Optional[User]: + with get_connection() as conn: + with conn.cursor() as cur: + cur.execute("SELECT * FROM users WHERE email = %s", (email,)) + row = cur.fetchone() + if row: + return User(*row) + return None + + def create_user(self, email: str, password_hash: str, name: str, role: str) -> Optional[User]: + with get_connection() as conn: + with conn.cursor() as cur: + cur.execute(""" + INSERT INTO users (email, password_hash, name, role) + VALUES (%s, %s, %s, %s) + RETURNING id, email, password_hash, name, role, created_at + """, (email, password_hash, name, role)) + + row = cur.fetchone() + conn.commit() + + if row: + return User(*row) + return None + + def update_user(self, user_id: int, name: str = None, role: str = None) -> bool: + with get_connection() as conn: + with conn.cursor() as cur: + updates = [] + params = [] + + if name is not None: + updates.append("name = %s") + params.append(name) + + if role is not None: + updates.append("role = %s") + params.append(role) + + if not updates: + return False + + params.append(user_id) + query = f"UPDATE users SET {', '.join(updates)} WHERE id = %s" + + cur.execute(query, params) + conn.commit() + return cur.rowcount > 0 + + def delete_user(self, user_id: int) -> bool: + with get_connection() as conn: + with conn.cursor() as cur: + cur.execute("DELETE FROM users WHERE id = %s", (user_id,)) + conn.commit() + return cur.rowcount > 0 + + def get_users_by_role(self, role: str) -> List[User]: + with get_connection() as conn: + with conn.cursor() as cur: + cur.execute("SELECT * FROM users WHERE role = %s ORDER BY name", (role,)) + return [ + User(*row) for row in cur.fetchall() + ] + + def change_password(self, user_id: int, new_password_hash: str) -> bool: + with get_connection() as conn: + with conn.cursor() as cur: + cur.execute(""" + UPDATE users + SET password_hash = %s + WHERE id = %s + """, (new_password_hash, user_id)) + conn.commit() + return cur.rowcount > 0 + + def authenticate_user(self, email: str, password_hash: str) -> Optional[User]: + with get_connection() as conn: + with conn.cursor() as cur: + cur.execute(""" + SELECT * FROM users + WHERE email = %s AND password_hash = %s + """, (email, password_hash)) + row = cur.fetchone() + if row: + return User(*row) + return None + + def user_exists(self, email: str) -> bool: + with get_connection() as conn: + with conn.cursor() as cur: + cur.execute("SELECT 1 FROM users WHERE email = %s", (email,)) + return cur.fetchone() is not None \ No newline at end of file