text: 'use strict'
  
  const { init } = require('./queries');
  
  let Queries = null;
  
  const start = app => {
    Queries = init();
  
    /* root */
  
    app.get('/', async (res) => {
      res.redirect('https://kpi-team-five.github.io/TeamFive/app/index.html');
    });
    
    /* auth */
    app.post('/auth', async (req, res) => {
      const body = req.body;
      const user = { id: null, login: body.login, pass: body.pass }
      const users = await Queries.UserQueries.getAll();
      let result = false;
      for (const entry of users) {
        if (entry.login == user.login && entry.pass == user.pass) {
          result = true;
          break;
        }
      }
      return res.send(result);
    });
  
    /* users */
    app.post('/users', async (req, res) => {
      const body = req.body;
      const user = { id: null, login: body.login, pass: body.pass }
      const result = await Queries.UserQueries.add(user);
      return res.send(result);
    });
  
    app.get('/users/:id', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.UserQueries.getOne(id);
      return res.send(JSON.stringify(result));
    });
  
    app.get('/users', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.UserQueries.getAll(id);
      return res.send(JSON.stringify(result));
    });
  
    app.put('/users', async (req, res) => {
      const body = req.body;
      const user = { id: null, login: body.login, pass: body.pass }
      const result = await Queries.UserQueries.update(body.id, user);
      return res.send(result);
    });
  
    app.delete('/users/:id', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.UserQueries.delete(id);
      return res.send(result);
    });
  
    /* products */
    app.post('/products', async (req, res) => {
      const body = req.body;
      const product = { id: null, name: body.name, img: body.img, price: body.price, category: body.category};
      const result = await Queries.ProductQueries.add(product);
      return res.send(result);
    });
  
    app.get('/products/:id', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.ProductQueries.getOne(id);
      return res.send(JSON.stringify(result));
    });
  
    app.get('/products', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.ProductQueries.getAll(id);
      return res.send(JSON.stringify(result));
    });
  
    app.put('/products/', async (req, res) => {
      const body = req.body;
      const product = { id: null, name: body.name, img: body.img, price: body.price, category: body.category};
      const result = await Queries.ProductQueries.update(body.id, product);
      return res.send(result);
    });
  
    app.delete('/products/:id', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.ProductQueries.delete(id);
      return res.send(result);
    });
  
    /* students */
    app.post('/students', async (req, res) => {
      const body = req.body;
      const student = { id: null, name: body.name }
      const result = await Queries.StudentQueries.add(student);
      return res.send(result);
    });
  
    app.get('/students/:id', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.StudentQueries.getOne(id);
      return res.send(JSON.stringify(result));
    });
  
    app.get('/students', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.StudentQueries.getAll(id);
      return res.send(JSON.stringify(result));
    });
  
    app.put('/students', async (req, res) => {
      const body = req.body;
      const student = { id: null, name: body.name }
      const result = await Queries.StudentQueries.update(body.id, student);
      return res.send(result);
    });
  
    app.delete('/students/:id', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.StudentQueries.delete(id);
      return res.send(result);
    });
  
    /* categories */
    app.post('/categories', async (req, res) => {
      const body = req.body;
      const category = { id: null, name: body.name }
      const result = await Queries.CategoryQueries.add(category);
      return res.send(result);
    });
  
    app.get('/categories/:id', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.CategoryQueries.getOne(id);
      return res.send(JSON.stringify(result));
    });
  
    app.get('/categories', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.CategoryQueries.getAll(id);
      return res.send(JSON.stringify(result));
    });
  
    app.put('/categories', async (req, res) => {
      const body = req.body;
      const category = { id: null, name: body.name }
      const result = await Queries.CategoryQueries.update(body.id, category);
      return res.send(result);
    });
  
    app.delete('/categories/:id', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.CategoryQueries.delete(id);
      return res.send(result);
    });
  
    /* cart */
    app.post('/cart', async (req, res) => {
      const body = req.body;
      console.log(body);
      const cart = { id: null, user: body.user, product: body.product }
      const result = await Queries.CartQueries.add(cart);
      return res.send(result);
    });
  
    app.get('/cart/:id', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.CartQueries.getOne(id);
      return res.send(JSON.stringify(result));
    });
  
    app.get('/cart', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.CartQueries.getAll(id);
      return res.send(JSON.stringify(result));
    });
  
    app.get('/cart/user/:id', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.CartQueries.getAllByUser(id);
      return res.send(JSON.stringify(result));
    });
  
    app.put('/cart', async (req, res) => {
      const body = req.body;
      const cart = { id: null, user: body.user, product: body.product }
      const result = await Queries.CartQueries.update(body.id, cart);
      return res.send(result);
    });
  
    app.delete('/cart/:id', async (req, res) => {
      const id = req.params.id;
      const result = await Queries.CartQueries.delete(id);
      return res.send(result);
    });
  
  };
  
  module.exports = {
    start,
  };
  