diff --git a/components/DbCompoundShowList.vue b/components/DbCompoundShowList.vue index 759ecc1..83984e9 100644 --- a/components/DbCompoundShowList.vue +++ b/components/DbCompoundShowList.vue @@ -315,37 +315,45 @@ function createCompound() { async function updateCompound(){ waitingProcess(t("message.updateInProgress")); - const holdOn = []; + const rows = await $fetch("/api/manageControl/rows", { + method:"POST", + body:{ + nameTable:"fitting", + wheres:{"id_compound":currentCompound.id} + } + }); - - const addListId = recordModif.add.filter(id => id != ""); - if(addListId.length>0){ - const addList = currentCompound.files.filter(file => - addListId.includes(file.id)); - holdOn.push ($fetch("/api/addFile",{ + const holdOn = []; + if(rows.length){ + // archived compound yet used + holdOn.push($fetch("/api/manageControl/update", { method:"POST", body:{ - files: addList, - folder: currentFolder, - id_compound: currentCompound.id + nameTable:props.nameTable, + id: currentCompound.id, + columns:{archive_date: Date.now()} + } + })); + // create new compond modified + holdOn.push($fetch("/api/manageControl/add", { + method:"POST", + body:{ + items: [currentCompound], + nameTable: props.nameTable } })); } - - const delListId = recordModif.del.filter(id => id !="" ); - if (delListId.length>0){ - holdOn.push($fetch("/api/delFile",{method:"POST",body: delListId})); - } - - if(recordModif.name.value != ""){ - holdOn.push($fetch("/api/changeNameCompound",{ + else{ + // modified compound + holdOn.push($fetch("/api/manageControl/update", { method:"POST", - body: { + body:{ + nameTable:props.nameTable, id: currentCompound.id, - name: recordModif.name.value + columns:currentCompound } })); - } + } Promise.all(holdOn) .then(() => processOk(currentCompound.name + " " + diff --git a/db/init.sql b/db/init.sql index 0c799c0..651c70d 100644 --- a/db/init.sql +++ b/db/init.sql @@ -84,9 +84,32 @@ CREATE TRIGGER t_content BEFORE UPDATE OR DELETE ON file CREATE TABLE compound ( id SERIAL PRIMARY KEY, - name VARCHAR(255), + name VARCHAR(255) UNIQUE, url VARCHAR(255), - description TEXT + description TEXT, + archive_date TIMESTAMPTZ +); + +CREATE TYPE m_type AS ENUM ('UV', 'FID', 'MZ'); + +CREATE TABLE machine +( + id SERIAL PRIMARY KEY, + name VARCHAR(255) UNIQUE, + m_type m_type, + description TEXT, + archive_date TIMESTAMPTZ +); + +CREATE TABLE fitting +( + id_compound SERIAL REFERENCES compound (id), + id_machine SERIAL REFERENCES machine (id), + date_create TIMESTAMPTZ NOT NULL, + url_provider VARCHAR(255), + lot VARCHAR(255) UNIQUE, + rt numeric, + PRIMARY KEY (id_compound, id_machine, date_create) ); INSERT INTO users (name, email, hash, team) diff --git a/server/api/class/tableClass.ts b/server/api/class/tableClass.ts index e11c23d..ca3c1ba 100644 --- a/server/api/class/tableClass.ts +++ b/server/api/class/tableClass.ts @@ -111,7 +111,7 @@ export default class Table { const client = new pg.Client(); await client.connect(); - let query = `SELECT * FROM ${this._nameTable} `; + let query = `SELECT * FROM ${this._nameTable} WHERE `; // create WHERE part of query const where:string[] = []; @@ -140,7 +140,8 @@ export default class Table { const valueOrEmpty = ((x: string|number|undefined) => x?x.toString():""); - const columns = this._headers.map(x => x.key).filter(x => x!= "id"); + const columns = this._headers.map(x => x.key).filter(x => + x!= "id" && x!= "archive_date"); let query = `INSERT INTO ${this._nameTable} (${columns.join(",")}) VALUES `; @@ -171,16 +172,20 @@ export default class Table { */ async update(id:number, columns:{[columnName:string]:string|number}) { - let query = `UPDATE ${this._nameTable} WHERE id = ${id} SET `; + let query = `UPDATE ${this._nameTable} SET `; for(const column in columns){ query += column + "='" + columns[column] +"',"; } - + // delete last , - query.slice(0,-1); + query = query.slice(0,-1); + + query += ` WHERE id = ${id} `; - const client = pg.Client(); + console.log(query); + + const client = new pg.Client(); await client.connect(); await client.query(query); client.end(); @@ -191,7 +196,7 @@ export default class Table { * @param id :number: id of element */ async del(id:number){ - const client = pg.Client(); + const client = new pg.Client(); await client.connect(); await client.query(`DELETE FROM ${this._nameTable} WHERE id=${id}`); client.end(); diff --git a/server/api/manageControl/[action].post.ts b/server/api/manageControl/[action].post.ts index ed58dca..557043e 100644 --- a/server/api/manageControl/[action].post.ts +++ b/server/api/manageControl/[action].post.ts @@ -18,6 +18,10 @@ export default defineEventHandler(async (event) => { return await table.totalItems(); case "add": return await table.add(body.items); + case "rows": + return await table.getRows(body.wheres); + case "update": + return await table.update(body.id, body.columns); default: return table.header; }