fix: insert TrackVersion alongside Track

This commit is contained in:
Roman Godmaire 2023-11-19 20:21:29 -05:00
parent 1f19aa4f0f
commit 3c9d9b40cc
3 changed files with 32 additions and 6 deletions

View file

@ -1,12 +1,14 @@
import type { Track } from '@prisma/client'; import type { Track, TrackVersion } from '@prisma/client';
export interface Database { export interface Database {
listTracksWithProducer: (producerId: string) => Promise< listTracksWithProducer: (producerId: string) => Promise<
{ {
title: string; title: string;
producer: { username: string }; version: number;
producer: string;
}[] }[]
>; >;
insertTrack: (producerId: string, title: string, objectKey: string) => Promise<Track>; createTrack: (producerId: string, title: string, objectKey: string) => Promise<Track>;
insertTrackVersion: (trackId: number) => Promise<TrackVersion>;
} }

View file

@ -16,6 +16,11 @@ export class DatabasePrisma implements Database {
select: { select: {
username: true username: true
} }
},
_count: {
select: {
versions: true
}
} }
}, },
where: { where: {
@ -23,10 +28,18 @@ export class DatabasePrisma implements Database {
} }
}); });
return tracks; const res = tracks.map((track) => {
return {
title: track.title,
version: track._count.versions,
producer: track.producer.username
};
});
return res;
}; };
insertTrack = async (producerId: string, title: string, objectKey: string) => { createTrack = async (producerId: string, title: string, objectKey: string) => {
const track = await this.client.track.create({ const track = await this.client.track.create({
data: { data: {
title, title,
@ -37,4 +50,14 @@ export class DatabasePrisma implements Database {
return track; return track;
}; };
insertTrackVersion = async (trackId: number) => {
const trackVersion = await this.client.trackVersion.create({
data: {
trackId
}
});
return trackVersion;
};
} }

View file

@ -30,7 +30,8 @@ export const actions: Actions = {
}); });
} }
await database.insertTrack(producerId, title, objectKey); const track = await database.createTrack(producerId, title, objectKey);
await database.insertTrackVersion(track.id);
throw redirect(302, '/'); throw redirect(302, '/');
} }