feat: edit
This commit is contained in:
+26
-22
@@ -41,25 +41,38 @@ const AnnouncementIdInput = z.object({
|
||||
announcementId: z.coerce.number().positive(),
|
||||
});
|
||||
|
||||
app.get("/hello", (_, res) => {
|
||||
res.send("Hello Vite + React + TypeScript!");
|
||||
app.get("/api/announcements", async (_, res) => {
|
||||
const announcements = await db
|
||||
.selectFrom("announcements")
|
||||
.select(["id", "title", "text", "publication_date", "image"])
|
||||
.orderBy("announcements.publication_date", "desc")
|
||||
.execute();
|
||||
res.json(announcements);
|
||||
});
|
||||
|
||||
app.get("/api/announcements/:announcementId", async (req, res) => {
|
||||
const { announcementId } = AnnouncementIdInput.parse(req.params);
|
||||
const announcement = await db
|
||||
.selectFrom("announcements")
|
||||
.select(["id", "title", "text", "publication_date", "image"])
|
||||
.where("id", "=", announcementId)
|
||||
.executeTakeFirstOrThrow();
|
||||
res.json(announcement);
|
||||
});
|
||||
|
||||
app.post("/api/announcements", upload.single("image"), async (req, res) => {
|
||||
const postInput = PostInput.parse(req);
|
||||
await db
|
||||
.insertInto("announcements")
|
||||
.values({
|
||||
title: postInput.body.title,
|
||||
text: postInput.body.text,
|
||||
publication_date: postInput.body.publication_date,
|
||||
image: postInput.file.filename,
|
||||
})
|
||||
.execute();
|
||||
const announcement = {
|
||||
title: postInput.body.title,
|
||||
text: postInput.body.text,
|
||||
publication_date: postInput.body.publication_date,
|
||||
image: postInput.file.filename,
|
||||
};
|
||||
await db.insertInto("announcements").values(announcement).execute();
|
||||
res.redirect("/");
|
||||
});
|
||||
|
||||
app.patch(
|
||||
app.post(
|
||||
"/api/announcements/:announcementId",
|
||||
upload.single("image"),
|
||||
async (req, res) => {
|
||||
@@ -75,20 +88,11 @@ app.patch(
|
||||
})
|
||||
.where("id", "=", announcementId)
|
||||
.execute();
|
||||
|
||||
res.redirect("/");
|
||||
},
|
||||
);
|
||||
|
||||
app.get("/api/announcements", async (_, res) => {
|
||||
// await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
const announcements = await db
|
||||
.selectFrom("announcements")
|
||||
.select(["id", "title", "text", "publication_date", "image"])
|
||||
.orderBy("announcements.publication_date", "desc")
|
||||
.execute();
|
||||
res.json(announcements);
|
||||
});
|
||||
|
||||
app.delete("/api/announcements/:announcementId", async (req, res) => {
|
||||
const { announcementId } = AnnouncementIdInput.parse(req.params);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user