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

#228: Rename property and increase stream history length issue-number #281

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def create_artist(name: str, photo: str, password: bytes, current_date: str) ->
"password": password,
"saved_playlists": [],
"playlists": [],
"playback_history": [],
"stream_history": [],
"uploaded_songs": [],
}
result = user_collection_provider.get_artist_collection().insert_one(artist)
Expand Down
4 changes: 2 additions & 2 deletions Backend/app/spotify_electron/user/artist/artist_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_artist_dao_from_document(document: dict[str, Any]) -> ArtistDAO:
photo=document["photo"],
register_date=document["register_date"][:-1],
password=document["password"],
playback_history=document["playback_history"],
stream_history=document["stream_history"],
playlists=document["playlists"],
saved_playlists=document["saved_playlists"],
uploaded_songs=document["uploaded_songs"],
Expand All @@ -65,7 +65,7 @@ def get_artist_dto_from_dao(artist_dao: ArtistDAO) -> ArtistDTO:
return ArtistDTO(
name=artist_dao.name,
photo=artist_dao.photo,
playback_history=artist_dao.playback_history,
stream_history=artist_dao.stream_history,
playlists=artist_dao.playlists,
register_date=artist_dao.register_date,
saved_playlists=artist_dao.saved_playlists,
Expand Down
22 changes: 11 additions & 11 deletions Backend/app/spotify_electron/user/base_user_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,18 @@ def search_by_name(name: str, collection: Collection) -> list[str]:
raise UserRepositoryException from exception


def add_playback_history(
def add_stream_history(
user_name: str,
song: str,
max_number_playback_history_songs: int,
max_number_stream_history_songs: int,
collection: Collection,
) -> None:
"""Add song playback history to user

Args:
user_name (str): user name
song (str): song name
max_number_playback_history_songs (int): max number of songs stored in playback history
max_number_stream_history_songs (int): max number of songs stored in playback history
collection (Collection): the user collection

Raises:
Expand All @@ -149,15 +149,15 @@ def add_playback_history(
try:
user_data = collection.find_one({"name": user_name})

playback_history = user_data["playback_history"] # type: ignore
stream_history = user_data["stream_history"] # type: ignore

if len(playback_history) == max_number_playback_history_songs:
playback_history.pop(0)
if len(stream_history) == max_number_stream_history_songs:
stream_history.pop(0)

playback_history.append(song)
stream_history.append(song)

collection.update_one(
{"name": user_name}, {"$set": {"playback_history": playback_history}}
{"name": user_name}, {"$set": {"stream_history": stream_history}}
)
except Exception as exception:
base_user_repository_logger.exception(
Expand Down Expand Up @@ -350,7 +350,7 @@ def get_user_playlist_names(user_name: str, collection: Collection) -> list[str]
return user_data["playlists"] # type: ignore


def get_user_playback_history_names(user_name: str, collection: Collection) -> list[str]:
def get_user_stream_history_names(user_name: str, collection: Collection) -> list[str]:
"""Get user playback history song names

Args:
Expand All @@ -360,6 +360,6 @@ def get_user_playback_history_names(user_name: str, collection: Collection) -> l
Returns:
list[str]: the user playback history
"""
user_data = collection.find_one({"name": user_name}, {"playback_history": 1, "_id": 0})
user_data = collection.find_one({"name": user_name}, {"stream_history": 1, "_id": 0})

return user_data["playback_history"] # type: ignore
return user_data["stream_history"] # type: ignore
14 changes: 7 additions & 7 deletions Backend/app/spotify_electron/user/base_user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@


# TODO not hardcoded
MAX_NUMBER_PLAYBACK_HISTORY_SONGS = 5
MAX_NUMBER_PLAYBACK_HISTORY_SONGS = 50
telepcak marked this conversation as resolved.
Show resolved Hide resolved


def get_user_type(user_name: str) -> UserType:
Expand Down Expand Up @@ -148,7 +148,7 @@ def get_user_password(user_name: str) -> bytes:
return password


def add_playback_history(user_name: str, song_name: str, token: TokenData) -> None:
def add_stream_history(user_name: str, song_name: str, token: TokenData) -> None:
"""Add playback history to user

Args:
Expand All @@ -173,10 +173,10 @@ def add_playback_history(user_name: str, song_name: str, token: TokenData) -> No
base_user_service_validations.validate_user_should_exists(user_name)
validate_song_should_exists(song_name)

base_user_repository.add_playback_history(
base_user_repository.add_stream_history(
user_name=user_name,
song=song_name,
max_number_playback_history_songs=MAX_NUMBER_PLAYBACK_HISTORY_SONGS,
max_number_stream_history_songs=MAX_NUMBER_PLAYBACK_HISTORY_SONGS,
collection=user_collection_provider.get_user_associated_collection(user_name),
)
except UserBadNameException as exception:
Expand Down Expand Up @@ -577,7 +577,7 @@ def get_user_playlist_names(user_name: str) -> list[str]:
return user_playlist_names


def get_user_playback_history(user_name: str) -> list[SongMetadataDTO]:
def get_user_stream_history(user_name: str) -> list[SongMetadataDTO]:
"""Get user song playback history

Args:
Expand All @@ -595,10 +595,10 @@ def get_user_playback_history(user_name: str) -> list[SongMetadataDTO]:
base_user_service_validations.validate_user_name_parameter(user_name)
base_user_service_validations.validate_user_should_exists(user_name)
collection = user_collection_provider.get_user_associated_collection(user_name)
playback_history_names = base_user_repository.get_user_playback_history_names(
stream_history_names = base_user_repository.get_user_stream_history_names(
user_name=user_name, collection=collection
)
songs_metadata = base_song_service.get_songs_metadata(playback_history_names)
songs_metadata = base_song_service.get_songs_metadata(stream_history_names)

except UserBadNameException as exception:
base_users_service_logger.exception(f"Bad user Parameter: {user_name}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def create_user(name: str, photo: str, password: bytes, current_date: str) -> No
"password": password,
"saved_playlists": [],
"playlists": [],
"playback_history": [],
"stream_history": [],
}
result = user_collection_provider.get_user_collection().insert_one(user)

Expand Down
8 changes: 4 additions & 4 deletions Backend/app/spotify_electron/user/user/user_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class UserDAO:
photo: str
register_date: str
password: bytes
playback_history: list[str]
stream_history: list[str]
playlists: list[str]
saved_playlists: list[str]

Expand All @@ -29,7 +29,7 @@ class UserDTO:
name: str
photo: str
register_date: str
playback_history: list[str]
stream_history: list[str]
playlists: list[str]
saved_playlists: list[str]

Expand Down Expand Up @@ -58,7 +58,7 @@ def get_user_dao_from_document(document: dict[str, Any]) -> UserDAO:
photo=document["photo"],
register_date=document["register_date"][:-1],
password=document["password"],
playback_history=document["playback_history"],
stream_history=document["stream_history"],
playlists=document["playlists"],
saved_playlists=document["saved_playlists"],
)
Expand All @@ -79,7 +79,7 @@ def get_user_dto_from_dao(user_dao: UserDAO) -> UserDTO:
return UserDTO(
name=user_dao.name,
photo=user_dao.photo,
playback_history=user_dao.playback_history,
stream_history=user_dao.stream_history,
playlists=user_dao.playlists,
register_date=user_dao.register_date,
saved_playlists=user_dao.saved_playlists,
Expand Down
18 changes: 8 additions & 10 deletions Backend/app/spotify_electron/user/user_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ def delete_user(name: str) -> Response:
)


@router.patch("/{name}/playback_history")
def patch_playback_history(
@router.patch("/{name}/stream_history")
def patch_stream_history(
name: str, song_name: str, token: Annotated[TokenData, Depends(JWTBearer())]
) -> Response:
"""Add song to playback history
telepcak marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -176,9 +176,7 @@ def patch_playback_history(
song_name (str): song name
"""
try:
base_user_service.add_playback_history(
user_name=name, song_name=song_name, token=token
)
base_user_service.add_stream_history(user_name=name, song_name=song_name, token=token)
return Response(None, HTTP_204_NO_CONTENT)
except UserBadNameException:
return Response(
Expand Down Expand Up @@ -422,8 +420,8 @@ def get_user_playlists_names(
)


@router.get("/{name}/playback_history")
def get_user_playback_history(
@router.get("/{name}/stream_history")
def get_user_stream_history(
name: str, token: Annotated[TokenData, Depends(JWTBearer())]
) -> Response:
"""Get user song playback history
telepcak marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -432,10 +430,10 @@ def get_user_playback_history(
name (str): user name
"""
try:
playback_history = base_user_service.get_user_playback_history(name)
playback_history_json = json_converter_utils.get_json_from_model(playback_history)
stream_history = base_user_service.get_user_stream_history(name)
stream_history_json = json_converter_utils.get_json_from_model(stream_history)
return Response(
playback_history_json, media_type="application/json", status_code=HTTP_200_OK
stream_history_json, media_type="application/json", status_code=HTTP_200_OK
)
except UserBadNameException:
return Response(
Expand Down
6 changes: 3 additions & 3 deletions Backend/tests/test_API/api_base_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def patch_history_playback(
user_name: str, song_name: str, headers: dict[str, str]
) -> Response:
return client.patch(
f"/users/{user_name}/playback_history/?song_name={song_name}", headers=headers
f"/users/{user_name}/stream_history/?song_name={song_name}", headers=headers
telepcak marked this conversation as resolved.
Show resolved Hide resolved
)


Expand Down Expand Up @@ -50,5 +50,5 @@ def get_user_playlists(name: str, headers: dict[str, str]) -> Response:
return client.get(f"/users/{name}/playlists", headers=headers)


def get_user_playback_history(user_name: str, headers: dict[str, str]) -> Response:
return client.get(f"/users/{user_name}/playback_history", headers=headers)
def get_user_stream_history(user_name: str, headers: dict[str, str]) -> Response:
return client.get(f"/users/{user_name}/stream_history", headers=headers)
4 changes: 2 additions & 2 deletions Backend/tests/test_API/api_test_artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ def update_artist( # noqa: PLR0913
photo: str,
playlists: list[str],
saved_playlists: list[str],
playback_history: list[str],
stream_history: list[str],
uploaded_songs: list[str],
headers: dict[str, str],
) -> Response:
url = f"/artists/{name}/?photo={photo}"

payload = {
"playback_history": playback_history,
"stream_history": stream_history,
"playlists": playlists,
"saved_playlists": saved_playlists,
"uploaded_songs": uploaded_songs,
Expand Down
4 changes: 2 additions & 2 deletions Backend/tests/test_API/api_test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ def update_user( # noqa: PLR0913
photo: str,
playlists: list[str],
saved_playlists: list[str],
playback_history: list[str],
stream_history: list[str],
headers: dict[str, str],
) -> Response:
url = f"/users/{name}/?photo={photo}"

payload = {
"playback_history": playback_history,
"stream_history": stream_history,
"playlists": playlists,
"saved_playlists": saved_playlists,
}
Expand Down
Loading