data mysql server
Published: 2024-01-26 22:40:21

const express = require('express')
const cors = require('cors')

 const db = require("./Mysql")

const port = 5000
const app =express()

app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))



app.get('/api/products', (req, res) => {
    db.getAllProducts((error, products) => {
        if (error) {
            res.status(500).json({ error: 'Internal Server Error' })
        } else {
            res.status(200).json(products)
        }
    })
})


app.post('/api/products', (req, res) => {
  const productData = req.body

  db.createProduct(productData, (error, productId) => {
      if (error) {
          res.status(500).json({ error: 'Internal Server Error' })
      } else {
          res.status(201).json({ id: productId, message: 'Product created successfully' })
      }
  })
})


app.put('/api/products/:id', (req, res) => {
  const productId = req.params.id
  const updatedData = req.body

  db.updateProduct(productId, updatedData, (error, affectedRows) => {
      if (error) {
          res.status(500).json({ error: 'Internal Server Error' })
      } else if (affectedRows === 0) {
          res.status(404).json({ error: 'Product not found' })
      } else {
          res.status(200).json({ message: 'Product updated successfully' })
      }
  })
})


app.delete('/api/products/:id', (req, res) => {
  const productId = req.params.id

  db.deleteProduct(productId, (error, affectedRows) => {
      if (error) {
          res.status(500).json({ error: 'Internal Server Error' })
      } else if (affectedRows === 0) {
          res.status(404).json({ error: 'Product not found' })
      } else {
          res.status(200).json({ message: 'Product deleted successfully' })
      }
  })
})




app.listen(port, () => {
  console.log(`listening on ${port}`)
})


///////////////// mysql 



const mysql = require('mysql2')

const config = {
  host: 'localhost',
  user: 'root',
  password: 'root',
  database: 'tekstore',
}

const connection = mysql.createConnection(config)

connection.connect((err) => {
  if (err) {
    console.error('Error connecting to MySQL:', err)
  } else {
    console.log('MySQL connected')
  }
})



const getAllProducts = (callback) => {
  connection.query('SELECT * FROM product', (error, results) => {
    if (error) {
      callback(error, null)
    } else {
      callback(null, results)
    }
  })
}

const createProduct = (productData, callback) => {
  connection.query('INSERT INTO product SET ?', productData, (error, results) => {
    if (error) {
      callback(error, null)
    } else {
      callback(null, results.insertId)
    }
  })
}

const updateProduct = (productId, updatedData, callback) => {
  connection.query('UPDATE product SET ? WHERE id = ?', [updatedData, productId], (error, results) => {
    if (error) {
      callback(error, null)
    } else {
      callback(null, results.affectedRows)
    }
  })
}

const deleteProduct = (productId, callback) => {
  connection.query('DELETE FROM product WHERE id = ?', productId, (error, results) => {
    if (error) {
      callback(error, null)
    } else {
      callback(null, results.affectedRows)
    }
  })
}

module.exports = { getAllProducts, createProduct, updateProduct, deleteProduct }