From fc1cfbfc31b694b62d7dfa57e3f819843ffba102 Mon Sep 17 00:00:00 2001 From: cofrizot Date: Tue, 28 May 2024 16:23:42 +0200 Subject: [PATCH] Add the update of the title and the content --- src/database.ts | 24 ++++++++++++++++++++++++ src/server.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/src/database.ts b/src/database.ts index 4cfe03c..a6c2754 100644 --- a/src/database.ts +++ b/src/database.ts @@ -354,3 +354,27 @@ export function selectWorkById(db: sqlite3.Database, id: number) { return getDB(db, selectWorkByIdQuery, [id]); } + +/* Update the work title by its ID */ +export function updateWorkTitle( + db: sqlite3.Database, + id: number, + newTitle: string +) { + const updateWorkTitleQuery = `UPDATE work SET title = ? WHERE id_work = ?`; + + return runDB(db, updateWorkTitleQuery, [newTitle, id]); +} + +/* Update the work content by its ID */ +export function updateWorkContent( + db: sqlite3.Database, + id: number, + newContent: string +) { + const updateWorkContentQuery = `UPDATE work SET content = ? WHERE id_work = ?`; + + return runDB(db, updateWorkContentQuery, [newContent, id]); +} + + diff --git a/src/server.ts b/src/server.ts index b89c714..3e781d7 100644 --- a/src/server.ts +++ b/src/server.ts @@ -536,6 +536,54 @@ fastify.get( }, ); +/* Update the work title by its ID */ +fastify.put( + "/works/:id/title", + { + schema: { + params: Type.Object({ + id: Type.Number({ + minimum: 0, + }), + }), + body: Type.Object({ + newTitle: Type.String(), + }), + }, + }, + async (request, reply) => { + const { id } = request.params; + const { newTitle } = request.body; + db.updateWorkTitle(database, id, newTitle); + reply.send({ success: true }); + } +); + +/* Update the work content by its ID */ +fastify.put( + "/works/:id/content", + { + schema: { + params: Type.Object({ + id: Type.Number({ + minimum: 0, + }), + }), + body: Type.Object({ + newContent: Type.String(), + }), + }, + }, + async (request, reply) => { + const { id } = request.params; + const { newContent } = request.body; + db.updateWorkContent(database, id, newContent); + reply.send({ success: true }); + } +); + + + /* Forward output est une fonction asynchrone qui permet de récupérer les messages envoyés par le container et de les renvoyer au client */ async function forwardOutput() { for await (const [buff] of receiver) {