mirror of
https://github.com/DerTyp7/explainegy-nextjs.git
synced 2025-10-29 12:52:13 +01:00
asd
This commit is contained in:
@@ -1,26 +1,22 @@
|
||||
import { Request, Response } from "express";
|
||||
import prisma from "../../../lib/prisma";
|
||||
import { Prisma, ContentTableEntry } from '@prisma/client';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { ResponseError } from "../../../types/responseErrors";
|
||||
import { getUrlSafeString } from "../../../app/utils";
|
||||
import { formatTextToUrlName } from "../../../utils";
|
||||
|
||||
type ArticleWithIncludes = Prisma.ArticleGetPayload<{ include: { contentTableEntries: true, category: true, image: true } }>
|
||||
|
||||
function sortContentTableEntries(entries: ContentTableEntry[]): ContentTableEntry[] {
|
||||
return entries.sort((a, b) => a.orderIndex - b.orderIndex);
|
||||
}
|
||||
|
||||
|
||||
export default async function handler(req: Request, res: Response) {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
|
||||
const articleName: string = getUrlSafeString(req.query.articleName.toString())
|
||||
console.log(articleName)
|
||||
const articleName: string = formatTextToUrlName(req.query.articleName.toString())
|
||||
|
||||
await prisma.article
|
||||
.findUnique({ where: { name: articleName }, include: { category: true, contentTableEntries: true, image: true } })
|
||||
.findUnique({ where: { name: articleName }, include: { category: true, image: true } })
|
||||
.then((result: ArticleWithIncludes) => {
|
||||
if (result !== null) {
|
||||
result.contentTableEntries = sortContentTableEntries(result.contentTableEntries);
|
||||
res.end(JSON.stringify(result));
|
||||
} else {
|
||||
const error: ResponseError = {
|
||||
|
||||
@@ -3,10 +3,46 @@ import prisma from "../../../lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Article, Category } from "@prisma/client";
|
||||
import { ResponseError } from "../../../types/responseErrors";
|
||||
import { PostArticle } from "../../../types/postData";
|
||||
import { isValidText } from "../../../validators";
|
||||
import { formatTextToUrlName } from "../../../utils";
|
||||
import { json } from "stream/consumers";
|
||||
|
||||
export default async function handler(req: Request, res: Response) {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
|
||||
await prisma.article.create({ data: req.body });
|
||||
console.log();
|
||||
const postData: any = req.body;
|
||||
console.log(postData);
|
||||
if (!isValidText(postData.title)) {
|
||||
res.send(JSON.stringify({ target: "title", error: "Not a valid title" }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isValidText(postData.introduction)) {
|
||||
res.send(JSON.stringify({ target: "introduction", error: "Not a valid introduction" }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!postData.categoryId) {
|
||||
res.send(JSON.stringify({ target: "category", error: "Category is required" }));
|
||||
return;
|
||||
}
|
||||
|
||||
postData.name = formatTextToUrlName(postData.title);
|
||||
prisma.article
|
||||
.create({ data: postData, include: { category: true } })
|
||||
.then(
|
||||
(data) => {
|
||||
res.send(JSON.stringify({ success: true, data: data }));
|
||||
},
|
||||
(errorReason) => {
|
||||
if (errorReason.code === "P2002") {
|
||||
res.send(JSON.stringify({ target: errorReason.meta.target[0], error: "Already exists" }));
|
||||
}
|
||||
}
|
||||
)
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
res.sendStatus(500).end();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ export default async function handler(req: Request, res: Response) {
|
||||
await prisma.article
|
||||
.findMany({
|
||||
where: { category: categoryName.length > 0 ? category : undefined },
|
||||
include: { category: true, contentTableEntries: true },
|
||||
include: { category: true },
|
||||
take: limit,
|
||||
orderBy: orderByObj
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user