From da8cd232a2d512d5ec04865d29d449425a75fc00 Mon Sep 17 00:00:00 2001 From: "M.Palerme" Date: Fri, 21 Jun 2024 10:10:56 +0200 Subject: [PATCH] #48 transform table file to become generic and we don't need a project --- db/init.sql | 17 +++++++++++++++-- server/api/addFile.post.ts | 31 ++++++++++++++++++++++++++++--- server/api/delProject.post.ts | 6 +++++- server/api/extract.post.ts | 2 +- server/api/getProjects.post.ts | 25 +++++++++++++------------ 5 files changed, 62 insertions(+), 19 deletions(-) diff --git a/db/init.sql b/db/init.sql index cf86bce..57db595 100644 --- a/db/init.sql +++ b/db/init.sql @@ -78,10 +78,23 @@ CREATE TABLE file date_create TIMESTAMPTZ NOT NULL, f_type VARCHAR(15), f_size INT, - content oid NOT NULL, - id_project SERIAL REFERENCES project (id) + content oid NOT NULL ); +CREATE TABLE proj_file +( + id_project SERIAL REFERENCES project (id) ON DELETE CASCADE, + id_file SERIAL REFERENCES file (id) ON DELETE CASCADE, + PRIMARY KEY (id_project, id_file) +); + +CREATE VIEW view_proj_file AS +SELECT file.id AS id, file.name AS name, file.date_create AS date_create, + file.f_type AS f_type, file.f_size AS f_size, file.content AS content, + proj_file.id_project AS id_project +FROM file, proj_file +WHERE file.id = proj_file.id_file; + CREATE TRIGGER t_content BEFORE UPDATE OR DELETE ON file FOR EACH ROW EXECUTE FUNCTION lo_manage(content); diff --git a/server/api/addFile.post.ts b/server/api/addFile.post.ts index 84a5f21..3628da4 100644 --- a/server/api/addFile.post.ts +++ b/server/api/addFile.post.ts @@ -8,6 +8,7 @@ import {join} from "path"; function addFile(file: tFile, folder: string, client: any, id_project: string) { + let oid: string; return readFile(join("/shareFile", folder, file.id), { encoding: "hex" }) .then(buffer => { // thx: https://stackoverflow.com/a/14408194 @@ -17,14 +18,38 @@ function addFile(file: tFile, folder: string, client: any, id_project: string) { if (respQuery.rows.length === 0) { throw new Error("OID not create"); } - const oid = respQuery.rows[0].oid; + oid = respQuery.rows[0].oid; return client.query(`INSERT INTO file(name, date_create, f_type, - f_size, content,id_project) + f_size, content) VALUES ('${file.name}', NOW(), '${file.type}', '${file.size}', - '${oid}', '${id_project}')`); + '${oid}')`); + }) + .then(() => { + // Break the promise chain when we haven't project + if (id_project === "NULL") { + throw new Error("Just add file in the database"); + } + // get id of the new file + return client.query(` + SELECT id FROM file + WHERE name = '${file.name}' + AND content = '${oid}'`); + }) + .then((respQuery) => { + if (respQuery.rows.length === 0) { + throw new Error("File not create"); + } + // associate the file with the project + return client.query(` + INSERT INTO proj_file(id_project, id_file) + VALUES ('${id_project}', '${respQuery.rows[0].id}')`); }) .catch((err) => { + // File add without project + if (id_project === "NULL") { + return; + } console.error("Add file fail : ", file.name, err); throw new Error("Add file fail"); }); diff --git a/server/api/delProject.post.ts b/server/api/delProject.post.ts index 6f215ed..044dc47 100644 --- a/server/api/delProject.post.ts +++ b/server/api/delProject.post.ts @@ -15,7 +15,11 @@ export default defineEventHandler((event) => { return client.connect() // thx https://stackoverflow.com/a/11691651 .then(() => client.query(`DELETE FROM file - WHERE id_project ='${idProject}'`)) + WHERE id IN ( + SELECT id + FROM view_proj_file + WHERE id_project = '${idProject}'); + `)) .then(() => client.query(`DELETE FROM project WHERE id ='${idProject}'`)) .finally(() => client.end()); diff --git a/server/api/extract.post.ts b/server/api/extract.post.ts index 48c1733..406992a 100644 --- a/server/api/extract.post.ts +++ b/server/api/extract.post.ts @@ -32,7 +32,7 @@ export default defineEventHandler(async (event) => { .then(() => readBody(event)) .then((idProject: string) => { return client.query(`SELECT content, f_size, f_type - FROM file + FROM view_proj_file WHERE id_project = '${idProject}'`); }) .then((respQuery: { diff --git a/server/api/getProjects.post.ts b/server/api/getProjects.post.ts index 5c8dcf1..e8ccc29 100644 --- a/server/api/getProjects.post.ts +++ b/server/api/getProjects.post.ts @@ -42,18 +42,19 @@ export default defineEventHandler(async (event): Promise<{ const offset = (askProject.page - 1) * limit; // Get Project about filter - const getProjectsSQL = `SELECT project.id as p_id, project.name as p_name, - project.date_create, file.name as f_name, - file.f_type, file.f_size, file.id as f_id - FROM project - FULL JOIN file on file.id_project = project.id - WHERE project.id IN ( - SELECT id - FROM project - WHERE team = '${team}' - ORDER BY ${orderBy} ${sort} - LIMIT ${limit} OFFSET ${offset}) - ORDER BY ${orderBy} ${sort}`; + const getProjectsSQL = ` + SELECT project.id as p_id, project.name as p_name, project.date_create, + view_proj_file.name as f_name, view_proj_file.f_type, + view_proj_file.f_size, view_proj_file.id as f_id + FROM project + FULL JOIN view_proj_file on view_proj_file.id_project = project.id + WHERE project.id IN ( + SELECT id + FROM project + WHERE team = '${team}' + ORDER BY ${orderBy} ${sort} + LIMIT ${limit} OFFSET ${offset}) + ORDER BY ${orderBy} ${sort}`; const resultProject = await client.query(getProjectsSQL);