11from fastapi import FastAPI , HTTPException , Request , Depends , Query
22from fastapi .responses import RedirectResponse
3+ from fastapi .middleware .cors import CORSMiddleware
34
45from firebase_admin import credentials , db , auth
56import firebase_admin
@@ -105,9 +106,15 @@ def get_api_key(api_key: str = Query(default=None, alias=API_KEY_NAME)):
105106
106107app = FastAPI (title = 'Cosmos API' , openapi_tags = tags_metadata )
107108
108- class AgendaPatch (BaseModel ):
109- nome_agenda : Optional [str ] = None
110- uid_do_responsável : Optional [str ] = None
109+ origins = ["*" ]
110+
111+ app .add_middleware (
112+ CORSMiddleware ,
113+ allow_origins = origins ,
114+ allow_credentials = True ,
115+ allow_methods = ["*" ],
116+ allow_headers = ["*" ],
117+ )
111118
112119@app .get ("/" )
113120def read_root ():
@@ -183,18 +190,46 @@ async def criar_um_usuario_com_email_e_senha(email: str, password: str, display_
183190 password = password ,
184191 display_name = display_name ,
185192 photo_url = photo_url ,
186- disabled = False )
193+ disabled = False
194+ )
187195
188196 return {"message" : 'Criado um usuário com sucesso. UID: {0}' .format (user .uid )}
189197
190198@app .delete ("/delete/user" , tags = ["Usuários" ], responses = STANDARD_RESPONSES )
191- async def deletar_um_usuario_com_o_uid (uid : str , api_key : str = Depends (get_api_key )):
192- if check_uid_exists (uid ):
193- auth .delete_user (uid )
194- return {"message" : f'O usuário com o UID { uid } foi deletado com sucesso.' }
199+ async def deletar_um_usuario_com_o_uid (uid_do_usuario : str , api_key : str = Depends (get_api_key )):
200+ if check_uid_exists (uid_do_usuario ):
201+ auth .delete_user (uid_do_usuario )
202+ return {"message" : f'O usuário com o UID { uid_do_usuario } foi deletado com sucesso.' }
195203 else :
196204 raise HTTPException (status_code = 400 , detail = "Este usuário não existe no banco de dados" )
197205
206+ @app .patch ("/update/user" , tags = ["Usuários" ], responses = STANDARD_RESPONSES )
207+ async def atualizar_os_dados_de_um_usuário (uid_do_usuario : str = Query (...), email : str = Query (None ), password : str = Query (None ), display_name : str = Query (None ), phone_number : str = Query (None ), photo_url : str = Query (None ), disabled : bool = Query (None ), api_key : str = Depends (get_api_key )):
208+ if not check_uid_exists (uid_do_usuario ):
209+ raise HTTPException (status_code = 404 , detail = "Este usuário não existe no banco de dados" )
210+
211+ try :
212+ update_data = {}
213+ if email is not None :
214+ update_data ['email' ] = email
215+ if phone_number is not None :
216+ update_data ['phone_number' ] = to_e164_br (phone_number )
217+ if password is not None :
218+ update_data ['password' ] = password
219+ if display_name is not None :
220+ update_data ['display_name' ] = display_name
221+ if photo_url is not None :
222+ update_data ['photo_url' ] = photo_url
223+ if disabled is not None :
224+ update_data ['disabled' ] = disabled
225+
226+ user = auth .update_user (uid_do_usuario , ** update_data )
227+
228+ return {"message" : f"Usuário { user .uid } atualizado com sucesso." }
229+
230+ except auth .AuthError as e :
231+ raise HTTPException (status_code = 500 , detail = f"Erro ao atualizar o usuário: { str (e )} " )
232+
198233@app .get ("/getAllAgendas" , tags = ["Agenda" ], responses = STANDARD_RESPONSES )
199234async def mostrar_todas_as_agendas_criadas (api_key : str = Depends (get_api_key )):
200235 if agenda_ref .get () is None :
@@ -340,7 +375,7 @@ async def deletar_um_evento_com_o_uid(uid_da_agenda: str, uid_do_evento: str, ap
340375 return {"message" : f'O evento com o UID { uid_do_evento } foi deletado com sucesso.' }
341376
342377@app .patch ("/update/agenda" , tags = ["Agenda" ], responses = STANDARD_RESPONSES )
343- async def atualizar_os_dados_da_agenda (uid_da_agenda : str = Query (...), nome_agenda : str = Query (None ), uid_do_responsável : str = Query (None ), api_key : str = Depends (get_api_key )):
378+ async def atualizar_os_dados_da_agenda (uid_da_agenda : str = Query (...), nome_agenda : str = Query (None ), uid_do_responsavel : str = Query (None ), api_key : str = Depends (get_api_key )):
344379 agenda_node = agenda_ref .child (uid_da_agenda )
345380 agenda_data = agenda_node .get ()
346381 if not agenda_data :
@@ -349,8 +384,8 @@ async def atualizar_os_dados_da_agenda(uid_da_agenda: str = Query(...), nome_age
349384 update_data = {}
350385 if nome_agenda is not None :
351386 update_data ["nome_agenda" ] = nome_agenda
352- if uid_do_responsável is not None :
353- update_data ["uid_do_responsável" ] = uid_do_responsável
387+ if uid_do_responsavel is not None :
388+ update_data ["uid_do_responsável" ] = uid_do_responsavel
354389
355390 if not update_data :
356391 raise HTTPException (status_code = 400 , detail = "Nenhum dado fornecido para atualização" )
0 commit comments