//telepitett modulok "importalasa"
const express = require('express')
const mysql = require('mysql')
const app = express()
const port = 3000

//termekbol hany darab van

//backend vegpont (endpoint)
app.get('/', (req, res) => {
  res.send('Hello World!')
})

//termek tipus lekerdezes (adatb)
app.get('/termektipus', (req, res) => {
    const connection = mysql.createConnection({
      host: 'localhost',
      user: 'root',
      password: '',
      database: 'termekadatb'
    })

    connection.connect()

    connection.query('SELECT * from termektipus', (err, rows, fields) => {
      if (err) throw err

      console.log('The solution is: ', rows[0].termek_nev)
      res.send(rows)//nem lehet szoveget ele irni
    })

    connection.end()
    
    //res.send('Hello World!')
})

//termek lekerdezese (adatb)
app.get('/termek', (req, res) => {
    const connection = mysql.createConnection({
      host: 'localhost',
      user: 'root',
      password: '',
      database: 'termekadatb'
    })

    connection.connect()

    connection.query('SELECT * from termek', (err, rows, fields) => {
      if (err) throw err

      //console.log('The solution is: ', rows[0].termek_nev)
      res.send(rows)//nem lehet szoveget ele irni
    })

    connection.end()
    
    //res.send('Hello World!')
})

//termektipus db lekerdezes
app.get('/termekdb', (req, res) => {
    const connection = mysql.createConnection({
      host: 'localhost',
      user: 'root',
      password: '',
      database: 'termekadatb'
    })

    connection.connect()

    connection.query('select termektipus.termek_nev,count(termek.termek_id) as Darab from termek inner join termektipus on termektipus.termektipus_id = termek.termek_tipusid group by termektipus.termek_nev', (err, rows, fields) => {
      if (err) throw err

      //console.log('The solution is: ', rows[0].termek_nev)
      res.send(rows)//nem lehet szoveget ele irni
    })

    connection.end()
    
    //res.send('Hello World!')
})
  

app.listen(port, () => {
  console.log(`Example app listening on port http://localhost:${port}`)
})