Crud completo
This commit is contained in:
@@ -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
|
||||
|
||||
0
enable-venv.sh
Normal file → Executable file
0
enable-venv.sh
Normal file → Executable file
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -11,3 +11,9 @@ 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):
|
||||
...
|
||||
Binary file not shown.
@@ -31,3 +31,24 @@ class ViewUserById:
|
||||
def execute(self, user_id: int) -> User | None:
|
||||
user = self.repo.viewById(user_id)
|
||||
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)
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -59,3 +59,41 @@ class SqlUserRepository(UserRepository):
|
||||
email = model.email,
|
||||
phone = model.phone
|
||||
)
|
||||
|
||||
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.")
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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())
|
||||
|
||||
@@ -42,3 +42,25 @@ def view_user_by_id(user_id: int):
|
||||
detail="User not found")
|
||||
|
||||
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)
|
||||
BIN
src/users.db
BIN
src/users.db
Binary file not shown.
Reference in New Issue
Block a user