| const db = require('../config/database'); | |
| exports.getAllPlantParts = async (req, res) => { | |
| try { | |
| const [rows] = await db.query('SELECT * FROM plant_parts'); | |
| res.json({ success: true, data: rows }); | |
| } catch (error) { | |
| res.status(500).json({ success: false, error: error.message }); | |
| } | |
| }; | |
| exports.getPlantPartById = async (req, res) => { | |
| try { | |
| const [rows] = await db.query('SELECT * FROM plant_parts WHERE part_id = ?', [req.params.id]); | |
| if (rows.length === 0) { | |
| return res.status(404).json({ success: false, error: 'Plant part not found' }); | |
| } | |
| res.json({ success: true, data: rows[0] }); | |
| } catch (error) { | |
| res.status(500).json({ success: false, error: error.message }); | |
| } | |
| }; | |
| exports.createPlantPart = async (req, res) => { | |
| try { | |
| const { part_name } = req.body; | |
| const [result] = await db.query( | |
| 'INSERT INTO plant_parts (part_name) VALUES (?)', | |
| [part_name] | |
| ); | |
| res.status(201).json({ | |
| success: true, | |
| message: 'Plant part created successfully', | |
| data: { part_id: result.insertId } | |
| }); | |
| } catch (error) { | |
| res.status(500).json({ success: false, error: error.message }); | |
| } | |
| }; | |
| exports.updatePlantPart = async (req, res) => { | |
| try { | |
| const { part_name } = req.body; | |
| const [result] = await db.query( | |
| 'UPDATE plant_parts SET part_name = ? WHERE part_id = ?', | |
| [part_name, req.params.id] | |
| ); | |
| if (result.affectedRows === 0) { | |
| return res.status(404).json({ success: false, error: 'Plant part not found' }); | |
| } | |
| res.json({ success: true, message: 'Plant part updated successfully' }); | |
| } catch (error) { | |
| res.status(500).json({ success: false, error: error.message }); | |
| } | |
| }; | |
| exports.deletePlantPart = async (req, res) => { | |
| try { | |
| const [result] = await db.query('DELETE FROM plant_parts WHERE part_id = ?', [req.params.id]); | |
| if (result.affectedRows === 0) { | |
| return res.status(404).json({ success: false, error: 'Plant part not found' }); | |
| } | |
| res.json({ success: true, message: 'Plant part deleted successfully' }); | |
| } catch (error) { | |
| res.status(500).json({ success: false, error: error.message }); | |
| } | |
| }; | |