Skip to content

Commit

Permalink
Adiciona a funcao de remover dos favoritos
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroGusta committed Aug 4, 2024
1 parent 0b3b42b commit 650d666
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/controller/savedVideosController.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ def add_to_favorite(favorite: savedVideosSchema.FavoriteCreate, db: Session = De
def check_favorite(video_id: str, user_id: str = Query(...), db: Session = Depends(get_db)):
status = savedVideosRepository.check_favorite_status(db=db, video_id=video_id, user_id=user_id)
return status

@favorite.delete("/{video_id}")
def remove_from_favorites(video_id: str, user_id: str = Query(...), db: Session = Depends(get_db)):
user_id = user_id.strip()
video_id = video_id.strip()
savedVideosRepository.remove_favorite(db=db, video_id=video_id, user_id=user_id)
return {"message": "Removed from favorites"}
18 changes: 18 additions & 0 deletions src/repository/savedVideosRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,21 @@ def check_favorite_status(db: Session, video_id: str, user_id: str) -> dict:
return {
"statusfavorite": False
}
def remove_favorite(db: Session, video_id: str, user_id: str):
video_id = video_id.strip()
user_id = user_id.strip()
print(f"Removing favorite video_id={video_id} for user_id={user_id}")
favorite_entry = db.query(WatchLater).filter(
WatchLater.video_id == video_id,
WatchLater.user_id == user_id,
WatchLater.statusfavorite == True
).first()
print(f"Query Result: {favorite_entry}")
if favorite_entry:
db.delete(favorite_entry)
db.commit()
print(f"Removed Favorite: user_id={user_id}, video_id={video_id}")
return {"message": "Removed from favorites"}
else:
raise HTTPException(status_code=404, detail="Video not found in favorites")

12 changes: 11 additions & 1 deletion tests/test_favorite.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ def test_check_favorite(setup_database):
print(response.json())
assert response.status_code == 200
assert response.json()["statusfavorite"] is True


def test_remove_from_favorites(setup_database):
response = client.delete("/api/favorite/video123?user_id=user123")
assert response.status_code == 200
assert response.json()["message"] == "Removed from favorites"


# Check status again to ensure it's removed
response = client.get("/api/favorite/status/video123?user_id=user123")
assert response.status_code == 200
assert response.json()["statusfavorite"] is False

0 comments on commit 650d666

Please sign in to comment.