start refactor

This commit is contained in:
Janis
2023-02-07 13:44:10 +01:00
parent d41c8be336
commit d5f5b79140
4 changed files with 36 additions and 10 deletions

View File

@@ -7,7 +7,11 @@ import { ArticleWithIncludes, FetchManager } from "../../../../manager/fetchMana
import { formatTextToUrlName } from "../../../../utils"; import { formatTextToUrlName } from "../../../../utils";
//* MAIN //* 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 articleName: string = formatTextToUrlName(params.articleName);
const article: ArticleWithIncludes = await FetchManager.Article.getByName(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 dateOptions: Intl.DateTimeFormatOptions = { month: "long", day: "numeric", year: "numeric" };
const markdown: string = article?.markdown ?? ""; const markdown: string = article?.markdown ?? "";
console.log(params.test);
return ( return (
<div className={styles.article}> <div className={styles.article}>
<ContentTable contentTableData={article.contentTable ? article.contentTable : []} /> <ContentTable contentTableData={article.contentTable ? article.contentTable : []} />
@@ -61,3 +66,8 @@ export async function generateStaticParams() {
})) }))
); );
} }
export function getServerSideProps() {
console.log("-----------------------------------");
return { test: "weird test" };
}

View File

@@ -12,22 +12,28 @@ type ArticleWithIncludes = Prisma.ArticleGetPayload<{ include: { contentTableEnt
export default async function handler(req: NextApiRequest, res: NextApiResponse) { export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const articleId: string = formatTextToUrlName(req.query.articleId.toString()) const articleId: string = formatTextToUrlName(req.query.articleId.toString())
console.log(`API articleId: ${articleId}`)
if (req.method == "GET") { //* GET if (req.method == "GET") { //* GET
console.log("get")
await prisma.article await prisma.article
.findUnique({ where: { id: articleId }, include: { category: true, image: true } }) .findUnique({ where: { id: articleId }, include: { category: true, image: true } })
.then((result: ArticleWithIncludes) => { .then((result: ArticleWithIncludes) => {
if (result !== null) { if (result !== null) {
console.log("result", result)
res.json(result); res.json(result);
} else { } else {
console.log("no article found")
const error: ResponseError = { const error: ResponseError = {
code: "404", code: "404",
message: "No article with this name found!", message: "No article with this name found!",
}; };
res.status(404).json(error); res.status(404).json(error);
} }
}, (err) => {
console.log("reason", err)
}) })
.catch((err) => { .catch((err) => {
console.log("catch", err)
const error: ResponseError = { const error: ResponseError = {
code: "500", code: "500",
message: err, message: err,

View File

@@ -10,12 +10,16 @@ import { UpdateArticle } from "../../../types/api";
import type { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(req: NextApiRequest, res: NextApiResponse) { export default async function handler(req: NextApiRequest, res: NextApiResponse) {
console.log("articles index.ts")
if (req.method == "GET") { //* GET if (req.method == "GET") { //* GET
console.log("get")
const categoryName: string = req.query.categoryName?.toString() ?? ""; const categoryName: string = req.query.categoryName?.toString() ?? "";
const limit: number = req.query.limit ? Number(req.query.limit) : undefined; const limit: number = req.query.limit ? Number(req.query.limit) : undefined;
const orderBy: string = req.query.orderBy?.toString() ?? ""; const orderBy: string = req.query.orderBy?.toString() ?? "";
const category = await prisma.category.findUnique({ where: { name: categoryName } }); const category = await prisma.category.findUnique({ where: { name: categoryName } });
console.log(categoryName, limit, orderBy, category)
let orderByObj: Prisma.Enumerable<Prisma.ArticleOrderByWithRelationInput>; let orderByObj: Prisma.Enumerable<Prisma.ArticleOrderByWithRelationInput>;
if (orderBy === "recent") { if (orderBy === "recent") {
@@ -28,12 +32,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await prisma.article await prisma.article
.findMany({ .findMany({
where: { category: categoryName.length > 0 ? category : undefined }, where: { category: category ?? undefined },
include: { category: true }, include: { category: true },
take: limit, take: limit,
orderBy: orderByObj orderBy: orderByObj
}) })
.then((result: Article[]) => { //! ContentTableEntries not sorted .then((result: Article[]) => { //! ContentTableEntries not sorted
console.log("result", result)
if (result !== null) { if (result !== null) {
res.end(JSON.stringify(result)); res.end(JSON.stringify(result));
} else { } else {
@@ -43,8 +48,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}; };
res.status(404).json(error); res.status(404).json(error);
} }
}, (err) => {
console.log("reason", err)
}) })
.catch((err) => { .catch((err) => {
console.log("catch", err)
const error: ResponseError = { const error: ResponseError = {
code: "500", code: "500",
message: err, message: err,
@@ -82,7 +90,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
res.json({ success: true, data: data }); res.json({ success: true, data: data });
}, },
(errorReason) => { (errorReason) => {
console.log(errorReason) console.log("reason", errorReason)
if (errorReason.code === "P2002") { if (errorReason.code === "P2002") {
res.json({ target: errorReason.meta.target[0], error: "Already exists" }); res.json({ target: errorReason.meta.target[0], error: "Already exists" });
} }

View File

@@ -6,28 +6,30 @@ import type { NextApiRequest, NextApiResponse } from 'next'
type ArticleWithIncludes = Prisma.ArticleGetPayload<{ include: { category: true, image: true } }> type ArticleWithIncludes = Prisma.ArticleGetPayload<{ include: { category: true, image: true } }>
export default async function handler(req: NextApiRequest, res: NextApiResponse) { export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const articleName: string = formatTextToUrlName(req.query.articleName.toString()) const articleName: string = formatTextToUrlName(req.query.articleName.toString())
console.log(`API: articleName: ${articleName}`)
await prisma.article await prisma.article
.findUnique({ where: { name: articleName }, include: { category: true, image: true } }) .findUnique({ where: { name: articleName }, include: { category: true, image: true } })
.then((result: ArticleWithIncludes) => { .then((result: ArticleWithIncludes) => {
console.log("result", result)
if (result !== null) { if (result !== null) {
console.log("send")
res.json(result); res.json(result);
} else { } else {
console.log("response no article found")
const error: ResponseError = { const error: ResponseError = {
code: "404", code: "404",
message: "No article with this name found!", message: "No article with this name found!",
}; };
res.status(404).json(error); res.status(404).json(error);
} }
}, (err) => {
console.log("reason", err)
}) })
.catch((err) => { .catch((err) => {
console.log("catch", err)
const error: ResponseError = { const error: ResponseError = {
code: "500", code: "500",
message: err, message: err,