diff --git a/components/ExtractInfo.vue b/components/ExtractInfo.vue index 359664e..c83011c 100644 --- a/components/ExtractInfo.vue +++ b/components/ExtractInfo.vue @@ -91,6 +91,7 @@ function emptyProject(project: tProject) { project.createDate = ""; project.nbFile = 0; project.files = [] as tFile[]; + project.series = []; } // The project consulted @@ -366,11 +367,13 @@ function openProject(id: string) { currentProject.createDate = tempProject[0].createDate; currentProject.nbFile = tempProject[0].nbFile; Object.assign(currentProject.files, tempProject[0].files); + Object.assign(currentProject.series, tempProject[0].series); oldProject.id = id; oldProject.name = tempProject[0].name; oldProject.createDate = tempProject[0].createDate; oldProject.nbFile = tempProject[0].nbFile; Object.assign(oldProject.files, tempProject[0].files); + Object.assign(oldProject.series, tempProject[0].series); } isOpen.value = true; } @@ -402,6 +405,7 @@ function processFail(msg: string) { } function createProject() { const body = new FormData(); + body.append("folder", currentFolder); body.append("project", JSON.stringify(currentProject)); // todo get team name @@ -726,6 +730,7 @@ async function deleteProject(id:string, name:string){ item-title="name" item-value="id" label="t('label.selectSerie')" + multiple /> diff --git a/db/init.sql b/db/init.sql index 5f5ade4..4fa2c82 100644 --- a/db/init.sql +++ b/db/init.sql @@ -149,6 +149,13 @@ FROM series, ratio WHERE series.id = ratio.id_series GROUP BY series.id; +CREATE TABLE proj_series +( + id_project SERIAL REFERENCES project (id) ON DELETE CASCADE, + id_series SERIAL REFERENCES series (id) ON DELETE CASCADE, + PRIMARY KEY (id_project, id_series) +); + INSERT INTO users (name, email, hash, team) VALUES ('root_ep2m2', 'admin@ep2m2.bzh', diff --git a/server/api/AssociateSeries.post.ts b/server/api/AssociateSeries.post.ts new file mode 100644 index 0000000..69d2c21 --- /dev/null +++ b/server/api/AssociateSeries.post.ts @@ -0,0 +1,30 @@ +// SPDX-FileCopyrightText: 2024 Marcellino Palerme +// +// SPDX-License-Identifier: MIT + +import pg from "pg"; + +export default defineEventHandler(async (event) => { + const body = await readBody(event); + const client = new pg.Client(); + return client.connect() + .then(() => { + console.log(body); + + const lPromises = []; + for(const idSerie of body.series){ + lPromises.push( + client.query(` + INSERT INTO proj_series(id_project, id_series) + VALUES ('${body.id_project}', '${idSerie}')` + ) + ); + } + return Promise.allSettled(lPromises); + }) + .catch((err: Error) => { + throw err; + }) + .finally(() => client.end()); + +}); \ No newline at end of file diff --git a/server/api/createProject.post.ts b/server/api/createProject.post.ts index 8096f9d..49e471e 100644 --- a/server/api/createProject.post.ts +++ b/server/api/createProject.post.ts @@ -28,34 +28,40 @@ export default defineEventHandler((event) => { project (name, date_create, team) VALUES ('${project.name}', NOW(), - '${team}')`; - + '${team}') + RETURNING id`; + let idProject: string; const client = new pg.Client(); return client.connect() .then(() => { return client.query(addProjectSql); }) - .then(() => { - return client.query(`SELECT id - FROM project - WHERE name = '${project.name}'`); - }) .then((respQuery:{rows:{id:string}[]}) => { + if (respQuery.rows.length === 0) { throw new Error("project not create"); } - + idProject = respQuery.rows[0].id; return $fetch("/api/addFile",{ method:"POST", body: { files: project.files, folder: folder, - id_project: respQuery.rows[0].id + id_project: idProject } }); }) + .then(() => { + return $fetch("/api/AssociateSeries",{ + method:"POST", + body: { + series: project.series, + id_project: idProject + } + }); + }) .then(() => rm(join("/shareFile", folder), { recursive: true, force: true })) .catch((err: Error) => {