From 79d0c8c71df0c2c9adf79560bf084a756fe3eb80 Mon Sep 17 00:00:00 2001 From: "M.Palerme" Date: Thu, 25 Jul 2024 09:12:18 +0200 Subject: [PATCH] #48 delete ratio because ratio calculate during exportation of project --- db/init.sql | 44 ++++------------------------- server/api/AddCalibCurve.post.ts | 21 +------------- server/api/UpdateCalibCurve.post.ts | 2 +- 3 files changed, 8 insertions(+), 59 deletions(-) diff --git a/db/init.sql b/db/init.sql index 9132fec..7b53dce 100644 --- a/db/init.sql +++ b/db/init.sql @@ -134,50 +134,18 @@ CREATE TABLE daughter PRIMARY KEY (id_calib_curves, id_file, id_mol) ); -CREATE TABLE ratio -( - id_mol VARCHAR(52), - id_calib_curves SERIAL REFERENCES calib_curves (id) ON DELETE CASCADE, - ratio FLOAT, - PRIMARY KEY (id_mol, id_calib_curves) -); - -CREATE OR REPLACE FUNCTION delete_ratio_if_unused() -RETURNS TRIGGER AS $$ -BEGIN - -- Check if the order_id from the deleted row in orders is used in order_details - IF NOT EXISTS ( - SELECT 1 - FROM daughter - WHERE id_calib_curves = OLD.id_calib_curves - AND id_mol = OLD.id_mol - ) THEN - -- Delete the row from order_details if it's not used elsewhere - DELETE FROM ratio - WHERE id_calib_curves = OLD.id_calib_curves - AND id_mol = OLD.id_mol; - END IF; - - RETURN OLD; -END; -$$ LANGUAGE plpgsql; - -CREATE TRIGGER trg_delete_ratio -AFTER DELETE ON daughter -FOR EACH ROW -EXECUTE FUNCTION delete_ratio_if_unused(); - - CREATE VIEW view_calib_curve AS -SELECT calib_curves.id AS id, calib_curves.name, array_agg(ratio.id_mol) AS metabolite, +SELECT calib_curves.id AS id, calib_curves.name, + array_agg(DISTINCT daughter.id_mol) AS metabolite, calib_curves.date_create, calib_curves.date_achieve -FROM calib_curves, ratio -WHERE calib_curves.id = ratio.id_calib_curves +FROM calib_curves, daughter +WHERE calib_curves.id = daughter.id_calib_curves AND calib_curves.date_achieve IS NULL GROUP BY calib_curves.id; CREATE VIEW view_show_calib_curve AS -SELECT calib_curves.id AS id, calib_curves.name, array_agg(DISTINCT daughter.id_mol) AS metabolite, +SELECT calib_curves.id AS id, calib_curves.name, + array_agg(DISTINCT daughter.id_mol) AS metabolite, calib_curves.date_create, calib_curves.date_achieve FROM calib_curves LEFT JOIN daughter ON calib_curves.id = daughter.id_calib_curves diff --git a/server/api/AddCalibCurve.post.ts b/server/api/AddCalibCurve.post.ts index 016a5b3..fd073c8 100644 --- a/server/api/AddCalibCurve.post.ts +++ b/server/api/AddCalibCurve.post.ts @@ -4,9 +4,7 @@ // This API is used to add a new calibration curve to the database. -import pg from "pg"; -import { calculateRatioCalibCurve } from "./function/RegCalibCurve"; - +import pg from "@/plugins/pg"; export default defineEventHandler(async (event) => { const body = await readBody(event); @@ -44,23 +42,6 @@ export default defineEventHandler(async (event) => { } return Promise.allSettled(lPromises); }) - .then(() => { - // calculate the ratio for each metabolite - return calculateRatioCalibCurve(idCalibCurve); - }) - .then((metaRatio:{[key:string]:number}) => { - const lPromises = []; - // save the ratio in the database - for(const key in metaRatio){ - lPromises.push( - client.query(` - INSERT INTO ratio(id_calib_curves, id_mol, ratio) - VALUES ('${idCalibCurve}', '${key}', '${metaRatio[key]}')` - ) - ); - } - return Promise.allSettled(lPromises); - }) .catch((err:Error) => { return new Error("Add calibration curve fail", err); }) diff --git a/server/api/UpdateCalibCurve.post.ts b/server/api/UpdateCalibCurve.post.ts index 51aa720..82fa890 100644 --- a/server/api/UpdateCalibCurve.post.ts +++ b/server/api/UpdateCalibCurve.post.ts @@ -4,7 +4,7 @@ // this file is used to update the concentration of the daughters // of a calibration curve -import pg from "pg"; +import pg from "@/plugins/pg"; export default defineEventHandler(async (event) => { const body = await readBody(event);