Download src/controllers/plantController.js from Mr-Thop/DigitalDB: direct link, hf CLI and curl.
- Browser
- Download file 3.72 kB
-
https://huggingface.co/spaces/Mr-Thop/DigitalDB/resolve/main/src/controllers/plantController.js
- Command line
-
hf download hf://spaces/Mr-Thop/DigitalDB/src/controllers/plantController.js
-
curl -L -o plantController.js https://huggingface.co/spaces/Mr-Thop/DigitalDB/resolve/main/src/controllers/plantController.js
3.72 kB
| const db = require('../config/database'); | |
| // GET all plants | |
| exports.getAllPlants = async (req, res) => { | |
| try { | |
| const [rows] = await db.query('SELECT * FROM plants ORDER BY created_at DESC'); | |
| res.json({ success: true, data: rows }); | |
| } catch (error) { | |
| res.status(500).json({ success: false, error: error.message }); | |
| } | |
| }; | |
| // GET single plant by ID | |
| exports.getPlantById = async (req, res) => { | |
| try { | |
| const [rows] = await db.query('SELECT * FROM plants WHERE plant_id = ?', [req.params.id]); | |
| if (rows.length === 0) { | |
| return res.status(404).json({ success: false, error: 'Plant not found' }); | |
| } | |
| res.json({ success: true, data: rows[0] }); | |
| } catch (error) { | |
| res.status(500).json({ success: false, error: error.message }); | |
| } | |
| }; | |
| // CREATE new plant | |
| exports.createPlant = async (req, res) => { | |
| try { | |
| const { local_name, scientific_name, plant_type, botanical_identification_by } = req.body; | |
| const [result] = await db.query( | |
| 'INSERT INTO plants (local_name, scientific_name, plant_type, botanical_identification_by) VALUES (?, ?, ?, ?)', | |
| [local_name, scientific_name, plant_type, botanical_identification_by] | |
| ); | |
| res.status(201).json({ | |
| success: true, | |
| message: 'Plant created successfully', | |
| data: { plant_id: result.insertId } | |
| }); | |
| } catch (error) { | |
| res.status(500).json({ success: false, error: error.message }); | |
| } | |
| }; | |
| // UPDATE plant | |
| exports.updatePlant = async (req, res) => { | |
| try { | |
| const { local_name, scientific_name, plant_type, botanical_identification_by } = req.body; | |
| const [result] = await db.query( | |
| 'UPDATE plants SET local_name = ?, scientific_name = ?, plant_type = ?, botanical_identification_by = ? WHERE plant_id = ?', | |
| [local_name, scientific_name, plant_type, botanical_identification_by, req.params.id] | |
| ); | |
| if (result.affectedRows === 0) { | |
| return res.status(404).json({ success: false, error: 'Plant not found' }); | |
| } | |
| res.json({ success: true, message: 'Plant updated successfully' }); | |
| } catch (error) { | |
| res.status(500).json({ success: false, error: error.message }); | |
| } | |
| }; | |
| // DELETE plant | |
| exports.deletePlant = async (req, res) => { | |
| try { | |
| const [result] = await db.query('DELETE FROM plants WHERE plant_id = ?', [req.params.id]); | |
| if (result.affectedRows === 0) { | |
| return res.status(404).json({ success: false, error: 'Plant not found' }); | |
| } | |
| res.json({ success: true, message: 'Plant deleted successfully' }); | |
| } catch (error) { | |
| res.status(500).json({ success: false, error: error.message }); | |
| } | |
| }; | |
| // GET plant alternate names | |
| exports.getPlantAlternateNames = async (req, res) => { | |
| try { | |
| const [rows] = await db.query( | |
| 'SELECT * FROM plant_alternate_names WHERE plant_id = ?', | |
| [req.params.id] | |
| ); | |
| res.json({ success: true, data: rows }); | |
| } catch (error) { | |
| res.status(500).json({ success: false, error: error.message }); | |
| } | |
| }; | |
| // ADD alternate name | |
| exports.addAlternateName = async (req, res) => { | |
| try { | |
| const { alternate_name, language_or_dialect } = req.body; | |
| const [result] = await db.query( | |
| 'INSERT INTO plant_alternate_names (plant_id, alternate_name, language_or_dialect) VALUES (?, ?, ?)', | |
| [req.params.id, alternate_name, language_or_dialect] | |
| ); | |
| res.status(201).json({ | |
| success: true, | |
| message: 'Alternate name added successfully', | |
| data: { alt_name_id: result.insertId } | |
| }); | |
| } catch (error) { | |
| res.status(500).json({ success: false, error: error.message }); | |
| } | |
| }; | |