refactor: many columns/tables in prisma

This commit is contained in:
diced
2023-01-15 13:39:07 -08:00
parent 2ace076fce
commit a8020ecebe
47 changed files with 424 additions and 386 deletions

View File

@@ -1,4 +1,4 @@
import { Image } from '@prisma/client';
import { File } from '@prisma/client';
import { FastifyInstance, FastifyReply } from 'fastify';
import fastifyPlugin from 'fastify-plugin';
@@ -6,21 +6,21 @@ function postFileDecorator(fastify: FastifyInstance, _: unknown, done: () => voi
fastify.decorateReply('postFile', postFile);
done();
async function postFile(this: FastifyReply, file: Image) {
async function postFile(this: FastifyReply, file: File) {
if (!file) return true;
const nFile = await this.server.prisma.image.update({
const nFile = await this.server.prisma.file.update({
where: { id: file.id },
data: { views: { increment: 1 } },
});
if (nFile.maxViews && nFile.views >= nFile.maxViews) {
await this.server.datasource.delete(file.file);
await this.server.prisma.image.delete({ where: { id: nFile.id } });
await this.server.datasource.delete(file.name);
await this.server.prisma.file.delete({ where: { id: nFile.id } });
this.server.logger
.child('file')
.info(`File ${file.file} has been deleted due to max views (${nFile.maxViews})`);
.info(`File ${file.name} has been deleted due to max views (${nFile.maxViews})`);
return true;
}
@@ -36,6 +36,6 @@ export default fastifyPlugin(postFileDecorator, {
declare module 'fastify' {
interface FastifyReply {
postFile: (file: Image) => Promise<boolean>;
postFile: (file: File) => Promise<boolean>;
}
}