diff --git a/components/FormMolecule.vue b/components/FormMolecule.vue index b74cf28..b864dce 100644 --- a/components/FormMolecule.vue +++ b/components/FormMolecule.vue @@ -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 @@ -271,7 +288,7 @@ function update() { validate-on="blur" persistent :disabled="props.action === 'view'" - @submit.prevent="props.action === 'modify' ? update : add" + @submit.prevent="add" > diff --git a/components/ManageMolecule.async.vue b/components/ManageMolecule.async.vue index bcb3063..9900835 100644 --- a/components/ManageMolecule.async.vue +++ b/components/ManageMolecule.async.vue @@ -29,6 +29,7 @@ function add(){ { +export default defineEventHandler(async (event: any) => { // get what to you want to do const what = getRouterParam(event, "what"); // get the method @@ -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); }); \ No newline at end of file diff --git a/server/api/molecule/functions.ts b/server/api/molecule/functions.ts index 8dd1c23..50b097e 100644 --- a/server/api/molecule/functions.ts +++ b/server/api/molecule/functions.ts @@ -34,7 +34,7 @@ const getCheck = () => { return true; }) .catch(() =>{ - console.log('Database is empty'); + console.log('error Database is empty'); return false; }); } @@ -90,7 +90,7 @@ function getSynonym(id: number): Promise { 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)) @@ -103,6 +103,7 @@ function getSynonym(id: number): Promise { * @returns number 0 if the operation is successful, 1 otherwise */ function addMolecule(mol: tMolecule): Promise { + console.log(mol); // Insert a new molecule into the database return queryDatabase(` @@ -121,7 +122,11 @@ function addMolecule(mol: tMolecule): Promise { return Promise.all(aPromises); }) .then(() => 0) - .catch(() => 1); + .catch((error) => { + console.log(error); + + return 1; + }); } /** @@ -151,7 +156,7 @@ function addEquivalents(idMolecule: number, equivalents: number[]): function addSynonyms(idMolecule: number, synonyms: string[], user: boolean): Promise { 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]); }); @@ -199,7 +204,7 @@ function updateMolecule(mol: tMolecule): Promise { function updateSynonyms(idMolecule: number, userSyn: string[]): Promise { // 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(() => {