feat: add patch method
This commit is contained in:
+40
-8
@@ -14,7 +14,7 @@ const upload = multer({
|
||||
|
||||
const app = express();
|
||||
|
||||
const AddInput = z.object({
|
||||
const PostInput = z.object({
|
||||
file: z.object({ filename: z.string() }),
|
||||
body: z.object({
|
||||
publication_date: z.coerce
|
||||
@@ -25,7 +25,19 @@ const AddInput = z.object({
|
||||
}),
|
||||
});
|
||||
|
||||
const DeleteInput = z.object({
|
||||
const PatchInput = z.object({
|
||||
file: z.object({ filename: z.string().optional() }).optional(),
|
||||
body: z.object({
|
||||
publication_date: z.coerce
|
||||
.date()
|
||||
.optional()
|
||||
.transform((x) => (x ? new Date(x).toISOString() : x)),
|
||||
title: z.string().nonempty().optional(),
|
||||
text: z.string().nonempty().optional(),
|
||||
}),
|
||||
});
|
||||
|
||||
const AnnouncementIdInput = z.object({
|
||||
announcementId: z.coerce.number().positive(),
|
||||
});
|
||||
|
||||
@@ -34,19 +46,39 @@ app.get("/hello", (_, res) => {
|
||||
});
|
||||
|
||||
app.post("/api/announcements", upload.single("image"), async (req, res) => {
|
||||
const addInput = AddInput.parse(req);
|
||||
const postInput = PostInput.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,
|
||||
title: postInput.body.title,
|
||||
text: postInput.body.text,
|
||||
publication_date: postInput.body.publication_date,
|
||||
image: postInput.file.filename,
|
||||
})
|
||||
.execute();
|
||||
res.redirect("/");
|
||||
});
|
||||
|
||||
app.patch(
|
||||
"/api/announcements/:announcementId",
|
||||
upload.single("image"),
|
||||
async (req, res) => {
|
||||
const patchInput = PatchInput.parse(req);
|
||||
const { announcementId } = AnnouncementIdInput.parse(req.params);
|
||||
await db
|
||||
.updateTable("announcements")
|
||||
.set({
|
||||
title: patchInput.body.title,
|
||||
text: patchInput.body.text,
|
||||
publication_date: patchInput.body.publication_date,
|
||||
image: patchInput.file?.filename,
|
||||
})
|
||||
.where("id", "=", announcementId)
|
||||
.execute();
|
||||
res.redirect("/");
|
||||
},
|
||||
);
|
||||
|
||||
app.get("/api/announcements", async (_, res) => {
|
||||
// await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
const announcements = await db
|
||||
@@ -58,7 +90,7 @@ app.get("/api/announcements", async (_, res) => {
|
||||
});
|
||||
|
||||
app.delete("/api/announcements/:announcementId", async (req, res) => {
|
||||
const { announcementId } = DeleteInput.parse(req.params);
|
||||
const { announcementId } = AnnouncementIdInput.parse(req.params);
|
||||
|
||||
const { image } = await db
|
||||
.selectFrom("announcements")
|
||||
|
||||
Reference in New Issue
Block a user