Skip to content

Commit

Permalink
#48 delete ratio because ratio calculate during exportation of project
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcellino-Palerme committed Jul 25, 2024
1 parent 04d53d0 commit 79d0c8c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 59 deletions.
44 changes: 6 additions & 38 deletions db/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 1 addition & 20 deletions server/api/AddCalibCurve.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
})
Expand Down
2 changes: 1 addition & 1 deletion server/api/UpdateCalibCurve.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 79d0c8c

Please sign in to comment.