From 8f16eb16198d2ccbb77f8202ad3d9bebf125f6e5 Mon Sep 17 00:00:00 2001 From: "M.Palerme" Date: Mon, 24 Jun 2024 18:56:40 +0200 Subject: [PATCH] #48 update series associated to project --- components/ExtractInfo.vue | 27 +++++++++++++++++++++++++++ server/api/DisassociateSeries.post.ts | 20 ++++++++++++++++++++ server/api/createProject.post.ts | 4 ++++ 3 files changed, 51 insertions(+) create mode 100644 server/api/DisassociateSeries.post.ts diff --git a/components/ExtractInfo.vue b/components/ExtractInfo.vue index c83011c..c34144c 100644 --- a/components/ExtractInfo.vue +++ b/components/ExtractInfo.vue @@ -453,6 +453,33 @@ async function updateProject(){ } })); } + // update series + if(currentProject.series != oldProject.series){ + // delete series + const lDelSeries = oldProject.series.filter(x => !currentProject.series.includes(x)); + // verify we have series to disassociate + if(lDelSeries.length > 0){ + holdOn.push($fetch("/api/DisassociateSeries",{ + method:"POST", + body: { + id_project: currentProject.id, + series: lDelSeries + } + })); + } + // add series + const lAddSeries = currentProject.series.filter(x => !oldProject.series.includes(x)); + // verify we have series to associate + if(lAddSeries.length > 0){ + holdOn.push($fetch("/api/AssociateSeries",{ + method:"POST", + body: { + id_project: currentProject.id, + series: lAddSeries + } + })); + } + } Promise.all(holdOn) .then(() => processOk(currentProject.name + " " + diff --git a/server/api/DisassociateSeries.post.ts b/server/api/DisassociateSeries.post.ts new file mode 100644 index 0000000..b0ec6e2 --- /dev/null +++ b/server/api/DisassociateSeries.post.ts @@ -0,0 +1,20 @@ +// SPDX-FileCopyrightText: 2024 Marcellino Palerme +// +// SPDX-License-Identifier: MIT + +import pg from "pg"; + +export default defineEventHandler((event) => { + return readBody(event) + .then((body) => { + + const client = new pg.Client(); + return client.connect() + + .then(() => client.query(`DELETE FROM proj_series + WHERE id_series IN + (${body.series.join(",")}) + AND id_project = ${body.id_project}`)) + .finally(() => client.end()); + }); +}); \ No newline at end of file diff --git a/server/api/createProject.post.ts b/server/api/createProject.post.ts index 49e471e..3e867a8 100644 --- a/server/api/createProject.post.ts +++ b/server/api/createProject.post.ts @@ -54,6 +54,10 @@ export default defineEventHandler((event) => { }) .then(() => { + // verify we have series to associate + if (project.series.length == 0) { + return; + } return $fetch("/api/AssociateSeries",{ method:"POST", body: {