|
43 | 43 |
|
44 | 44 | ref = db.reference("/") |
45 | 45 | agenda_ref = ref.child('agendas') |
| 46 | +agenda_membros_ref = ref.child('agenda_membros') |
46 | 47 |
|
47 | 48 | def to_e164_br(phone_number): |
48 | 49 | try: |
@@ -268,42 +269,64 @@ async def mostrar_todas_as_agendas_criadas(api_key: str = Depends(get_api_key)): |
268 | 269 | else: |
269 | 270 | return agenda_ref.get() |
270 | 271 |
|
| 272 | +@app.get("/getAllAgendasLinkedToUser", tags=["Agenda"], responses=STANDARD_RESPONSES) |
| 273 | +async def mostrar_todas_as_agendas_que_o_usuário_faz_parte(uid_do_responsavel: str, api_key: str = Depends(get_api_key)): |
| 274 | + user_agenda_ids = agenda_membros_ref.child(uid_do_responsavel).get() |
| 275 | + |
| 276 | + if not user_agenda_ids: |
| 277 | + raise HTTPException(status_code=404, detail="O usuário não está ligado a nenhuma agenda") |
| 278 | + |
| 279 | + agendas = {} |
| 280 | + for agenda_id in user_agenda_ids: |
| 281 | + agenda_data = agenda_ref.child(agenda_id).get() |
| 282 | + if agenda_data: |
| 283 | + agendas[agenda_id] = agenda_data |
| 284 | + |
| 285 | + return agendas |
| 286 | + |
271 | 287 | @app.post("/add/agenda", tags=["Agenda"], responses=STANDARD_RESPONSES) |
272 | 288 | async def criar_uma_agenda(nome_agenda: str, uid_do_responsavel: str, api_key: str = Depends(get_api_key)): |
273 | | - if check_uid_exists(uid_do_responsavel): |
274 | | - uid = str(uuid.uuid4()) |
275 | | - agenda_ref.update({ |
276 | | - uid: { |
277 | | - 'nome_agenda': nome_agenda, |
278 | | - 'uid_do_responsável': uid_do_responsavel, |
279 | | - 'chave_de_convite': generate_random_invite_key() |
280 | | - } |
281 | | - }) |
282 | | - |
283 | | - return {"message": f'A agenda {nome_agenda} com o UID {uid} foi criada com sucesso'} |
284 | | - else: |
| 289 | + if not check_uid_exists(uid_do_responsavel): |
285 | 290 | raise HTTPException(status_code=401, detail="Este usuário não existe no banco de dados") |
286 | 291 |
|
| 292 | + uid_da_agenda = str(uuid.uuid4()) |
| 293 | + agenda_ref.update({ |
| 294 | + uid_da_agenda: { |
| 295 | + 'nome_agenda': nome_agenda, |
| 296 | + 'chave_de_convite': generate_random_invite_key() |
| 297 | + } |
| 298 | + }) |
| 299 | + |
| 300 | + agenda_membros_ref.child(uid_do_responsavel).update({ |
| 301 | + uid_da_agenda: { |
| 302 | + "role": "admin" |
| 303 | + } |
| 304 | + }) |
| 305 | + |
| 306 | + return {"message": f'A agenda {nome_agenda} com o UID {uid_da_agenda} foi criada com sucesso'} |
| 307 | + |
| 308 | + |
287 | 309 | @app.post("/add/agenda/membro", tags=["Agenda"], responses=STANDARD_RESPONSES) |
288 | 310 | async def adicionar_um_membro_na_agenda_já_criada(uid_da_agenda: str, uid_do_membro: str, api_key: str = Depends(get_api_key)): |
289 | 311 | agenda_node = agenda_ref.child(uid_da_agenda) |
290 | 312 | agenda_data = agenda_node.get() |
| 313 | + |
291 | 314 | if not agenda_data: |
292 | 315 | raise HTTPException(status_code=404, detail=f"A agenda com o UID {uid_da_agenda} não existe") |
293 | 316 |
|
294 | 317 | if not check_uid_exists(uid_do_membro): |
295 | | - raise HTTPException(status_code=400, detail="Este usuário não existe no banco de dados") |
| 318 | + raise HTTPException(status_code=401, detail="Este usuário não existe no banco de dados") |
296 | 319 |
|
297 | 320 | user = auth.get_user(uid_do_membro) |
298 | 321 |
|
299 | | - membro_ref = agenda_node.child("membros") |
300 | | - membro_ref.update({ |
301 | | - user.uid: { |
302 | | - 'nome_do_usuário': user.display_name |
| 322 | + agenda_membros_ref.child(user.uid).update({ |
| 323 | + uid_da_agenda: { |
| 324 | + "role": "user" |
303 | 325 | } |
304 | 326 | }) |
305 | 327 |
|
306 | | - return {"message": f'O membro {user.display} com o UID {user.uid} foi adicionado com sucesso'} |
| 328 | + return {"message": f'O membro {user.display_name} com o UID {user.uid} foi adicionado com sucesso na agenda {agenda_data["nome_agenda"]}'} |
| 329 | + |
307 | 330 |
|
308 | 331 | @app.post("/add/agenda/materia", tags=["Agenda"], responses=STANDARD_RESPONSES) |
309 | 332 | async def criar_uma_materia_na_agenda_já_criada(uid_da_agenda: str, nome_da_matéria: str, nome_do_professor: str | None = None, horario_de_inicio_da_materia: str | None = None, horario_de_fim_da_materia: str | None = None, api_key: str = Depends(get_api_key)): |
@@ -372,6 +395,17 @@ async def deletar_uma_agenda_com_o_uid(uid_da_agenda: str, api_key: str = Depend |
372 | 395 |
|
373 | 396 | return {"message": f'A agenda com o UID {uid_da_agenda} foi deletada com sucesso.'} |
374 | 397 |
|
| 398 | +@app.delete("/delete/agenda/membro", tags=["Agenda"], responses=STANDARD_RESPONSES) |
| 399 | +async def deletar_um_membro_na_agenda(uid_da_agenda: str, uid_do_membro: str, api_key: str = Depends(get_api_key)): |
| 400 | + membro_node = agenda_membros_ref.child(uid_do_membro).child(uid_da_agenda) |
| 401 | + membro_data = membro_node.get() |
| 402 | + if not membro_data: |
| 403 | + raise HTTPException(status_code=404, detail=f"Esse usuário com o UID {uid_do_membro} não pertence a agenda com o UID {uid_da_agenda}") |
| 404 | + |
| 405 | + membro_node.delete() |
| 406 | + |
| 407 | + return {"message": f'O membro com o UID {uid_do_membro} foi removido da agenda com o UID {uid_da_agenda}'} |
| 408 | + |
375 | 409 | @app.delete("/delete/agenda/materia", tags=["Agenda"], responses=STANDARD_RESPONSES) |
376 | 410 | async def deletar_uma_materia_com_o_uid(uid_da_agenda: str, uid_da_materia: str, api_key: str = Depends(get_api_key)): |
377 | 411 | matéria_node = agenda_ref.child(uid_da_agenda).child("matérias").child(uid_da_materia) |
|
0 commit comments