@@ -4,8 +4,36 @@ import { db } from '@app/backend-shared';
44
55const singlesRouter = Router ( ) ;
66
7- singlesRouter . get ( '/:id' , async ( req : Request , res ) => {
8- const singleId = Number ( req . params . id ) ;
7+ function 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+ ] ) ;
31+ }
32+ export type Single = Awaited <
33+ ReturnType < ReturnType < typeof getSingles > [ 'execute' ] >
34+ > [ number ] ;
35+
36+ singlesRouter . get ( '/' , async ( req : Request , res ) => {
937 const userId = req . userId ;
1038 if ( userId === undefined ) {
1139 res . json ( {
@@ -15,30 +43,7 @@ singlesRouter.get('/:id', async (req: Request, res) => {
1543 }
1644
1745 try {
18- const singles = await db
19- . selectFrom ( 'singles' )
20- . leftJoin ( 'artists_hired' , 'singles.artists_hired_id' , 'artists_hired.id' )
21- . leftJoin (
22- 'label_artists' ,
23- 'label_artists.artists_hired_id' ,
24- 'artists_hired.id' ,
25- )
26- . leftJoin ( 'labels' , 'labels.id' , 'label_artists.label_id' )
27- . leftJoin ( 'users' , 'users.id' , 'labels.users_id' )
28- . leftJoin ( 'artists' , 'artists.id' , 'artists_hired.artists_id' )
29- . where ( 'singles.id' , '=' , singleId )
30- . where ( 'labels.users_id' , '=' , userId )
31- . select ( [
32- 'singles.artists_hired_id' ,
33- 'singles.name' ,
34- 'singles.listeners' ,
35- 'singles.money_earned' ,
36- 'singles.score' ,
37- 'artists.firstname as artist_firstname' ,
38- 'artists.lastname as artist_lastname' ,
39- 'artists.alias as artist_alias' ,
40- ] )
41- . execute ( ) ;
46+ const singles = await getSingles ( userId ) . execute ( ) ;
4247
4348 res . json ( singles ) ;
4449 return ;
@@ -48,7 +53,30 @@ singlesRouter.get('/:id', async (req: Request, res) => {
4853 }
4954} ) ;
5055
51- singlesRouter . get ( '/' , async ( req : Request , res ) => {
56+ singlesRouter . get ( '/filter' , async ( req : Request , res ) => {
57+ const userId = req . userId ;
58+ if ( userId === undefined ) {
59+ res . json ( {
60+ ok : false ,
61+ } ) ;
62+ return ;
63+ }
64+
65+ try {
66+ const singles = await getSingles ( userId )
67+ . orderBy ( 'id' , 'desc' )
68+ . executeTakeFirst ( ) ;
69+
70+ res . json ( singles ) ;
71+ return ;
72+ } catch ( error ) {
73+ console . error ( 'Error fetching singles:' , error ) ;
74+ res . status ( 500 ) . json ( { error : 'Internal Server Error' } ) ;
75+ }
76+ } ) ;
77+
78+ singlesRouter . get ( '/:id' , async ( req : Request , res ) => {
79+ const singleId = Number ( req . params . id ) ;
5280 const userId = req . userId ;
5381 if ( userId === undefined ) {
5482 res . json ( {
@@ -69,8 +97,9 @@ singlesRouter.get('/', async (req: Request, res) => {
6997 . leftJoin ( 'labels' , 'labels.id' , 'label_artists.label_id' )
7098 . leftJoin ( 'users' , 'users.id' , 'labels.users_id' )
7199 . leftJoin ( 'artists' , 'artists.id' , 'artists_hired.artists_id' )
100+ . where ( 'singles.id' , '=' , singleId )
101+ . where ( 'labels.users_id' , '=' , userId )
72102 . select ( [
73- 'singles.id as id' ,
74103 'singles.artists_hired_id' ,
75104 'singles.name' ,
76105 'singles.listeners' ,
@@ -80,7 +109,6 @@ singlesRouter.get('/', async (req: Request, res) => {
80109 'artists.lastname as artist_lastname' ,
81110 'artists.alias as artist_alias' ,
82111 ] )
83- . where ( 'labels.users_id' , '=' , userId )
84112 . execute ( ) ;
85113
86114 res . json ( singles ) ;
@@ -90,7 +118,6 @@ singlesRouter.get('/', async (req: Request, res) => {
90118 res . status ( 500 ) . json ( { error : 'Internal Server Error' } ) ;
91119 }
92120} ) ;
93-
94121singlesRouter . post ( '/' , async ( req : Request , res ) => {
95122 const { artistHiredId, singleName, genreId, price } = req . body ;
96123
0 commit comments