@@ -45,40 +45,37 @@ def all(self, query_params: QueryParams) -> dict[str, Any]:
4545 output = paginate (query , page , rows_per_page , sort )
4646 return output
4747
48- def view (self , user_id : int ) -> dict [str , Any ]:
48+ def view (self , user_id : int ) -> dict [str , Any ] | None :
4949 user = None
5050
5151 try :
5252 with PgDatabase () as db :
5353 db .cursor .execute (f"SELECT { self .all_columns } FROM { self .table } WHERE id = %s" , (user_id ,))
5454 row = db .cursor .fetchone ()
55-
56- if row is None :
57- raise HTTPException (status_code = 404 , detail = {"error" : True , "message" : "Usuário não encontrado" })
5855 except Exception :
5956 raise HTTPException (status_code = 500 , detail = {"error" : True , "message" : "Database error" })
60-
61- user = line_to_dict (row , self .columns )
57+
58+ if row is not None :
59+ user = line_to_dict (row , self .columns )
60+
6261 return user
63-
6462
65- def view_by_email (self , email : str ) -> dict [str , Any ]:
63+ def view_by_email (self , email : str ) -> dict [str , Any ] | None :
6664 user = None
6765
6866 try :
6967 with PgDatabase () as db :
7068 db .cursor .execute (f"SELECT { self .all_columns } FROM { self .table } WHERE email = %s" , (email ,))
7169 row = db .cursor .fetchone ()
72-
73- if row is None :
74- raise HTTPException (status_code = 404 , detail = {"error" : True , "message" : "Usuário não encontrado" })
7570 except Exception :
7671 raise HTTPException (status_code = 500 , detail = {"error" : True , "message" : "Database error" })
7772
78- user = line_to_dict (row , self .columns )
73+ if row is not None :
74+ user = line_to_dict (row , self .columns )
75+
7976 return user
8077
81- def add (self , user : UserAddSchema ) -> dict [ str , Any ] :
78+ def add (self , user : UserAddSchema ) -> int :
8279 senha = bcrypt_context .hash (user .senha )
8380
8481 try :
@@ -91,12 +88,14 @@ def add(self, user: UserAddSchema) -> dict[str, Any]:
9188
9289 inserted_id = raw_id [0 ]
9390 db .connection .commit ()
91+ except HTTPException as e :
92+ raise e
9493 except Exception as e :
9594 raise HTTPException (status_code = 500 , detail = {"error" : True , "message" : str (e )})
9695
97- return { "error" : False , "message" : f"Usuário { user . email } adicionado com sucesso." , "id" : inserted_id }
96+ return inserted_id
9897
99- def edit (self , user_id : int , user : UserEditSchema ) -> dict [ str , Any ] :
98+ def edit (self , user_id : int , user : UserEditSchema ) -> None :
10099 user_dict = user .model_dump (exclude_none = True )
101100 if not user_dict :
102101 raise HTTPException (status_code = 200 , detail = {"error" : False , "message" : f"Usuário com id { user_id } editado com sucesso." })
@@ -109,14 +108,10 @@ def edit(self, user_id: int, user: UserEditSchema) -> dict[str, Any]:
109108 except Exception :
110109 raise HTTPException (status_code = 500 , detail = {"error" : True , "message" : "Database error" })
111110
112- return {"error" : False , "message" : f"Usuário com id { user_id } editado com sucesso." }
113-
114- def delete (self , user_id : int ) -> dict [str , Any ]:
111+ def delete (self , user_id : int ) -> None :
115112 try :
116113 with PgDatabase () as db :
117114 db .cursor .execute (f"DELETE FROM { self .table } WHERE id = %s" , (user_id ,))
118115 db .connection .commit ()
119116 except Exception :
120117 raise HTTPException (status_code = 500 , detail = {"error" : True , "message" : "Database error" })
121-
122- return {"error" : False , "message" : f"Usuário com id { user_id } deletado com sucesso." }
0 commit comments