2023-02-03 21:18:23 +03:00
|
|
|
import { PrismaClient } from "@prisma/client";
|
|
|
|
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/index.js";
|
2023-02-02 22:07:57 +03:00
|
|
|
|
2023-02-03 21:18:23 +03:00
|
|
|
const prisma = new PrismaClient();
|
2023-02-02 22:07:57 +03:00
|
|
|
|
2023-02-04 19:19:06 +03:00
|
|
|
export async function createPost({ slug, title, body, publish }) {
|
2023-02-03 21:18:23 +03:00
|
|
|
try {
|
|
|
|
await prisma.post.create({
|
|
|
|
data: {
|
|
|
|
slug,
|
|
|
|
title,
|
|
|
|
body,
|
2023-02-04 19:19:06 +03:00
|
|
|
publish,
|
2023-02-03 21:18:23 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof PrismaClientKnownRequestError) {
|
|
|
|
console.log("MESSAGE:", e.message, e.meta, e.code, e.name);
|
|
|
|
console.dir(e);
|
|
|
|
|
|
|
|
return { errorMessage: e.message };
|
|
|
|
|
|
|
|
// specific error
|
|
|
|
} else {
|
|
|
|
console.trace(e);
|
|
|
|
throw e;
|
|
|
|
}
|
2023-02-02 22:07:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-04 19:19:06 +03:00
|
|
|
export async function updatePost({ slug, title, body, publish }) {
|
|
|
|
try {
|
|
|
|
const data = {
|
|
|
|
slug,
|
|
|
|
title,
|
|
|
|
body,
|
|
|
|
publish: new Date(publish),
|
|
|
|
};
|
|
|
|
await prisma.post.upsert({
|
|
|
|
where: {
|
|
|
|
slug,
|
|
|
|
},
|
|
|
|
create: data,
|
|
|
|
update: data,
|
|
|
|
});
|
|
|
|
return null;
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof PrismaClientKnownRequestError) {
|
|
|
|
// https://www.prisma.io/docs/reference/api-reference/error-reference
|
|
|
|
console.log("MESSAGE:", e.message, e.meta, e.code, e.name);
|
|
|
|
console.dir(e);
|
2023-02-02 22:07:57 +03:00
|
|
|
|
2023-02-04 19:19:06 +03:00
|
|
|
return { errorMessage: e.message };
|
|
|
|
|
|
|
|
// specific error
|
|
|
|
} else {
|
|
|
|
console.trace(e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2023-02-02 22:07:57 +03:00
|
|
|
}
|
|
|
|
|
2023-02-06 04:12:02 +03:00
|
|
|
export async function deletePost({ slug }) {
|
|
|
|
await prisma.post.delete({
|
|
|
|
where: { slug },
|
|
|
|
});
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-02-04 19:19:06 +03:00
|
|
|
export async function getPost(slug) {
|
|
|
|
try {
|
|
|
|
return await prisma.post.findFirst({
|
|
|
|
where: {
|
|
|
|
slug,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
body: true,
|
|
|
|
title: true,
|
|
|
|
slug: true,
|
|
|
|
publish: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log("ERROR");
|
|
|
|
console.trace(e);
|
|
|
|
return null;
|
2023-02-02 22:07:57 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-04 19:19:06 +03:00
|
|
|
|
|
|
|
export async function posts() {
|
2023-02-06 04:11:43 +03:00
|
|
|
return await prisma.post.findMany({
|
|
|
|
orderBy: {
|
|
|
|
title: "asc",
|
|
|
|
},
|
|
|
|
});
|
2023-02-04 19:19:06 +03:00
|
|
|
}
|