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 7 commits into
base: master
Choose a base branch
from
Open
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
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
32 changes: 16 additions & 16 deletions Backend/app/spotify_electron/user/base_user_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,39 +129,39 @@ 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
"""Add song stream 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 stream history
collection (Collection): the user collection

Raises:
UserRepositoryException: unexpected error adding song to user playback history
UserRepositoryException: unexpected error adding song to user stream 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(
f"Error adding playback history of song {song} to user {user_name} in database"
f"Error adding stream history of song {song} to user {user_name} in database"
)
raise UserRepositoryException from exception

Expand Down Expand Up @@ -350,16 +350,16 @@ 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]:
"""Get user playback history song names
def get_user_stream_history_names(user_name: str, collection: Collection) -> list[str]:
"""Get user stream history song names

Args:
user_name (str): user name
collection (Collection): user collection

Returns:
list[str]: the user playback history
list[str]: the user stream 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
38 changes: 19 additions & 19 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_STREAM_HISTORY_SONGS = 50


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


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

Args:
user_name (str): user name
Expand All @@ -160,10 +160,10 @@ def add_playback_history(user_name: str, song_name: str, token: TokenData) -> No
UserBadNameException: invalid user name
UserNotFoundException: user doesn't exists
SongBadNameException: invalid song name
UserUnauthorizedException: user cannot modify playback history that \
UserUnauthorizedException: user cannot modify stream history that \
is not created by him
SongNotFoundException: song doesn't exists
UserServiceException: unexpected error adding playback history to user
UserServiceException: unexpected error adding stream history to user
"""
try:
base_user_service_validations.validate_user_name_parameter(user_name)
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_STREAM_HISTORY_SONGS,
collection=user_collection_provider.get_user_associated_collection(user_name),
)
except UserBadNameException as exception:
Expand All @@ -190,7 +190,7 @@ def add_playback_history(user_name: str, song_name: str, token: TokenData) -> No
raise SongBadNameException from exception
except UserUnauthorizedException as exception:
base_users_service_logger.exception(
f"Unathorized user {user_name} for adding playback songs"
f"Unathorized user {user_name} for adding stream songs"
)
raise UserUnauthorizedException from exception
except SongNotFoundException as exception:
Expand All @@ -199,18 +199,18 @@ def add_playback_history(user_name: str, song_name: str, token: TokenData) -> No
except UserRepositoryException as exception:
base_users_service_logger.exception(
f"Unexpected error in User Repository adding song {song_name} "
f"to user {user_name} playback history"
f"to user {user_name} stream history"
)
raise UserServiceException from exception
except Exception as exception:
base_users_service_logger.exception(
f"Unexpected error in User Service adding song {song_name} "
f"to user {user_name} playback history"
f"to user {user_name} stream history"
)
raise UserServiceException from exception
else:
base_users_service_logger.info(
f"Song {song_name} added to User {user_name} playback history"
f"Song {song_name} added to User {user_name} stream history"
)


Expand Down Expand Up @@ -577,28 +577,28 @@ def get_user_playlist_names(user_name: str) -> list[str]:
return user_playlist_names


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

Args:
user_name (str): user name

Raises:
UserBadNameException: invalid user name
UserNotFoundException: user not found
UserServiceException: unexpected error getting playback history from user
UserServiceException: unexpected error getting stream history from user

Returns:
list[str]: the song playback history from user
list[str]: the song stream history from user
"""
try:
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 All @@ -608,13 +608,13 @@ def get_user_playback_history(user_name: str) -> list[SongMetadataDTO]:
raise UserNotFoundException from exception
except UserRepositoryException as exception:
base_users_service_logger.exception(
f"Unexpected error in User Repository getting playback history "
f"Unexpected error in User Repository getting stream history "
f"from owner {user_name}"
)
raise UserServiceException from exception
except Exception as exception:
base_users_service_logger.exception(
f"Unexpected error in User Service getting getting playback history "
f"Unexpected error in User Service getting getting stream history "
f"from owner {user_name}"
)
raise UserServiceException from exception
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
22 changes: 10 additions & 12 deletions Backend/app/spotify_electron/user/user_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,18 @@ 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
"""Add song to stream history

Args:
name (str): user name
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,20 +420,20 @@ 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
"""Get user song stream history

Args:
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
10 changes: 4 additions & 6 deletions Backend/tests/test_API/api_base_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
client = TestClient(app)


def patch_history_playback(
user_name: str, song_name: str, headers: dict[str, str]
) -> Response:
def patch_history_stream(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 +48,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
Loading