mirror of
https://github.com/DerTyp7/explainegy-nextjs.git
synced 2025-10-29 21:02:13 +01:00
refactor
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
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";
|
||||
|
||||
|
||||
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");
|
||||
|
||||
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) => {
|
||||
|
||||
const error: ResponseError = {
|
||||
code: "500",
|
||||
message: err,
|
||||
};
|
||||
res.status(500).send(JSON.stringify(error));
|
||||
});
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
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";
|
||||
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");
|
||||
|
||||
if (req.method == "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>;
|
||||
|
||||
if (orderBy === "recent") {
|
||||
orderByObj = {
|
||||
dateCreated: "desc"
|
||||
}
|
||||
} else if (orderBy === "popularity") {
|
||||
|
||||
}
|
||||
|
||||
|
||||
await prisma.article
|
||||
.findMany({
|
||||
where: { category: categoryName.length > 0 ? category : undefined },
|
||||
include: { category: true },
|
||||
take: limit,
|
||||
orderBy: orderByObj
|
||||
})
|
||||
.then((result: Article[]) => { //! ContentTableEntries not sorted
|
||||
if (result !== null) {
|
||||
res.end(JSON.stringify(result));
|
||||
} else {
|
||||
const error: ResponseError = {
|
||||
code: "404",
|
||||
message: "No articles found!",
|
||||
};
|
||||
res.status(404).send(JSON.stringify(error));
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
const error: ResponseError = {
|
||||
code: "500",
|
||||
message: err,
|
||||
};
|
||||
res.status(500).send(JSON.stringify(error));
|
||||
});
|
||||
} else if (req.method == "POST") {
|
||||
const data: any = 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;
|
||||
}
|
||||
|
||||
data.name = formatTextToUrlName(data.title);
|
||||
prisma.article
|
||||
.create({ data: data, 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();
|
||||
});
|
||||
} 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();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
import { Request, Response } from "express";
|
||||
import prisma from "../../../../lib/prisma";
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { ResponseError } from "../../../../types/responseErrors";
|
||||
import { formatTextToUrlName } from "../../../../utils";
|
||||
|
||||
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");
|
||||
|
||||
const articleName: string = formatTextToUrlName(req.query.articleName.toString())
|
||||
|
||||
await prisma.article
|
||||
.findUnique({ where: { name: articleName }, 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) => {
|
||||
|
||||
const error: ResponseError = {
|
||||
code: "500",
|
||||
message: err,
|
||||
};
|
||||
res.status(500).send(JSON.stringify(error));
|
||||
});
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import { Request, Response } from "express";
|
||||
import prisma from "../../../lib/prisma";
|
||||
import { Category } from "@prisma/client";
|
||||
import { ResponseError } from "../../../types/responseErrors";
|
||||
|
||||
export default async function handler(req: Request, res: Response) {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
|
||||
const categoryId: string = req.query.categoryId.toString() ?? undefined;
|
||||
|
||||
await prisma.category
|
||||
.findUnique({ where: { id: categoryId }, include: { svg: true } })
|
||||
.then((result: Category) => {
|
||||
if (result !== null) {
|
||||
res.end(JSON.stringify(result));
|
||||
} else {
|
||||
const error: ResponseError = {
|
||||
code: "404",
|
||||
message: "No category with this id found!",
|
||||
};
|
||||
res.status(404).send(JSON.stringify(error));
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
const error: ResponseError = {
|
||||
code: "500",
|
||||
message: err,
|
||||
};
|
||||
res.status(500).send(JSON.stringify(error));
|
||||
});
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
import { Request, Response } from "express";
|
||||
import prisma from "../../../lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Category, Svg } from "@prisma/client";
|
||||
import { ResponseError } from "../../../types/responseErrors";
|
||||
import { formatTextToUrlName } from "../../../utils";
|
||||
import { isValidText } from "../../../validators";
|
||||
import { title } from "process";
|
||||
|
||||
export default async function handler(req: Request, res: Response) {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
|
||||
if (req.method == "GET") {
|
||||
await prisma.category
|
||||
.findMany({ include: { svg: true } })
|
||||
.then((result: Category[]) => {
|
||||
if (result !== null) {
|
||||
res.end(JSON.stringify(result));
|
||||
} else {
|
||||
console.log("No categories found");
|
||||
res.end(JSON.stringify([]));
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
res.end(JSON.stringify([]));
|
||||
});
|
||||
} else if (req.method == "POST") {
|
||||
const data: any = req.body;
|
||||
if (!isValidText(data.title)) {
|
||||
res.send(JSON.stringify({ target: "title", error: "Not a valid title" }));
|
||||
return;
|
||||
}
|
||||
|
||||
data.name = formatTextToUrlName(data.title);
|
||||
data.svg.viewbox = data.svg.viewbox.length > 1 ? data.svg.viewbox : null;
|
||||
console.log(data);
|
||||
|
||||
await prisma.svg
|
||||
.create({ data: data.svg })
|
||||
.then(
|
||||
async (svgData) => {
|
||||
await prisma.category
|
||||
.create({
|
||||
data: { title: data.title, name: data.name, color: data.name, svgId: svgData.id },
|
||||
include: { svg: 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();
|
||||
});
|
||||
},
|
||||
(errorReason) => {
|
||||
res.sendStatus(500).end(errorReason);
|
||||
}
|
||||
)
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
res.sendStatus(500).end();
|
||||
});
|
||||
} else if (req.method == "PUT") {
|
||||
const data: any = req.body;
|
||||
if (!isValidText(data.title)) {
|
||||
res.send(JSON.stringify({ target: "title", error: "Not a valid title" }));
|
||||
return;
|
||||
}
|
||||
|
||||
data.name = formatTextToUrlName(data.title);
|
||||
|
||||
console.log(data);
|
||||
const newSvg: Prisma.SvgUncheckedUpdateInput = {
|
||||
viewbox: data.svg.viewbox,
|
||||
path: data.svg.path,
|
||||
};
|
||||
|
||||
const newCategory: Prisma.CategoryUncheckedUpdateInput = {
|
||||
title: data.title,
|
||||
name: data.name,
|
||||
color: data.color,
|
||||
};
|
||||
|
||||
await prisma.category
|
||||
.update({
|
||||
data: newCategory,
|
||||
where: { id: data.id },
|
||||
include: { svg: true },
|
||||
})
|
||||
.then(
|
||||
async (categoryData) => {
|
||||
console.log("2");
|
||||
await prisma.svg
|
||||
.update({ data: newSvg, where: { id: categoryData.svg.id } })
|
||||
.then(
|
||||
(svgData) => {
|
||||
console.log("3");
|
||||
res.send(JSON.stringify({ success: true, data: categoryData }));
|
||||
},
|
||||
(errorReason) => {
|
||||
res.sendStatus(500).end(errorReason);
|
||||
}
|
||||
)
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
res.sendStatus(500).end();
|
||||
});
|
||||
},
|
||||
(errorReason) => {
|
||||
console.log(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();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
import { Request, Response } from "express";
|
||||
|
||||
import { Category } from "@prisma/client";
|
||||
import prisma from "../../../../lib/prisma";
|
||||
import { ResponseError } from "../../../../types/responseErrors";
|
||||
|
||||
|
||||
export default async function handler(req: Request, res: Response) {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
|
||||
const categoryName: string = req.query.categoryName.toString() ?? undefined;
|
||||
|
||||
await prisma.category
|
||||
.findUnique({ where: { name: categoryName }, include: { svg: true } })
|
||||
.then((result: Category) => {
|
||||
if (result !== null) {
|
||||
res.end(JSON.stringify(result));
|
||||
} else {
|
||||
const error: ResponseError = {
|
||||
code: "404",
|
||||
message: "No category with this name found!",
|
||||
};
|
||||
res.status(404).send(JSON.stringify(error));
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
const error: ResponseError = {
|
||||
code: "500",
|
||||
message: err,
|
||||
};
|
||||
res.status(500).send(JSON.stringify(error));
|
||||
});
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import { Request, Response } from "express";
|
||||
import prisma from "../../../lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
export default async function handler(req: Request, res: Response) {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
|
||||
if (req.method == "GET") {
|
||||
res.send(await prisma.image.findMany())
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import prisma from "../../lib/prisma";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
let query: string = req.query?.q ?? "";
|
||||
|
||||
query = query.toLowerCase().replaceAll("%20", "");
|
||||
query = query.toLowerCase().replaceAll(" ", "");
|
||||
|
||||
if (query.length > 0) {
|
||||
const articles = await prisma.article.findMany({
|
||||
select: { title: true, name: true },
|
||||
take: 5,
|
||||
}); //TODO order by most viewed
|
||||
|
||||
let result = [];
|
||||
|
||||
articles.forEach((a) => {
|
||||
let title = a.title.toLowerCase().replaceAll(" ", "");
|
||||
title = title.toLowerCase().replaceAll("%20", "");
|
||||
|
||||
if (title.includes(query)) {
|
||||
result.push(a);
|
||||
}
|
||||
});
|
||||
|
||||
res.end(JSON.stringify(result));
|
||||
} else {
|
||||
res.end(JSON.stringify([]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user