Files
drop/server/internal/news/index.ts
DecDuck 45848d175e Small fixes (#141)
* fix: save task as Json rather than string

* fix: pull objects before creating game in database

* fix: strips relative dirs from version information

* fix: #132

* fix: lint

* fix: news object ids and small tweaks

* fix: notification styling errors

* fix: lint
2025-07-20 14:56:15 +10:00

138 lines
2.8 KiB
TypeScript

import prisma from "../db/database";
import objectHandler from "../objects";
class NewsManager {
async create(data: {
title: string;
content: string;
description: string;
tags: string[];
authorId: string;
imageObjectId?: string;
}) {
return await prisma.article.create({
data: {
title: data.title,
description: data.description,
content: data.content,
tags: {
connectOrCreate: data.tags.map((e) => ({
where: { name: e },
create: { name: e },
})),
},
...(data.imageObjectId && { imageObjectId: data.imageObjectId }),
author: {
connect: {
id: data.authorId,
},
},
},
include: {
author: {
select: {
id: true,
displayName: true,
},
},
tags: true,
},
});
}
async fetch(
options: {
take?: number;
skip?: number;
orderBy?: "asc" | "desc";
tags?: string[];
search?: string;
} = {},
) {
return await prisma.article.findMany({
where: {
AND: [
options.tags
? {
tags: {
some: { OR: options.tags?.map((e) => ({ name: e })) ?? [] },
},
}
: undefined,
options.search
? {
title: {
search: options.search,
},
description: {
search: options.search,
},
content: {
search: options.search,
},
}
: undefined,
].filter((e) => e !== undefined),
},
take: options?.take || 10,
skip: options?.skip || 0,
orderBy: {
publishedAt: options?.orderBy || "desc",
},
include: {
author: {
select: {
id: true,
displayName: true,
},
},
tags: true,
},
});
}
async fetchById(id: string) {
return await prisma.article.findUnique({
where: { id },
include: {
author: {
select: {
id: true,
displayName: true,
},
},
tags: true,
},
});
}
async update(
id: string,
data: {
title?: string;
content?: string;
excerpt?: string;
image?: string;
},
) {
return await prisma.article.update({
where: { id },
data,
});
}
async delete(id: string) {
const article = await prisma.article.delete({
where: { id },
});
if (article.imageObjectId) {
return await objectHandler.deleteAsSystem(article.imageObjectId);
}
return true;
}
}
export default new NewsManager();