mirror of
https://github.com/DerTyp7/explainegy-nextjs.git
synced 2025-10-29 04:42:12 +01:00
start refactor
This commit is contained in:
@@ -7,7 +7,11 @@ import { ArticleWithIncludes, FetchManager } from "../../../../manager/fetchMana
|
||||
import { formatTextToUrlName } from "../../../../utils";
|
||||
|
||||
//* MAIN
|
||||
export default async function ArticlePage({ params }: { params: { articleName: string; categoryName: string } }) {
|
||||
export default async function ArticlePage({
|
||||
params,
|
||||
}: {
|
||||
params: { articleName: string; categoryName: string; test: string };
|
||||
}) {
|
||||
const articleName: string = formatTextToUrlName(params.articleName);
|
||||
const article: ArticleWithIncludes = await FetchManager.Article.getByName(articleName);
|
||||
|
||||
@@ -16,6 +20,7 @@ export default async function ArticlePage({ params }: { params: { articleName: s
|
||||
const dateOptions: Intl.DateTimeFormatOptions = { month: "long", day: "numeric", year: "numeric" };
|
||||
const markdown: string = article?.markdown ?? "";
|
||||
|
||||
console.log(params.test);
|
||||
return (
|
||||
<div className={styles.article}>
|
||||
<ContentTable contentTableData={article.contentTable ? article.contentTable : []} />
|
||||
@@ -61,3 +66,8 @@ export async function generateStaticParams() {
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
export function getServerSideProps() {
|
||||
console.log("-----------------------------------");
|
||||
return { test: "weird test" };
|
||||
}
|
||||
|
||||
@@ -12,22 +12,28 @@ type ArticleWithIncludes = Prisma.ArticleGetPayload<{ include: { contentTableEnt
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const articleId: string = formatTextToUrlName(req.query.articleId.toString())
|
||||
|
||||
console.log(`API articleId: ${articleId}`)
|
||||
if (req.method == "GET") { //* GET
|
||||
console.log("get")
|
||||
await prisma.article
|
||||
.findUnique({ where: { id: articleId }, include: { category: true, image: true } })
|
||||
.then((result: ArticleWithIncludes) => {
|
||||
if (result !== null) {
|
||||
console.log("result", result)
|
||||
res.json(result);
|
||||
} else {
|
||||
console.log("no article found")
|
||||
const error: ResponseError = {
|
||||
code: "404",
|
||||
message: "No article with this name found!",
|
||||
};
|
||||
res.status(404).json(error);
|
||||
}
|
||||
}, (err) => {
|
||||
console.log("reason", err)
|
||||
})
|
||||
.catch((err) => {
|
||||
|
||||
console.log("catch", err)
|
||||
const error: ResponseError = {
|
||||
code: "500",
|
||||
message: err,
|
||||
|
||||
@@ -10,12 +10,16 @@ import { UpdateArticle } from "../../../types/api";
|
||||
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
console.log("articles index.ts")
|
||||
if (req.method == "GET") { //* GET
|
||||
console.log("get")
|
||||
const categoryName: string = req.query.categoryName?.toString() ?? "";
|
||||
const limit: number = req.query.limit ? Number(req.query.limit) : undefined;
|
||||
const orderBy: string = req.query.orderBy?.toString() ?? "";
|
||||
const category = await prisma.category.findUnique({ where: { name: categoryName } });
|
||||
|
||||
console.log(categoryName, limit, orderBy, category)
|
||||
|
||||
let orderByObj: Prisma.Enumerable<Prisma.ArticleOrderByWithRelationInput>;
|
||||
|
||||
if (orderBy === "recent") {
|
||||
@@ -28,12 +32,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
|
||||
await prisma.article
|
||||
.findMany({
|
||||
where: { category: categoryName.length > 0 ? category : undefined },
|
||||
where: { category: category ?? undefined },
|
||||
include: { category: true },
|
||||
take: limit,
|
||||
orderBy: orderByObj
|
||||
})
|
||||
.then((result: Article[]) => { //! ContentTableEntries not sorted
|
||||
console.log("result", result)
|
||||
if (result !== null) {
|
||||
res.end(JSON.stringify(result));
|
||||
} else {
|
||||
@@ -43,8 +48,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
};
|
||||
res.status(404).json(error);
|
||||
}
|
||||
}, (err) => {
|
||||
console.log("reason", err)
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log("catch", err)
|
||||
const error: ResponseError = {
|
||||
code: "500",
|
||||
message: err,
|
||||
@@ -82,7 +90,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
res.json({ success: true, data: data });
|
||||
},
|
||||
(errorReason) => {
|
||||
console.log(errorReason)
|
||||
console.log("reason", errorReason)
|
||||
if (errorReason.code === "P2002") {
|
||||
res.json({ target: errorReason.meta.target[0], error: "Already exists" });
|
||||
}
|
||||
|
||||
@@ -6,28 +6,30 @@ import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
|
||||
type ArticleWithIncludes = Prisma.ArticleGetPayload<{ include: { category: true, image: true } }>
|
||||
|
||||
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
|
||||
const articleName: string = formatTextToUrlName(req.query.articleName.toString())
|
||||
console.log(`API: articleName: ${articleName}`)
|
||||
await prisma.article
|
||||
.findUnique({ where: { name: articleName }, include: { category: true, image: true } })
|
||||
.then((result: ArticleWithIncludes) => {
|
||||
|
||||
console.log("result", result)
|
||||
if (result !== null) {
|
||||
console.log("send")
|
||||
res.json(result);
|
||||
} else {
|
||||
console.log("response no article found")
|
||||
const error: ResponseError = {
|
||||
code: "404",
|
||||
message: "No article with this name found!",
|
||||
};
|
||||
res.status(404).json(error);
|
||||
}
|
||||
}, (err) => {
|
||||
|
||||
console.log("reason", err)
|
||||
})
|
||||
.catch((err) => {
|
||||
|
||||
console.log("catch", err)
|
||||
const error: ResponseError = {
|
||||
code: "500",
|
||||
message: err,
|
||||
|
||||
Reference in New Issue
Block a user