| const db = require('../config/database');
|
|
|
|
|
| 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 });
|
| }
|
| };
|
|
|
|
|
| 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 });
|
| }
|
| };
|
|
|
|
|
| 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 });
|
| }
|
| };
|
|
|
|
|
| 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 });
|
| }
|
| };
|
|
|
|
|
| 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 });
|
| }
|
| };
|
|
|
|
|
| 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 });
|
| }
|
| };
|
|
|
|
|
| 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 });
|
| }
|
| };
|
|
|