Skip to content

Commit

Permalink
#58 add molecule
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcellino-Palerme committed Nov 7, 2024
1 parent 69b2272 commit 4851007
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
23 changes: 20 additions & 3 deletions components/FormMolecule.vue
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,25 @@ function removeEquivalent(item: any) {
* Add molecule
*/
function add() {
// TODO
event('close');
// add molecule in database
$fetch('/api/molecule/molecule', {
method: 'POST',
body: JSON.stringify({
id: molDisplay.value.id,
name: molDisplay.value.name,
formula: molDisplay.value.formula,
mass: molDisplay.value.mass,
synonyms: (molDisplay.value?.synonyms || []),
userSyns: (molDisplay.value?.userSyns || []),
equivalents: listEquivalents.value.map((val) => val.id)
})
})
.then(() => {
event('close');
})
.catch((error) => {
// TODO manage error
});
}
/**
* Update molecule
Expand All @@ -271,7 +288,7 @@ function update() {
validate-on="blur"
persistent
:disabled="props.action === 'view'"
@submit.prevent="props.action === 'modify' ? update : add"
@submit.prevent="add"
>
<v-card>
<!-- title of dialog box about action -->
Expand Down
1 change: 1 addition & 0 deletions components/ManageMolecule.async.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function add(){
<table-db-action
name-db-table="view_tab_molecule"
:add="add"
:update="dialogView"
/>
<v-dialog
v-model="dialogView"
Expand Down
19 changes: 9 additions & 10 deletions db/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -162,36 +162,35 @@ CREATE TABLE synonym
(
id_mol SERIAL REFERENCES molecule (id) ON DELETE CASCADE,
name VARCHAR(255),
user BOOLEAN DEFAULT FALSE,
is_user BOOLEAN DEFAULT FALSE,
PRIMARY KEY (id_mol, name)
);

-- view to show information of molecule in tab
CREATE VIEW view_tab_molecule AS
SELECT molecule.id AS id, molecule.name AS name, formula, mass,
COUNT(DISTINCT (equivalent.id_mol_0, equivalent.id_mol_1)) AS equivalent
COUNT(equivalent.id_mol_0) AS equivalent
FROM molecule
LEFT JOIN equivalent ON molecule.id = equivalent.id_mol_0
OR molecule.id = equivalent.id_mol_1
LEFT JOIN synonym ON molecule.id = synonym.id_mol
OR molecule.id = equivalent.id_mol_1
GROUP BY molecule.id;

-- Function to get all names of equivalent molecules
CREATE OR REPLACE FUNCTION func_equivalent_molecule(id_mol INTEGER)
CREATE OR REPLACE FUNCTION func_equivalent_molecule(in_id_mol INTEGER)
RETURNS TABLE(name VARCHAR(255)) AS $$
BEGIN
RETURN QUERY
SELECT DISTINCT molecule.name AS name
FROM molecule
LEFT JOIN equivalent ON molecule.id = equivalent.id_mol_0
OR molecule.id = equivalent.id_mol_1
WHERE molecule.id = id_mol;
JOIN equivalent ON molecule.id = equivalent.id_mol_0
OR molecule.id = equivalent.id_mol_1
WHERE molecule.id = in_id_mol;

END;
$$ LANGUAGE plpgsql;

-- Function to get all synonyms and equivalents of a molecule
CREATE OR REPLACE FUNCTION func_synonym_equivalent_molecule(id_mol INTEGER)
CREATE OR REPLACE FUNCTION func_synonym_equivalent_molecule(in_id_mol INTEGER)
RETURNS TABLE(synonyms VARCHAR[], equivalents INTEGER[]) AS $$
BEGIN
RETURN QUERY
Expand All @@ -200,7 +199,7 @@ BEGIN
LEFT JOIN equivalent ON molecule.id = equivalent.id_mol_0
OR molecule.id = equivalent.id_mol_1
LEFT JOIN synonym ON molecule.id = synonym.id_mol
WHERE molecule.id = id_mol;
WHERE molecule.id = in_id_mol;
END;
$$ LANGUAGE plpgsql;

Expand Down
5 changes: 3 additions & 2 deletions server/api/molecule/[what].ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import * as mol from "./functions"

export default defineEventHandler(async (event: any) => {
export default defineEventHandler(async (event: any) => {
// get what to you want to do
const what = getRouterParam(event, "what");
// get the method
Expand All @@ -33,6 +33,7 @@ export default defineEventHandler(async (event: any) => {
}
// In case of a post or put request, we need to get the body
const body = await readBody(event)
return await funcMap[`${method}-${what}`](body.json());

return await funcMap[`${method}-${what}`](body);

});
15 changes: 10 additions & 5 deletions server/api/molecule/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const getCheck = () => {
return true;
})
.catch(() =>{
console.log('Database is empty');
console.log('error Database is empty');
return false;
});
}
Expand Down Expand Up @@ -90,7 +90,7 @@ function getSynonym(id: number): Promise<string[] | number> {
return queryDatabase(`
SELECT name
FROM synonym
WHERE id_molecule = $1`,
WHERE id_mol = $1`,
[id])
.then((result) => result.rows.map(
(row: { name: string; }) => row.name))
Expand All @@ -103,6 +103,7 @@ function getSynonym(id: number): Promise<string[] | number> {
* @returns number 0 if the operation is successful, 1 otherwise
*/
function addMolecule(mol: tMolecule): Promise<number> {
console.log(mol);

// Insert a new molecule into the database
return queryDatabase(`
Expand All @@ -121,7 +122,11 @@ function addMolecule(mol: tMolecule): Promise<number> {
return Promise.all(aPromises);
})
.then(() => 0)
.catch(() => 1);
.catch((error) => {
console.log(error);

return 1;
});
}

/**
Expand Down Expand Up @@ -151,7 +156,7 @@ function addEquivalents(idMolecule: number, equivalents: number[]):
function addSynonyms(idMolecule: number, synonyms: string[], user: boolean): Promise<void> {
const promises = synonyms.map((synonym) => {
return queryDatabase(`
INSERT INTO synonym (id_molecule, name, user)
INSERT INTO synonym (id_mol, name, is_user)
VALUES ($1, $2, $3)`,
[idMolecule, synonym, user]);
});
Expand Down Expand Up @@ -199,7 +204,7 @@ function updateMolecule(mol: tMolecule): Promise<any> {
function updateSynonyms(idMolecule: number, userSyn: string[]): Promise<void> {
// Delete all user synonyms of the molecule from the database
return queryDatabase(`
DELETE FROM synonym WHERE id_molecule = $1 AND user = $2`,
DELETE FROM synonym WHERE id_mol = $1 AND is_user = $2`,
[idMolecule, true])
// Insert all synonyms into the database
.then(() => {
Expand Down

0 comments on commit 4851007

Please sign in to comment.