2023-06-11 09:30:11 +02:00

33 lines
1.2 KiB
TypeScript

import mysql2, { Connection, RowDataPacket, OkPacket, QueryError } from "mysql2";
import { getConnection } from "@/db";
import { Post, postPlaceholder } from "@/model/Models";
import { getPosts, IPost } from "@/controller/Post";
import { NextApiRequest, NextApiResponse } from "next";
import { MPost } from "@/model/Models"
import { Sequelize } from "sequelize";
export default async function handler(req:NextApiRequest, res:NextApiResponse) {
await MPost.sync();
switch (req.method) {
case 'GET':
const posts = await MPost.findAll();
res.status(200).json(posts);
break;
case 'POST':
try {
const post = await MPost.create({
title: req.body.title,
content: req.body.content,
createdAt: new Date(),
updatedAt: new Date(),
},{ fields: ['title','content'] });
const posts2 = await MPost.findAll();
res.status(200).json(posts2);
} catch (error) {
console.log(error)
}
break;
default:
break;
}
}