mirror of
https://github.com/DerTyp7/explainegy-nextjs.git
synced 2025-10-29 12:52:13 +01:00
clean up
This commit is contained in:
@@ -1,38 +1,83 @@
|
||||
import { Prisma, Article } from "@prisma/client";
|
||||
import { Request, Response } from "express";
|
||||
import { ResponseError } from "../../../types/responseErrors";
|
||||
import { formatTextToUrlName } from "../../../utils";
|
||||
import prisma from "../../../lib/prisma";
|
||||
|
||||
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { UpdateArticle } from "../../../types/api";
|
||||
import { isValidText } from "../../../validators";
|
||||
|
||||
type ArticleWithIncludes = Prisma.ArticleGetPayload<{ include: { contentTableEntries: true, category: true, image: true } }>
|
||||
|
||||
|
||||
|
||||
export default async function handler(req: Request, res: Response) {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const articleId: string = formatTextToUrlName(req.query.articleId.toString())
|
||||
|
||||
await prisma.article
|
||||
.findUnique({ where: { id: articleId }, include: { category: true, image: true } })
|
||||
.then((result: ArticleWithIncludes) => {
|
||||
if (result !== null) {
|
||||
res.end(JSON.stringify(result));
|
||||
} else {
|
||||
const error: ResponseError = {
|
||||
code: "404",
|
||||
message: "No article with this name found!",
|
||||
};
|
||||
res.status(404).send(JSON.stringify(error));
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
if (req.method == "GET") { //* GET
|
||||
await prisma.article
|
||||
.findUnique({ where: { id: articleId }, include: { category: true, image: true } })
|
||||
.then((result: ArticleWithIncludes) => {
|
||||
if (result !== null) {
|
||||
res.json(result);
|
||||
} else {
|
||||
const error: ResponseError = {
|
||||
code: "404",
|
||||
message: "No article with this name found!",
|
||||
};
|
||||
res.status(404).json(error);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
|
||||
const error: ResponseError = {
|
||||
code: "500",
|
||||
message: err,
|
||||
};
|
||||
res.status(500).json(error);
|
||||
});
|
||||
} else if (req.method == "PUT") {//* PUT
|
||||
const data: UpdateArticle = req.body;
|
||||
|
||||
if (!isValidText(data.title)) {
|
||||
res.json({ target: "title", error: "Not a valid title" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isValidText(data.introduction)) {
|
||||
res.json({ target: "introduction", error: "Not a valid introduction" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.categoryId) {
|
||||
res.json({ target: "category", error: "Category is required" });
|
||||
return;
|
||||
}
|
||||
|
||||
const newArticle: Prisma.ArticleUncheckedUpdateInput = {
|
||||
title: data.title,
|
||||
name: formatTextToUrlName(data.title),
|
||||
introduction: data.introduction,
|
||||
|
||||
categoryId: data.categoryId.toString(),
|
||||
contentTable: data.contentTable,
|
||||
markdown: data.markdown,
|
||||
imageId: data.imageId.toString(),
|
||||
}
|
||||
|
||||
await prisma.article.update({ data: newArticle, where: { id: articleId }, include: { category: true } })
|
||||
.then(
|
||||
(data) => {
|
||||
res.json({ success: true, data: data });
|
||||
},
|
||||
(errorReason) => {
|
||||
if (errorReason.code === "P2002") {
|
||||
res.json({ target: errorReason.meta.target[0], error: "Already exists" });
|
||||
}
|
||||
}
|
||||
)
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
res.status(500).end();
|
||||
});
|
||||
}
|
||||
|
||||
const error: ResponseError = {
|
||||
code: "500",
|
||||
message: err,
|
||||
};
|
||||
res.status(500).send(JSON.stringify(error));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import { Request, Response } from "express";
|
||||
import prisma from "../../../lib/prisma";
|
||||
//@ts-ignore
|
||||
import { Prisma } from "@prisma/client";
|
||||
//@ts-ignore
|
||||
import { Article, Category } from "@prisma/client";
|
||||
import { ResponseError } from "../../../types/responseErrors";
|
||||
import { formatTextToUrlName } from "../../../utils";
|
||||
@@ -10,14 +7,13 @@ import { isValidText } from "../../../validators";
|
||||
import { title } from 'process';
|
||||
import { UpdateArticle } from "../../../types/api";
|
||||
|
||||
export default async function handler(req: Request, res: Response) {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
|
||||
if (req.method == "GET") {
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method == "GET") { //* 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 } });
|
||||
|
||||
let orderByObj: Prisma.Enumerable<Prisma.ArticleOrderByWithRelationInput>;
|
||||
@@ -27,10 +23,9 @@ export default async function handler(req: Request, res: Response) {
|
||||
dateCreated: "desc"
|
||||
}
|
||||
} else if (orderBy === "popularity") {
|
||||
|
||||
// TODO filter with views
|
||||
}
|
||||
|
||||
|
||||
await prisma.article
|
||||
.findMany({
|
||||
where: { category: categoryName.length > 0 ? category : undefined },
|
||||
@@ -46,7 +41,7 @@ export default async function handler(req: Request, res: Response) {
|
||||
code: "404",
|
||||
message: "No articles found!",
|
||||
};
|
||||
res.status(404).send(JSON.stringify(error));
|
||||
res.status(404).json(error);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
@@ -54,23 +49,25 @@ export default async function handler(req: Request, res: Response) {
|
||||
code: "500",
|
||||
message: err,
|
||||
};
|
||||
res.status(500).send(JSON.stringify(error));
|
||||
res.status(500).json(JSON.stringify(error));
|
||||
});
|
||||
} else if (req.method == "POST") {
|
||||
const data: any = req.body;
|
||||
|
||||
|
||||
} else if (req.method == "POST") { //* POST
|
||||
const data: any = req.body;
|
||||
console.log(data)
|
||||
if (!isValidText(data.title)) {
|
||||
res.send(JSON.stringify({ target: "title", error: "Not a valid title" }));
|
||||
res.json({ target: "title", error: "Not a valid title" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isValidText(data.introduction)) {
|
||||
res.send(JSON.stringify({ target: "introduction", error: "Not a valid introduction" }));
|
||||
res.json({ target: "introduction", error: "Not a valid introduction" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.categoryId) {
|
||||
res.send(JSON.stringify({ target: "category", error: "Category is required" }));
|
||||
res.json({ target: "category", error: "Category is required" });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -79,62 +76,19 @@ export default async function handler(req: Request, res: Response) {
|
||||
.create({ data: data, include: { category: true } })
|
||||
.then(
|
||||
(data) => {
|
||||
res.send(JSON.stringify({ success: true, data: data }));
|
||||
console.log("success")
|
||||
res.json({ success: true, data: data });
|
||||
},
|
||||
(errorReason) => {
|
||||
console.log(errorReason)
|
||||
if (errorReason.code === "P2002") {
|
||||
res.send(JSON.stringify({ target: errorReason.meta.target[0], error: "Already exists" }));
|
||||
res.json({ target: errorReason.meta.target[0], error: "Already exists" });
|
||||
}
|
||||
}
|
||||
)
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
res.sendStatus(500).end();
|
||||
});
|
||||
} else if (req.method == "PUT") {
|
||||
const data: UpdateArticle = req.body;
|
||||
|
||||
if (!isValidText(data.title)) {
|
||||
res.send(JSON.stringify({ target: "title", error: "Not a valid title" }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isValidText(data.introduction)) {
|
||||
res.send(JSON.stringify({ target: "introduction", error: "Not a valid introduction" }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.categoryId) {
|
||||
res.send(JSON.stringify({ target: "category", error: "Category is required" }));
|
||||
return;
|
||||
}
|
||||
|
||||
const newArticle: Prisma.ArticleUncheckedUpdateInput = {
|
||||
title: data.title,
|
||||
name: formatTextToUrlName(data.title),
|
||||
introduction: data.introduction,
|
||||
//@ts-ignore
|
||||
categoryId: data.categoryId,
|
||||
contentTable: data.contentTable,
|
||||
markdown: data.markdown,
|
||||
//@ts-ignore
|
||||
imageId: data.imageId,
|
||||
}
|
||||
|
||||
await prisma.article.update({ data: newArticle, where: { id: data.id }, 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();
|
||||
res.status(500).end();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
import { Request, Response } from "express";
|
||||
import prisma from "../../../../lib/prisma";
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { ResponseError } from "../../../../types/responseErrors";
|
||||
import { formatTextToUrlName } from "../../../../utils";
|
||||
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
|
||||
type ArticleWithIncludes = Prisma.ArticleGetPayload<{ include: { contentTableEntries: true, category: true, image: true } }>
|
||||
|
||||
|
||||
|
||||
export default async function handler(req: Request, res: Response) {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
|
||||
const articleName: string = formatTextToUrlName(req.query.articleName.toString())
|
||||
|
||||
await prisma.article
|
||||
.findUnique({ where: { name: articleName }, include: { category: true, image: true } })
|
||||
.then((result: ArticleWithIncludes) => {
|
||||
|
||||
Reference in New Issue
Block a user