Skip to content
This repository was archived by the owner on Feb 28, 2026. It is now read-only.

Commit 3cfb41d

Browse files
authored
Merge pull request #84 from WildCodeSchool/feature/fix-notoreity
notoriety on artists and label
2 parents c4f3690 + 14a4292 commit 3cfb41d

File tree

3 files changed

+73
-83
lines changed

3 files changed

+73
-83
lines changed

packages/backend/api/src/albums/albums.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,36 @@ albumsRouter.post('/create', async (req: Request, res) => {
175175
)
176176
.execute();
177177

178+
const milestones = await db
179+
.selectFrom('milestones')
180+
.leftJoin('artists_hired', 'artists_hired.milestones_id', 'milestones.id')
181+
.leftJoin(
182+
'label_artists',
183+
'label_artists.artists_hired_id',
184+
'artists_hired.id',
185+
)
186+
.leftJoin('labels', 'labels.id', 'label_artists.label_id')
187+
.select('milestones.value')
188+
.where('labels.users_id', '=', userId)
189+
.executeTakeFirst();
190+
191+
const gain = Number(milestones?.value) / 1000;
192+
193+
const labels = await db
194+
.selectFrom('labels')
195+
.select('labels.notoriety')
196+
.where('labels.users_id', '=', userId)
197+
.executeTakeFirst();
198+
199+
const currentNotoriety = Number(labels?.notoriety);
200+
const newNotoriety = Math.min(Number(currentNotoriety) + Number(gain), 5);
201+
202+
await db
203+
.updateTable('labels')
204+
.set({ notoriety: Number(newNotoriety) })
205+
.where('labels.id', '=', userId)
206+
.execute();
207+
178208
res.status(201).json({ success: true });
179209
return;
180210
} catch (err) {

packages/backend/api/src/singles/singles.ts

Lines changed: 40 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,42 @@ import { db } from '@app/backend-shared';
55
const singlesRouter = Router();
66

77
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-
]);
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
}
3245
export 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-
});
12191
singlesRouter.post('/', async (req: Request, res) => {
12292
const { artistHiredId, singleName, genreId, price, skills } = req.body;
12393
const userId = req.userId;
@@ -186,24 +156,19 @@ singlesRouter.post('/', async (req: Request, res) => {
186156
res.status(400).json({ error: 'No milestone found' });
187157
return;
188158
}
189-
190-
if (artistHired.notoriety >= 5) {
191-
res.json({ message: 'max 5' });
192-
return;
193-
}
159+
const currentNotoriety = Number(artistHired.notoriety);
160+
const newNotoriety = Math.min(Number(currentNotoriety) + Number(gain), 5);
194161

195162
await db
196163
.updateTable('artists_hired')
197-
.set((eb) => ({
198-
notoriety: eb('notoriety', '+', Number(gain)),
199-
}))
164+
.set({ notoriety: newNotoriety })
200165
.where('artists_hired.id', '=', artistHiredId)
201166
.execute();
202167

203168
const newMilestone = await db
204169
.selectFrom('milestones')
205170
.select('id')
206-
.where('value', '<=', Number(artistHired.notoriety) * 10)
171+
.where('value', '<=', Number(newNotoriety) * 10)
207172
.orderBy('id', 'desc')
208173
.limit(1)
209174
.executeTakeFirst();
@@ -213,11 +178,6 @@ singlesRouter.post('/', async (req: Request, res) => {
213178
return;
214179
}
215180

216-
if (newMilestone.id >= 5) {
217-
res.json({ message: 'max 5' });
218-
return;
219-
}
220-
221181
await db
222182
.updateTable('artists_hired')
223183
.set({ milestones_id: newMilestone.id })

packages/frontend/web/src/components/modal-singles.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useState } from 'react';
22

3-
import type { Singles } from '../../../../backend/api/src/singles/singles';
3+
import type { Single } from '../../../../backend/api/src/singles/singles';
44

55
export type ModalSinglesProps = {
66
readonly isOpen: boolean;
@@ -15,13 +15,13 @@ export function ModalSingles({
1515
artistId,
1616
onSelectSingle,
1717
}: ModalSinglesProps) {
18-
const [singles, setSingles] = useState<Singles[]>([]);
18+
const [singles, setSingles] = useState<Single[]>([]);
1919

2020
useEffect(() => {
2121
const fetchSingles = async () => {
2222
try {
2323
const res = await fetch(`/api/singles`);
24-
const data: Singles[] = await res.json();
24+
const data: Single[] = await res.json();
2525

2626
if (artistId != null) {
2727
const filtered = data.filter(

0 commit comments

Comments
 (0)