diff --git a/src/database.ts b/src/database.ts index 21b5ab1..560be42 100644 --- a/src/database.ts +++ b/src/database.ts @@ -353,3 +353,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 fd70e5e..d82fe70 100644 --- a/src/server.ts +++ b/src/server.ts @@ -488,6 +488,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) {