diff --git a/.env/pyvenv.cfg b/.env/pyvenv.cfg index d676224..8b8effe 100644 --- a/.env/pyvenv.cfg +++ b/.env/pyvenv.cfg @@ -2,4 +2,4 @@ home = /usr/sbin include-system-site-packages = false version = 3.14.2 executable = /usr/bin/python3.14 -command = /usr/sbin/python -m venv /mnt/c/Users/Rodo Machenike/py/Microservices/.env +command = /usr/sbin/python3 -m venv /home/rodo/Documents/microservices/FastAPI---Microservices/.env diff --git a/enable-venv.sh b/enable-venv.sh old mode 100644 new mode 100755 diff --git a/src/__pycache__/main.cpython-314.pyc b/src/__pycache__/main.cpython-314.pyc index b3693bf..575ca77 100644 Binary files a/src/__pycache__/main.cpython-314.pyc and b/src/__pycache__/main.cpython-314.pyc differ diff --git a/src/application/__pycache__/exceptions.cpython-314.pyc b/src/application/__pycache__/exceptions.cpython-314.pyc index df9e6ab..6841d90 100644 Binary files a/src/application/__pycache__/exceptions.cpython-314.pyc and b/src/application/__pycache__/exceptions.cpython-314.pyc differ diff --git a/src/application/ports/__pycache__/user_repository.cpython-314.pyc b/src/application/ports/__pycache__/user_repository.cpython-314.pyc index de91283..dbe562d 100644 Binary files a/src/application/ports/__pycache__/user_repository.cpython-314.pyc and b/src/application/ports/__pycache__/user_repository.cpython-314.pyc differ diff --git a/src/application/ports/user_repository.py b/src/application/ports/user_repository.py index 173423e..4adf654 100644 --- a/src/application/ports/user_repository.py +++ b/src/application/ports/user_repository.py @@ -10,4 +10,10 @@ class UserRepository(ABC): ... @abstractmethod def viewById(self, user_id:int): + ... + @abstractmethod + def removeUser(self, user_id:int): + ... + @abstractmethod + def editUser(self, user_id:int, user:User): ... \ No newline at end of file diff --git a/src/application/services/__pycache__/user_services.cpython-314.pyc b/src/application/services/__pycache__/user_services.cpython-314.pyc index 1c5f6ab..653bbd0 100644 Binary files a/src/application/services/__pycache__/user_services.cpython-314.pyc and b/src/application/services/__pycache__/user_services.cpython-314.pyc differ diff --git a/src/application/services/user_services.py b/src/application/services/user_services.py index 338d828..be58631 100644 --- a/src/application/services/user_services.py +++ b/src/application/services/user_services.py @@ -30,4 +30,25 @@ class ViewUserById: def execute(self, user_id: int) -> User | None: user = self.repo.viewById(user_id) - return user \ No newline at end of file + return user + +class RemoveUserById: + def __init__(self, repo:UserRepository): + if not isinstance(repo, UserRepository): + raise TypeError("repo must implement UserRepository") + self.repo = repo + + def execute(self, user_id:int) -> User: + user = self.repo.removeUser(user_id) + return {"Result": f"User {user_id} deleted succesfully"} + +class EditUserById: + def __init__(self, repo:UserRepository): + if not isinstance(repo, UserRepository): + raise TypeError("repo must implement UserRepository") + self.repo = repo + + def execute(self, user_id:int, name:str, email:str, phone:str) -> User | None: + user = User(user_id=user_id, name=name, email=email, phone=phone) + return self.repo.editUser(user_id, user) + \ No newline at end of file diff --git a/src/core/__pycache__/config.cpython-314.pyc b/src/core/__pycache__/config.cpython-314.pyc index 6c3e40f..88c0388 100644 Binary files a/src/core/__pycache__/config.cpython-314.pyc and b/src/core/__pycache__/config.cpython-314.pyc differ diff --git a/src/domain/__pycache__/users.cpython-314.pyc b/src/domain/__pycache__/users.cpython-314.pyc index 6e7cb2b..6225dda 100644 Binary files a/src/domain/__pycache__/users.cpython-314.pyc and b/src/domain/__pycache__/users.cpython-314.pyc differ diff --git a/src/infrastructure/adapters/persistence/__pycache__/__init__.cpython-314.pyc b/src/infrastructure/adapters/persistence/__pycache__/__init__.cpython-314.pyc index 8475cf8..523173f 100644 Binary files a/src/infrastructure/adapters/persistence/__pycache__/__init__.cpython-314.pyc and b/src/infrastructure/adapters/persistence/__pycache__/__init__.cpython-314.pyc differ diff --git a/src/infrastructure/adapters/persistence/__pycache__/db.cpython-314.pyc b/src/infrastructure/adapters/persistence/__pycache__/db.cpython-314.pyc index 55c050a..c3ef8b5 100644 Binary files a/src/infrastructure/adapters/persistence/__pycache__/db.cpython-314.pyc and b/src/infrastructure/adapters/persistence/__pycache__/db.cpython-314.pyc differ diff --git a/src/infrastructure/adapters/persistence/__pycache__/models.cpython-314.pyc b/src/infrastructure/adapters/persistence/__pycache__/models.cpython-314.pyc index 0d134ee..138ba89 100644 Binary files a/src/infrastructure/adapters/persistence/__pycache__/models.cpython-314.pyc and b/src/infrastructure/adapters/persistence/__pycache__/models.cpython-314.pyc differ diff --git a/src/infrastructure/adapters/persistence/__pycache__/user_repository_sql.cpython-314.pyc b/src/infrastructure/adapters/persistence/__pycache__/user_repository_sql.cpython-314.pyc index b302c3b..8e76dbc 100644 Binary files a/src/infrastructure/adapters/persistence/__pycache__/user_repository_sql.cpython-314.pyc and b/src/infrastructure/adapters/persistence/__pycache__/user_repository_sql.cpython-314.pyc differ diff --git a/src/infrastructure/adapters/persistence/user_repository_sql.py b/src/infrastructure/adapters/persistence/user_repository_sql.py index d659514..61bc1c9 100644 --- a/src/infrastructure/adapters/persistence/user_repository_sql.py +++ b/src/infrastructure/adapters/persistence/user_repository_sql.py @@ -58,4 +58,42 @@ class SqlUserRepository(UserRepository): name = model.name, email = model.email, phone = model.phone - ) \ No newline at end of file + ) + + def removeUser(self, user_id): + model = self.db.query(UserModel).filter(UserModel.user_id == user_id).first() + + if not model: + return False + + try: + self.db.delete(model) + self.db.commit() + return True + except IntegrityError: + self.db.rollback() + raise UserAlreadyExists("Cannot delete user due to existing references.") + + def editUser(self, user_id: int, user: User) -> User | None: + model = self.db.query(UserModel).filter(UserModel.user_id == user_id).first() + + if not model: + return None + + try: + model.name = user.name + model.email = user.email + model.phone = user.phone + + self.db.commit() + self.db.refresh(model) + + return User( + user_id=model.user_id, + name=model.name, + email=model.email, + phone=model.phone + ) + except IntegrityError: + self.db.rollback() + raise UserAlreadyExists("A user with the same phone number or name does already exist.") \ No newline at end of file diff --git a/src/infrastructure/api/users/__pycache__/app.cpython-314.pyc b/src/infrastructure/api/users/__pycache__/app.cpython-314.pyc index a351659..e0b6473 100644 Binary files a/src/infrastructure/api/users/__pycache__/app.cpython-314.pyc and b/src/infrastructure/api/users/__pycache__/app.cpython-314.pyc differ diff --git a/src/infrastructure/api/users/__pycache__/root.cpython-314.pyc b/src/infrastructure/api/users/__pycache__/root.cpython-314.pyc index 480cafb..2d6ce02 100644 Binary files a/src/infrastructure/api/users/__pycache__/root.cpython-314.pyc and b/src/infrastructure/api/users/__pycache__/root.cpython-314.pyc differ diff --git a/src/infrastructure/api/users/__pycache__/router.cpython-314.pyc b/src/infrastructure/api/users/__pycache__/router.cpython-314.pyc index a0f4cf5..f280b53 100644 Binary files a/src/infrastructure/api/users/__pycache__/router.cpython-314.pyc and b/src/infrastructure/api/users/__pycache__/router.cpython-314.pyc differ diff --git a/src/infrastructure/api/users/__pycache__/users.cpython-314.pyc b/src/infrastructure/api/users/__pycache__/users.cpython-314.pyc index 36f244c..97eae4f 100644 Binary files a/src/infrastructure/api/users/__pycache__/users.cpython-314.pyc and b/src/infrastructure/api/users/__pycache__/users.cpython-314.pyc differ diff --git a/src/infrastructure/api/users/users.py b/src/infrastructure/api/users/users.py index 463a353..f21c0d2 100644 --- a/src/infrastructure/api/users/users.py +++ b/src/infrastructure/api/users/users.py @@ -1,5 +1,5 @@ from fastapi import APIRouter, HTTPException -from application.services.user_services import CreateUser, ViewUsers, ViewUserById +from application.services.user_services import CreateUser, ViewUsers, ViewUserById, RemoveUserById, EditUserById from infrastructure.adapters.persistence.user_repository_sql import SqlUserRepository from application.exceptions import UserAlreadyExists @@ -30,7 +30,7 @@ def view_all_users(): status_code=500, detail=e) -@router.get("/{id}") +@router.get("/{user_id}") def view_user_by_id(user_id: int): service = ViewUserById(SqlUserRepository()) @@ -41,4 +41,26 @@ def view_user_by_id(user_id: int): status_code=404, detail="User not found") - return result \ No newline at end of file + return result + +@router.delete("/{user_id}") +def delete_user_by_id(user_id: int): + service = RemoveUserById(SqlUserRepository()) + + result = service.execute(user_id) + + if result is None: + raise HTTPException( + status_code=404, + detail="User not found") + +@router.put("/{user_id}") +def edit_user_by_id(user_id: int, name:str, email:str, phone:str): + service = EditUserById(SqlUserRepository()) + + try: + result = service.execute(user_id=user_id, name=name, email=email, phone=phone) + except Exception as e: + raise HTTPException( + status_code=500, + detail=e) \ No newline at end of file diff --git a/src/users.db b/src/users.db index fdae28b..c120b74 100644 Binary files a/src/users.db and b/src/users.db differ