Files
x-news-ai/src/shared/pg.post.service.ts
T
2026-05-23 07:26:01 +00:00

43 lines
1.1 KiB
TypeScript

import {Injectable} from "@nestjs/common";
import {PrismaService} from "../../prisma/prisma.service";
import {Post, Prisma} from "../generated/prisma/client";
import {_toNum} from "./helper";
@Injectable()
export class PgPostService {
constructor(private prisma: PrismaService) {
}
async post(postWhereUniqueInput: Prisma.PostWhereUniqueInput): Promise<Post | null> {
return this.prisma.post.findUnique({
where: postWhereUniqueInput,
});
}
async getById(id: number): Promise<Post | null> {
return this.post({id})
}
async createPost(data: Prisma.PostCreateInput): Promise<Post> {
if(_toNum(process.env.X_NEWS_ALLOW_SAVE_POST_CONTENT) != 1) {
console.error('Not allow save ..');
// @ts-ignore
return {
id: 0
};
}
return this.prisma.post.create({
data,
});
}
async updatePost(id, data: Prisma.PostUpdateInput): Promise<Post> {
return this.prisma.post.update({
data,
where: {
id
}
});
}
}