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