From 28716eeab25720e2b482219adbeece39034f56c9 Mon Sep 17 00:00:00 2001 From: danis Date: Wed, 5 Jul 2023 09:26:45 +0200 Subject: [PATCH] init genreDetailsView --- back/src/song/song.controller.ts | 6 ++++++ back/src/song/song.service.ts | 8 ++++++++ front/API.ts | 30 ++++++++++++++++++++++++++---- front/views/ArtistDetailsView.tsx | 20 ++++++-------------- front/views/GenreDetailsView.tsx | 14 ++++++++++++++ 5 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 front/views/GenreDetailsView.tsx diff --git a/back/src/song/song.controller.ts b/back/src/song/song.controller.ts index a80a31b..be725ed 100644 --- a/back/src/song/song.controller.ts +++ b/back/src/song/song.controller.ts @@ -146,4 +146,10 @@ export class SongController { songId: id, }); } + + @Get('/artist/:artistId') + async getSongByArtist(@Param('artistId', ParseIntPipe) artistId: number) { + const res = await this.songService.songByArtist(artistId) + return res; + } } diff --git a/back/src/song/song.service.ts b/back/src/song/song.service.ts index 4a7a285..de7aa02 100644 --- a/back/src/song/song.service.ts +++ b/back/src/song/song.service.ts @@ -6,6 +6,14 @@ import { PrismaService } from 'src/prisma/prisma.service'; export class SongService { constructor(private prisma: PrismaService) {} + async songByArtist(data: number): Promise { + return this.prisma.song.findMany({ + where: { + artistId: {equals: data}, + }, + }); + } + async createSong(data: Prisma.SongCreateInput): Promise { return this.prisma.song.create({ data, diff --git a/front/API.ts b/front/API.ts index b75cc0a..5e30e63 100644 --- a/front/API.ts +++ b/front/API.ts @@ -278,10 +278,32 @@ export default class API { * @param artistId is the id of the artist that composed the songs aimed * @returns a Promise of Songs type array */ - public static async getSongsByArtist(artistId: number): Promise { - return API.fetch({ - route: `/song?artistId=${artistId}`, - }); + public static getSongsByArtist(artistId: string): Query { + return { + key: ['songs', artistId], + exec: async () => { + const songs = await API.fetch({ + route: `/song/artist/${artistId}`, + }); + + // this is a dummy illustration, we will need to fetch the real one from the API + return songs.data.map( + // To be fixed with #168 + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (song: any) => + ({ + id: song.id as number, + name: song.name as string, + artistId: song.artistId as number, + albumId: song.albumId as number, + genreId: song.genreId as number, + details: song.difficulties, + cover: `${baseAPIUrl}/song/${song.id}/illustration`, + metrics: {}, + } as Song) + ); + }, + }; } /** diff --git a/front/views/ArtistDetailsView.tsx b/front/views/ArtistDetailsView.tsx index 2e5e088..9b07677 100644 --- a/front/views/ArtistDetailsView.tsx +++ b/front/views/ArtistDetailsView.tsx @@ -10,25 +10,17 @@ import { Key, useEffect, useState } from 'react'; import { useNavigation } from '../Navigation'; const ArtistDetailsView = ({ artistId }: any) => { - const { isLoading, data: artistData, isError } = useQuery(API.getArtist(artistId)); - // const { isLoading: isLoadingSongs, data: songData = [], error: errorSongs } = useQuery(['songs', artistId], () => API.getSongsByArtist(artistId)) + const { isLoading: isLoadingArt, data: artistData, error: isErrorArt } = useQuery(API.getArtist(artistId)); + const { isLoading: isLoadingSong, data: songData = [], error: isErrorSong } = useQuery(API.getSongsByArtist(artistId)); const screenSize = useBreakpointValue({ base: "small", md: "big" }); const isMobileView = screenSize == "small"; const navigation = useNavigation(); - const [merde, setMerde] = useState(null); - useEffect(() => { - // Code to be executed when the component is focused - console.warn('Component focused!'); - setMerde(API.getSongsByArtist(112)); - // Call your function or perform any other actions here - }, []); - - if (isLoading) { + if (isLoadingArt) { return ; } - if (isError) { + if (isErrorArt) { navigation.navigate('Error'); } @@ -44,9 +36,9 @@ const ArtistDetailsView = ({ artistId }: any) => { resizeMode='cover' /> - Abba + {artistData?.name} - {merde.map((comp: Song | SongWithArtist, index: Key | null | undefined) => ( + {songData.map((comp: Song | SongWithArtist, index: Key | null | undefined) => ( { + return ( + + + + + + ); +} \ No newline at end of file