feat: add entries
This commit is contained in:
@@ -1,13 +1,44 @@
|
||||
import express from "express";
|
||||
import multer from "multer";
|
||||
import ViteExpress from "vite-express";
|
||||
import z from "zod";
|
||||
import { db } from "./db/database.js";
|
||||
|
||||
const upload = multer({
|
||||
dest: "uploads/",
|
||||
});
|
||||
|
||||
const app = express();
|
||||
|
||||
const AddInput = z.object({
|
||||
file: z.object({ filename: z.string() }),
|
||||
body: z.object({
|
||||
publication_date: z.coerce
|
||||
.date()
|
||||
.transform((x) => new Date(x).toISOString()),
|
||||
title: z.string().nonempty(),
|
||||
text: z.string().nonempty(),
|
||||
}),
|
||||
});
|
||||
|
||||
app.get("/hello", (_, res) => {
|
||||
res.send("Hello Vite + React + TypeScript!");
|
||||
});
|
||||
|
||||
app.post("/api/announcements/add", upload.single("image"), async (req, res) => {
|
||||
const addInput = AddInput.parse(req);
|
||||
await db
|
||||
.insertInto("announcements")
|
||||
.values({
|
||||
title: addInput.body.title,
|
||||
text: addInput.body.text,
|
||||
publication_date: addInput.body.publication_date,
|
||||
image: addInput.file.filename,
|
||||
})
|
||||
.execute();
|
||||
res.redirect("/");
|
||||
});
|
||||
|
||||
app.get("/api/announcements", async (_, res) => {
|
||||
// await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
const announcements = await db
|
||||
|
||||
Reference in New Issue
Block a user