Skip to content

Commit

Permalink
#48 transform table file to become generic and we don't need a project
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcellino-Palerme committed Jun 21, 2024
1 parent b6184c9 commit da8cd23
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
17 changes: 15 additions & 2 deletions db/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
31 changes: 28 additions & 3 deletions server/api/addFile.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
});
Expand Down
6 changes: 5 additions & 1 deletion server/api/delProject.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion server/api/extract.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
25 changes: 13 additions & 12 deletions server/api/getProjects.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit da8cd23

Please sign in to comment.