mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-13 00:02:37 +10:00
feat(backend): Added backend communction between API & Frontend
This commit is contained in:
114
server/internal/news/index.ts
Normal file
114
server/internal/news/index.ts
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
import prisma from "../db/database";
|
||||||
|
|
||||||
|
class NewsManager {
|
||||||
|
async create(data: {
|
||||||
|
title: string;
|
||||||
|
content: string;
|
||||||
|
excerpt: string;
|
||||||
|
tags: string[];
|
||||||
|
authorId: string;
|
||||||
|
image?: string;
|
||||||
|
}) {
|
||||||
|
return await prisma.news.create({
|
||||||
|
data: {
|
||||||
|
title: data.title,
|
||||||
|
content: data.content,
|
||||||
|
excerpt: data.excerpt,
|
||||||
|
tags: data.tags,
|
||||||
|
image: data.image,
|
||||||
|
author: {
|
||||||
|
connect: {
|
||||||
|
id: data.authorId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
publishedAt: new Date(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getAll(options?: {
|
||||||
|
take?: number;
|
||||||
|
skip?: number;
|
||||||
|
orderBy?: 'asc' | 'desc';
|
||||||
|
tags?: string[];
|
||||||
|
search?: string;
|
||||||
|
}) {
|
||||||
|
const where = {
|
||||||
|
AND: [
|
||||||
|
options?.tags?.length ? {
|
||||||
|
tags: {
|
||||||
|
hasSome: options.tags,
|
||||||
|
},
|
||||||
|
} : {},
|
||||||
|
options?.search ? {
|
||||||
|
OR: [
|
||||||
|
{
|
||||||
|
title: {
|
||||||
|
contains: options.search,
|
||||||
|
mode: 'insensitive' as const,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
content: {
|
||||||
|
contains: options.search,
|
||||||
|
mode: 'insensitive' as const,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
} : {},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
return await prisma.news.findMany({
|
||||||
|
where,
|
||||||
|
take: options?.take,
|
||||||
|
skip: options?.skip,
|
||||||
|
orderBy: {
|
||||||
|
publishedAt: options?.orderBy || 'desc',
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
author: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
displayName: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getById(id: string) {
|
||||||
|
return await prisma.news.findUnique({
|
||||||
|
where: { id },
|
||||||
|
include: {
|
||||||
|
author: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
displayName: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async update(id: string, data: {
|
||||||
|
title?: string;
|
||||||
|
content?: string;
|
||||||
|
excerpt?: string;
|
||||||
|
tags?: string[];
|
||||||
|
image?: string;
|
||||||
|
}) {
|
||||||
|
return await prisma.news.update({
|
||||||
|
where: { id },
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete(id: string) {
|
||||||
|
return await prisma.news.delete({
|
||||||
|
where: { id },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default new NewsManager();
|
||||||
Reference in New Issue
Block a user