This commit is contained in:
Janis
2023-02-07 13:45:40 +01:00
parent d5f5b79140
commit d2ff34d3b6
70 changed files with 1388 additions and 8768 deletions

View File

@@ -1,4 +1,3 @@
import { Request, Response } from "express";
import prisma from "../../../lib/prisma";
import { Category } from "@prisma/client";
import { ResponseError } from "../../../types/responseErrors";
@@ -7,51 +6,29 @@ import { Prisma } from "@prisma/client";
import type { NextApiRequest, NextApiResponse } from 'next'
import { formatTextToUrlName } from "../../../utils";
import { isValidText } from "../../../validators";
import { UpdateCategory } from '../../../types/api';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const categoryId: string = req.query.categoryId.toString() ?? undefined;
if (req.method == "GET") {
await prisma.category
.findUnique({ where: { id: categoryId }, include: { svg: true } })
.then((result: Category) => {
if (result !== null) {
res.json(result);
} else {
const error: ResponseError = {
code: "404",
message: "No category with this id 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") {
const data: any = req.body;
if (!isValidText(data.title)) {
if (req.method == "PUT") {
const categoryId: string = req.query.categoryId?.toString() ?? "";
const categoryData: UpdateCategory = req.body;
if (categoryData.title && !isValidText(categoryData.title)) {
res.json({ 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,
viewbox: categoryData.svg?.viewbox ?? undefined,
path: categoryData.svg?.path ?? undefined,
};
const newCategory: Prisma.CategoryUncheckedUpdateInput = {
title: data.title,
name: data.name,
color: data.color,
title: categoryData.title ?? undefined,
name: categoryData.title ? formatTextToUrlName(categoryData.title) : undefined,
color: categoryData.color ?? undefined,
};
await prisma.category
@@ -62,7 +39,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
})
.then(
async (categoryData) => {
console.log("2");
await prisma.svg
.update({ data: newSvg, where: { id: categoryData.svg.id } })
.then(

View File

@@ -1,5 +1,5 @@
import prisma from "../../../lib/prisma";
import prisma, { CategoryWithIncludes } from "../../../lib/prisma";
import { Prisma } from "@prisma/client";
import { Category, Svg } from "@prisma/client";
import { ResponseError } from "../../../types/responseErrors";
@@ -7,48 +7,52 @@ import { formatTextToUrlName } from "../../../utils";
import { isValidText } from "../../../validators";
import type { NextApiRequest, NextApiResponse } from 'next'
import { CreateCategory } from "@/types/api";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method == "GET") { //* GET
await prisma.category
.findMany({ include: { svg: true } })
.then((result: Category[]) => {
if (result !== null) {
res.json(result);
} else {
console.log("No categories found");
res.json([]);
}
})
.catch((err) => {
console.log(err);
res.json([]);
});
} else if (req.method == "POST") {
const data: any = req.body;
if (!isValidText(data.title)) {
if (req.method == "POST") {
console.log("API new category")
const categoryData: CreateCategory = req.body;
console.log(categoryData)
if (!isValidText(categoryData.title)) {
res.json({ 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);
categoryData.svg.viewbox = categoryData.svg.viewbox.length > 1 ? categoryData.svg.viewbox : "";
const newSvg: Prisma.SvgUncheckedCreateInput = {
viewbox: categoryData.svg.viewbox,
path: categoryData.svg.path
}
await prisma.svg
.create({ data: data.svg })
.create({ data: newSvg })
.then(
async (svgData) => {
async (createdSvg: Svg) => {
const newCategory: Prisma.CategoryUncheckedCreateInput = {
title: categoryData.title,
name: formatTextToUrlName(categoryData.title),
color: categoryData.color ?? "teal",
svgId: createdSvg.id,
}
await prisma.category
.create({
data: { title: data.title, name: data.name, color: data.name, svgId: svgData.id },
include: { svg: true },
data: newCategory,
include: { svg: true, articles: true },
})
.then(
(data) => {
res.json({ success: true, data: data });
(createdCategory: CategoryWithIncludes | null) => {
if (createdCategory) {
res.json({ success: true, data: createdCategory });
} else {
res.json({ error: true, message: "Could not create category" });
}
},
(errorReason) => {
console.log(errorReason)
if (errorReason.code === "P2002") {
res.json({ target: errorReason.meta.target[0], error: "Already exists" });
}

View File

@@ -1,29 +0,0 @@
import { Category } from "@prisma/client";
import prisma from "../../../../lib/prisma";
import { ResponseError } from "../../../../types/responseErrors";
import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
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.json(result);
} else {
const error: ResponseError = {
code: "404",
message: "No category with this name found!",
};
res.status(404).json(error);
}
})
.catch((err) => {
const error: ResponseError = {
code: "500",
message: err,
};
res.status(500).json(error);
});
}