const express = require('express');
const { sequelize } = require('../models');
const { QueryTypes } = require('sequelize');

const router = express.Router();

router.get('/', async (req, res) => {
  try {
    res.json(await sequelize.query(`
      SELECT *
      FROM services
      `, {
      logging: false,
      type: QueryTypes.SELECT
    }))
  } catch (error) {
    console.error(error)
  } finally {
    res.end()
  }
})

router.get('/:id(\\d+)', async (req, res) => {
  try {
    res.json(await sequelize.query(`
      SELECT *
      FROM services
      WHERE service_id = ${req.params.id}
      `, {
        logging: false,
        type: QueryTypes.SELECT
      }))
    } catch (error) {
      console.error(error)
    } finally {
      res.end()
    }
})

module.exports = router;