Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

23 excluir historico #9

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/controller/recordController.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,29 @@ def get_record_sorted(user_id: str = Query(...), ascending: bool = Query(True),
sorted_videos = dict(sorted(aggregated_videos.items(), key=lambda item: item[1], reverse=not ascending))
return {"videos": sorted_videos}
return {"videos": []}

@Record.delete("/delete_video")
def delete_video_from_record(video_id: str = Query(...), user_id: str = Query(...), db: Session = Depends(get_db)):
record_entry = db.query(recordModel.Record).filter(recordModel.Record.user_id == user_id).first()

if record_entry:
video_id = str(video_id) # video_id como uma string

print(f"Histórico antes da exclusão: {record_entry.videos}")

if video_id in record_entry.videos:
print(f"Removendo vídeo {video_id} do histórico.")

# cria um novo dicionario sem o video a ser removido
updated_videos = {k: v for k, v in record_entry.videos.items() if k != video_id}
record_entry.videos = updated_videos # atualiza o dicionário de videos

db.commit()
print(f"Histórico após a exclusão: {record_entry.videos}")
return JSONResponse(status_code=200, content={"message": "Vídeo removido do histórico com sucesso."})
else:
print(f"Vídeo {video_id} não encontrado no histórico.")
return JSONResponse(status_code=404, content={"message": "Vídeo não encontrado no histórico."})
else:
print(f"Registro de histórico para o usuário {user_id} não encontrado.")
return JSONResponse(status_code=404, content={"message": "Registro de histórico não encontrado."})
24 changes: 24 additions & 0 deletions tests/test_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,27 @@ def test_get_record_sorted(setup_database):
expected_order = sorted(videos.items(), key=lambda x: x[1], reverse=True)
sorted_keys = list(sorted_videos.keys())
assert sorted_keys == [k for k, v in expected_order] # Checa se está em ordem descendente

def test_delete_video_from_record(setup_database):
user_id = str(uuid.uuid4())
video_id = str(uuid.uuid4())
timestamp = "2024-08-14 12:00:00"

# Adiciona o vídeo ao histórico
response = client.post("/api/record/", json={"user_id": user_id, "videos": {video_id: timestamp}})
assert response.status_code == 200

# Verifica se o vídeo foi adicionado ao histórico
response = client.get("/api/record/get_record/", params={"user_id": user_id})
assert response.status_code == 200
assert video_id in response.json()["videos"]

# Exclui o vídeo do histórico
response = client.delete("/api/record/delete_video", params={"user_id": user_id, "video_id": video_id})
assert response.status_code == 200
assert response.json()["message"] == "Vídeo removido do histórico com sucesso."

# Verifica se o vídeo foi removido do histórico
response = client.get("/api/record/get_record/", params={"user_id": user_id})
assert response.status_code == 200
assert video_id not in response.json()["videos"]
Loading