Skip to content

Commit

Permalink
refractor: rename variable, fronend fix to show songs in separate com…
Browse files Browse the repository at this point in the history
…ponent, openapi generated | [AntonioMrtz#228]
  • Loading branch information
Tomáš Telepčák committed Nov 12, 2024
1 parent a1978cd commit c360e6e
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 91 deletions.
12 changes: 6 additions & 6 deletions Backend/app/spotify_electron/user/base_user_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,16 @@ def add_stream_history(
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_stream_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})
Expand All @@ -161,7 +161,7 @@ def add_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 @@ -351,14 +351,14 @@ def get_user_playlist_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
"""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}, {"stream_history": 1, "_id": 0})

Expand Down
16 changes: 8 additions & 8 deletions Backend/app/spotify_electron/user/base_user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def get_user_password(user_name: str) -> bytes:


def add_stream_history(user_name: str, song_name: str, token: TokenData) -> None:
"""Add playback history to user
"""Add stream history to user
Args:
user_name (str): user name
Expand All @@ -160,10 +160,10 @@ def add_stream_history(user_name: str, song_name: str, token: TokenData) -> None
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 Down Expand Up @@ -578,18 +578,18 @@ def get_user_playlist_names(user_name: str) -> list[str]:


def get_user_stream_history(user_name: str) -> list[SongMetadataDTO]:
"""Get user song playback history
"""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)
Expand All @@ -608,13 +608,13 @@ def get_user_stream_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
4 changes: 2 additions & 2 deletions Backend/app/spotify_electron/user/user_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def delete_user(name: str) -> Response:
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
Expand Down Expand Up @@ -424,7 +424,7 @@ def get_user_playlists_names(
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
Expand Down
4 changes: 1 addition & 3 deletions Backend/tests/test_API/api_base_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
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}/stream_history/?song_name={song_name}", headers=headers
)
Expand Down
22 changes: 11 additions & 11 deletions Backend/tests/test__base_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
get_user_playlists,
get_user_relevant_playlists,
get_user_stream_history,
patch_history_playback,
patch_history_stream,
patch_playlist_saved,
whoami,
)
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_patch_stream_history_user_correct(clear_test_data_db):
)
assert res_create_song.status_code == HTTP_201_CREATED

res_patch_user = patch_history_playback(
res_patch_user = patch_history_stream(
user_name=name, song_name=song_name, headers=jwt_headers_user
)
assert res_patch_user.status_code == HTTP_204_NO_CONTENT
Expand Down Expand Up @@ -112,7 +112,7 @@ def test_patch_stream_history_artist_correct(clear_test_data_db):
)
assert res_create_song.status_code == HTTP_201_CREATED

res_patch_user = patch_history_playback(artista, song_name, headers=jwt_headers_artist)
res_patch_user = patch_history_stream(artista, song_name, headers=jwt_headers_artist)
assert res_patch_user.status_code == HTTP_204_NO_CONTENT

res_get_artist = get_artist(name=artista, headers=jwt_headers_artist)
Expand All @@ -127,7 +127,7 @@ def test_patch_stream_history_artist_correct(clear_test_data_db):
assert res_delete_artist.status_code == HTTP_202_ACCEPTED


def test_patch_stream_history_invalid_bad_user():
def test_patch_stream_history_bad_user():
photo = "https://photo"
password = "hola"
artista = "artista"
Expand All @@ -139,19 +139,19 @@ def test_patch_stream_history_invalid_bad_user():

jwt_headers_artist = get_user_jwt_header(username=artista, password=password)

res_patch_user = patch_history_playback("", "", headers=jwt_headers_artist)
res_patch_user = patch_history_stream("", "", headers=jwt_headers_artist)
assert res_patch_user.status_code == HTTP_404_NOT_FOUND

res_delete_artist = delete_user(artista)
assert res_delete_artist.status_code == HTTP_202_ACCEPTED


def test_patch_stream_history_non_existing_user():
res_patch_user = patch_history_playback("usuario1", "cancion1", {})
res_patch_user = patch_history_stream("usuario1", "cancion1", {})
assert res_patch_user.status_code == HTTP_403_FORBIDDEN


def test_patch_stream_history_user_correct_insert_6_songs(clear_test_data_db):
def test_patch_stream_history_user_correct_insert_50_songs(clear_test_data_db):
name = "8232392323623823723"
photo = "https://photo"
password = "hola"
Expand Down Expand Up @@ -191,10 +191,10 @@ def test_patch_stream_history_user_correct_insert_6_songs(clear_test_data_db):
assert res_create_song.status_code == HTTP_201_CREATED

for i in range(0, 49):
res_patch_user = patch_history_playback(name, song_name, headers=jwt_headers_user)
res_patch_user = patch_history_stream(name, song_name, headers=jwt_headers_user)
assert res_patch_user.status_code == HTTP_204_NO_CONTENT

res_patch_user = patch_history_playback(name, new_song_name, headers=jwt_headers_user)
res_patch_user = patch_history_stream(name, new_song_name, headers=jwt_headers_user)
assert res_patch_user.status_code == HTTP_204_NO_CONTENT

res_get_user = get_user(name=name, headers=jwt_headers_user)
Expand Down Expand Up @@ -971,12 +971,12 @@ def test_get_user_stream_history_correct():
)
assert res_create_song.status_code == HTTP_201_CREATED

res_patch_user = patch_history_playback(
res_patch_user = patch_history_stream(
user_name=artist_name, song_name=song_name, headers=jwt_headers_artist
)
assert res_patch_user.status_code == HTTP_204_NO_CONTENT

res_patch_user = patch_history_playback(
res_patch_user = patch_history_stream(
user_name=artist_name, song_name=song_name_2, headers=jwt_headers_artist
)
assert res_patch_user.status_code == HTTP_204_NO_CONTENT
Expand Down
2 changes: 1 addition & 1 deletion Electron/src/__tests__/pages/UserProfile.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ beforeEach(() => {
useNavigate: jest.fn(),
})); */

test('UserProfile User load Playback history and his Playlists', async () => {
test('UserProfile User load Stream history and his Playlists', async () => {
const playlistName = 'playlisttest';
const songName = 'songName';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import SongCard from 'components/Cards/SongCard/SongCard';
import useFetchGetUserStreamHistory from 'hooks/useFetchGetUserPlaybackHistory';
import { saniticeUserName } from 'utils/saniticeParameters';
import { PropsItemsSongsStreamHistory } from '../types/PropsItems';

export default function ItemsAllSongsFromStreamHistory({
refreshSidebarData,
changeSongName,
userName,
}: PropsItemsSongsStreamHistory) {
const { streamHistory } = useFetchGetUserStreamHistory(
saniticeUserName(userName),
);

return (
// eslint-disable-next-line react/jsx-no-useless-fragment
<>
{streamHistory &&
streamHistory.map((songItem, index) => {
return (
<SongCard
// eslint-disable-next-line react/no-array-index-key
key={`${songItem.name}-${index}`}
name={songItem.name}
photo={songItem.photo}
artist={songItem.artist}
changeSongName={changeSongName}
refreshSidebarData={refreshSidebarData}
/>
);
})}
</>
);
}
9 changes: 9 additions & 0 deletions Electron/src/components/ShowAllItems/ShowAllItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PropsAllItems, ShowAllItemsTypes } from './types/PropsShowAllItems';
import ItemsArtist from './Items/ItemsAllArtist';
import ItemsAllPlaylistsFromUser from './Items/ItemsAllPlaylistFromUser';
import ItemsAllSongsFromArtist from './Items/ItemsAllSongsFromArtist';
import ItemsAllSongsFromStreamHistory from './Items/ItemsAllSongsFromStreamHistory';

export default function ShowAllItems({
refreshSidebarData,
Expand All @@ -31,6 +32,14 @@ export default function ShowAllItems({
id={id}
/>
),
[ShowAllItemsTypes.ALL_STREAM_HISTORY_FROM_USER]: (
<ItemsAllSongsFromStreamHistory
userName={user || 'NoUser'}
changeSongName={changeSongName}
refreshSidebarData={refreshSidebarData}
id={id}
/>
),
[ShowAllItemsTypes.ALL_SONGS_FROM_ARTIST]: (
<ItemsAllSongsFromArtist
artistName={artist || 'NoArtist'}
Expand Down
10 changes: 10 additions & 0 deletions Electron/src/components/ShowAllItems/types/PropsItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ export interface PropsItems {
id: string | undefined;
}

export interface PropsItemsSongs extends PropsItems {
refreshSidebarData: () => void;
}

export interface PropsItemsPlaylist extends PropsItems {
refreshSidebarData: () => void;
}

export interface PropsItemsSongsStreamHistory extends PropsItemsSongs {
userName: string;
refreshSidebarData: () => void;
changeSongName: (songName: string) => void;
}

export interface PropsItemsPlaylistsFromUser extends PropsItemsPlaylist {
userName: string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum ShowAllItemsTypes {
ALL_PLAYLISTS = 'all-playlists',
ALL_ARTISTS = 'all-artists',
ALL_PLAYLIST_FROM_USER = 'all-playlists-from-user',
ALL_STREAM_HISTORY_FROM_USER = 'all-stream-history-from-user',
ALL_SONGS_FROM_ARTIST = 'all-songs-from-artist',
SONG = 'song',
}
Expand Down
8 changes: 4 additions & 4 deletions Electron/src/components/footer/Player/PlayerBlob.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ export default function PlayerBlob({
}
};

const handleUpdatePlaybackHistory = async () => {
const handleUpdateStreamHistory = async () => {
const userName = getTokenUsername();

try {
await UsersService.patchPlaybackHistoryUsersNamePlaybackHistoryPatch(
await UsersService.patchStreamHistoryUsersNameStreamHistoryPatch(
userName,
songName,
);
} catch (err) {
console.log(
`Unable to update User ${userName} playback history with Son ${songName}`,
`Unable to update User ${userName} stream history with Son ${songName}`,
);
console.log(err);
}
Expand All @@ -119,7 +119,7 @@ export default function PlayerBlob({
const songData = await SongsService.getSongSongsNameGet(songName);

handleIncreaseSongStreams();
handleUpdatePlaybackHistory();
handleUpdateStreamHistory();

changeSongInfo({
name: songData.name,
Expand Down
8 changes: 4 additions & 4 deletions Electron/src/components/footer/Player/PlayerServerless.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ export default function PlayerServerless({
}
};

const handleUpdatePlaybackHistory = async () => {
const handleUpdateStreamHistory = async () => {
const userName = getTokenUsername();

try {
await UsersService.patchPlaybackHistoryUsersNamePlaybackHistoryPatch(
await UsersService.patchStreamHistoryUsersNameStreamHistoryPatch(
userName,
songName,
);
} catch (err) {
console.log(
`Unable to update User ${userName} playback history with Son ${songName}`,
`Unable to update User ${userName} stream history with Son ${songName}`,
);
console.log(err);
}
Expand All @@ -117,7 +117,7 @@ export default function PlayerServerless({
const songData = await SongsService.getSongSongsNameGet(songName);

handleIncreaseSongStreams();
handleUpdatePlaybackHistory();
handleUpdateStreamHistory();

changeSongInfo({
name: songData.name,
Expand Down
Loading

0 comments on commit c360e6e

Please sign in to comment.