@@ -5,29 +5,42 @@ import { db } from '@app/backend-shared';
55const singlesRouter = Router ( ) ;
66
77function getSingles ( userId : number ) {
8- return db
9- . selectFrom ( 'singles' )
10- . leftJoin ( 'artists_hired' , 'singles.artists_hired_id' , 'artists_hired.id' )
11- . leftJoin ( 'artists' , 'artists.id' , 'artists_hired.artists_id' )
12- . leftJoin (
13- 'label_artists' ,
14- 'label_artists.artists_hired_id' ,
15- 'artists_hired.id' ,
16- )
17- . leftJoin ( 'labels' , 'labels.id' , 'label_artists.label_id' )
18- . leftJoin ( 'users' , 'users.id' , 'labels.users_id' )
19- . where ( 'labels.users_id' , '=' , userId )
20- . select ( [
21- 'singles.id' ,
22- 'singles.artists_hired_id' ,
23- 'singles.name as name' ,
24- 'singles.listeners' ,
25- 'singles.money_earned' ,
26- 'singles.score' ,
27- 'artists.firstname as artist_firstname' ,
28- 'artists.lastname as artist_lastname' ,
29- 'artists.alias as artist_alias' ,
30- ] ) ;
8+ return (
9+ db
10+ . selectFrom ( 'singles' )
11+ . leftJoin ( 'artists_hired' , 'singles.artists_hired_id' , 'artists_hired.id' )
12+ . leftJoin ( 'artists' , 'artists.id' , 'artists_hired.artists_id' )
13+ . leftJoin (
14+ 'label_artists' ,
15+ 'label_artists.artists_hired_id' ,
16+ 'artists_hired.id' ,
17+ )
18+ . leftJoin ( 'labels' , 'labels.id' , 'label_artists.label_id' )
19+ . leftJoin ( 'users' , 'users.id' , 'labels.users_id' )
20+ // .where('labels.users_id', '=', userId)
21+ . select ( [
22+ 'singles.id' ,
23+ 'singles.artists_hired_id' ,
24+ 'singles.name as name' ,
25+ 'singles.listeners' ,
26+ 'singles.money_earned' ,
27+ 'singles.score' ,
28+ 'artists.firstname as artist_firstname' ,
29+ 'artists.lastname as artist_lastname' ,
30+ 'artists.alias as artist_alias' ,
31+ ] )
32+ . where ( ( eb ) =>
33+ eb . not (
34+ eb . exists (
35+ eb
36+ . selectFrom ( 'singles_albums' )
37+ . select ( 'singles_albums.id' )
38+ . whereRef ( 'singles_albums.singles_id' , '=' , 'singles.id' )
39+ . where ( 'labels.users_id' , '=' , userId ) ,
40+ ) ,
41+ ) ,
42+ )
43+ ) ;
3144}
3245export type Single = Awaited <
3346 ReturnType < ReturnType < typeof getSingles > [ 'execute' ] >
@@ -75,49 +88,6 @@ singlesRouter.get('/filter', async (req: Request, res) => {
7588 }
7689} ) ;
7790
78- singlesRouter . get ( '/:id' , async ( req : Request , res ) => {
79- const singleId = Number ( req . params . id ) ;
80- const userId = req . userId ;
81- if ( userId === undefined ) {
82- res . json ( {
83- ok : false ,
84- } ) ;
85- return ;
86- }
87-
88- try {
89- const singles = await db
90- . selectFrom ( 'singles' )
91- . leftJoin ( 'artists_hired' , 'singles.artists_hired_id' , 'artists_hired.id' )
92- . leftJoin (
93- 'label_artists' ,
94- 'label_artists.artists_hired_id' ,
95- 'artists_hired.id' ,
96- )
97- . leftJoin ( 'labels' , 'labels.id' , 'label_artists.label_id' )
98- . leftJoin ( 'users' , 'users.id' , 'labels.users_id' )
99- . leftJoin ( 'artists' , 'artists.id' , 'artists_hired.artists_id' )
100- . where ( 'singles.id' , '=' , singleId )
101- . where ( 'labels.users_id' , '=' , userId )
102- . select ( [
103- 'singles.artists_hired_id' ,
104- 'singles.name' ,
105- 'singles.listeners' ,
106- 'singles.money_earned' ,
107- 'singles.score' ,
108- 'artists.firstname as artist_firstname' ,
109- 'artists.lastname as artist_lastname' ,
110- 'artists.alias as artist_alias' ,
111- ] )
112- . execute ( ) ;
113-
114- res . json ( singles ) ;
115- return ;
116- } catch ( error ) {
117- console . error ( 'Error fetching singles:' , error ) ;
118- res . status ( 500 ) . json ( { error : 'Internal Server Error' } ) ;
119- }
120- } ) ;
12191singlesRouter . post ( '/' , async ( req : Request , res ) => {
12292 const { artistHiredId, singleName, genreId, price } = req . body ;
12393
0 commit comments