mirror of
				https://github.com/DerTyp7/explainegy-nextjs.git
				synced 2025-10-31 05:37:12 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			70 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			70 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { CreateArticle } from "@/types/api";
 | |
| import prisma, { ArticleWithIncludes } from "../../../lib/prisma";
 | |
| import { formatTextToUrlName } from "../../../utils";
 | |
| import { isValidText } from "../../../validators";
 | |
| 
 | |
| 
 | |
| import type { NextApiRequest, NextApiResponse } from 'next'
 | |
| import { Prisma } from '@prisma/client';
 | |
| import { getServerSession } from 'next-auth';
 | |
| import { authOptions } from "../auth/[...nextauth]";
 | |
| 
 | |
| export default async function handler(req: NextApiRequest, res: NextApiResponse) {
 | |
|   const session = await getServerSession(req, res, authOptions);
 | |
|   if (session) {
 | |
|     if (req.method == "POST") { //* POST
 | |
|       console.log("API new article")
 | |
|       const articleData: CreateArticle = req.body
 | |
|       console.log(articleData)
 | |
| 
 | |
| 
 | |
|       if (!isValidText(articleData.title)) {
 | |
|         res.status(500).json({ target: "title", error: "Not a valid title" });
 | |
|         return;
 | |
|       }
 | |
| 
 | |
|       if (!isValidText(articleData.introduction)) {
 | |
|         res.status(500).json({ target: "introduction", error: "Not a valid introduction" });
 | |
|         return;
 | |
|       }
 | |
| 
 | |
|       if (!articleData.categoryId) {
 | |
|         res.status(500).json({ target: "category", error: "Category is required" });
 | |
|         return;
 | |
|       }
 | |
| 
 | |
|       const newArticle: Prisma.ArticleUncheckedCreateInput = {
 | |
|         title: articleData.title,
 | |
|         name: formatTextToUrlName(articleData.title),
 | |
|         introduction: articleData.introduction,
 | |
|         categoryId: articleData.categoryId,
 | |
|         markdown: articleData.markdown ?? "",
 | |
|         contentTable: articleData.contentTable ?? {},
 | |
|         imageUrl: articleData.imageUrl ?? ""
 | |
|       }
 | |
| 
 | |
|       prisma.article
 | |
|         .create({ data: newArticle, include: { category: true } })
 | |
|         .then(
 | |
|           (data: ArticleWithIncludes) => {
 | |
|             res.json({ success: true, data: data });
 | |
|           },
 | |
|           (errorReason) => {
 | |
|             console.log("reason", errorReason)
 | |
|             if (errorReason.code === "P2002") {
 | |
|               res.json({ target: errorReason.meta.target[0], error: "Already exists" });
 | |
|             }
 | |
|           }
 | |
|         )
 | |
|         .catch((err) => {
 | |
|           console.error(err);
 | |
|           res.status(500).end();
 | |
|         });
 | |
|     }
 | |
|   } else {
 | |
|     res.status(403).json({ error: true, message: "Authorization Required" });
 | |
|   }
 | |
| 
 | |
| 
 | |
| }
 | 
