feat: logic for creating comments

Prisma is still in the TODO state.
This commit is contained in:
Roman Godmaire 2024-02-10 18:20:04 -05:00
parent db8cd9e38f
commit a3d6ea4b69
6 changed files with 170 additions and 2 deletions

View file

@ -41,6 +41,9 @@ export interface Database {
}[]; }[];
}[]; }[];
} | null>; } | null>;
getLatestTrackVersion: (trackId: number) => Promise<{ id: string } | null>;
createTrack: (producerId: string, title: string) => Promise<Track>; createTrack: (producerId: string, title: string) => Promise<Track>;
createTrackVersion: (trackId: number) => Promise<TrackVersion>; createTrackVersion: (trackId: number) => Promise<TrackVersion>;
createComment: (versionId: string, userId: string, content: string) => Promise<null>; createComment: (versionId: string, userId: string, content: string) => Promise<null>;

View file

@ -1,5 +1,7 @@
import type { Database } from '$lib/server/db'; import type { Database } from '$lib/server/db';
import { v4 as uuidv4 } from 'uuid';
// TODO Flesh this out // TODO Flesh this out
export class MockGenericDatabase implements Database { export class MockGenericDatabase implements Database {
fetchHomepageData = async (_producerId: string) => { fetchHomepageData = async (_producerId: string) => {
@ -31,4 +33,12 @@ export class MockGenericDatabase implements Database {
return Promise.resolve(data); return Promise.resolve(data);
}; };
getLatestTrackVersion = async (_trackId: number) => {
return Promise.resolve({ id: '66dbd96c-2c84-4fe6-8a74-eeb3876f6f34' });
};
createComment = async (_versionId: string, _userId: string, _content: string) => {
return Promise.resolve(null);
};
} }

View file

@ -88,6 +88,20 @@ export class DatabasePrisma implements Database {
}); });
}; };
getLatestTrackVersion = async (trackId: number) => {
return await this.client.trackVersion.findFirst({
select: {
id: true
},
where: {
trackId
},
orderBy: {
createdAt: 'desc'
}
});
};
createTrack = async (producerId: string, title: string) => { createTrack = async (producerId: string, title: string) => {
return await this.client.track.create({ return await this.client.track.create({
data: { data: {

View file

@ -1,6 +1,8 @@
import { error, redirect } from '@sveltejs/kit'; import { error, redirect } from '@sveltejs/kit';
import type { Actions, PageServerLoad } from './$types'; import type { Actions, PageServerLoad } from './$types';
import { validate as validateUuid } from 'uuid';
export const load: PageServerLoad = async ({ params: { slug }, locals: { database } }) => { export const load: PageServerLoad = async ({ params: { slug }, locals: { database } }) => {
const trackId = parseInt(slug); const trackId = parseInt(slug);
if (isNaN(trackId)) { if (isNaN(trackId)) {
@ -22,11 +24,28 @@ export const actions: Actions = {
const session = await authReq.validate(); const session = await authReq.validate();
if (!session) redirect(302, '/login'); if (!session) redirect(302, '/login');
const versionId = slug; let versionId;
const re = /[1-9]\d*/;
if (validateUuid(slug)) {
versionId = slug;
} else if (slug.match(re)?.[0] === slug) {
const trackId = parseInt(slug);
const result = await database.getLatestTrackVersion(trackId);
if (result === null) {
error(404, 'Track not found');
}
versionId = result.id;
} else {
error(404, 'Track or track version not found');
}
const formData = await request.formData(); const formData = await request.formData();
const comment = formData.get('comment') as string; const comment = formData.get('comment') as string;
database.createComment(versionId, session.user.userId, comment); await database.createComment(versionId, session.user.userId, comment);
} }
}; };

View file

@ -0,0 +1,30 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`create comment -- id 1`] = `undefined`;
exports[`create comment -- id does not exist 1`] = `
HttpError {
"body": {
"message": "Track not found",
},
"status": 404,
}
`;
exports[`create comment -- not logged in 1`] = `
Redirect {
"location": "/login",
"status": 302,
}
`;
exports[`create comment -- not uuid or id 1`] = `
HttpError {
"body": {
"message": "Track or track version not found",
},
"status": 404,
}
`;
exports[`create comment -- uuid 1`] = `undefined`;

View file

@ -0,0 +1,92 @@
import { expect, test } from 'vitest';
import { actions } from './+page.server';
import { v4 as uuidv4 } from 'uuid';
import { MockGenericDatabase } from '$lib/server/db/mock';
const baseLocals = {
// TODO: Replace with authentication adapter
authReq: {
validate: () => {
return {
user: {
userId: 1
}
};
}
},
database: new MockGenericDatabase()
};
const baseRequest = {
formData: async () => {
let formData = new FormData();
formData.append('comment', 'uwu');
return formData;
}
};
test('create comment -- uuid', async () => {
expect(
await actions.comment({
request: baseRequest,
params: { slug: uuidv4() },
locals: baseLocals
})
).toMatchSnapshot();
});
test('create comment -- id', async () => {
expect(
await actions.comment({
request: baseRequest,
params: { slug: '1' },
locals: baseLocals
})
).toMatchSnapshot();
});
test('create comment -- id does not exist', async () => {
expect(async () => {
let locals = baseLocals;
locals.database.getLatestTrackVersion = async () => {
return null;
};
await actions.comment({
request: baseRequest,
params: { slug: '1' },
locals
});
}).rejects.toThrowErrorMatchingSnapshot();
});
test('create comment -- not uuid or id', async () => {
expect(
async () =>
await actions.comment({
request: baseRequest,
params: { slug: 'no' },
locals: baseLocals
})
).rejects.toThrowErrorMatchingSnapshot();
});
test('create comment -- not logged in', async () => {
let locals = baseLocals;
locals.authReq = {
validate: () => {
return null;
}
};
expect(
async () =>
await actions.comment({
request: baseRequest,
params: { slug: uuidv4() },
locals: baseLocals
})
).rejects.toThrowErrorMatchingSnapshot();
});