Skip to content

Commit

Permalink
#26 update compound and add other table
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcellino-Palerme committed Jan 7, 2024
1 parent e8cac2d commit 56bf92d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 30 deletions.
50 changes: 29 additions & 21 deletions components/DbCompoundShowList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 + " " +
Expand Down
27 changes: 25 additions & 2 deletions db/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 12 additions & 7 deletions server/api/class/tableClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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 `;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions server/api/manageControl/[action].post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 56bf92d

Please sign in to comment.