const port = 20113
  , express = require("express")
  , expressLogging = require('express-logging')
  , logger = require('logops')
  , fetch = require("node-fetch")
  , Category = require("./src/Category")
  , Product = require("./src/Product")
  , Order = require("./src/Order")



const dateFormat = require('dateformat');

const melon = {
  data: {
    url: 'https://www.melon.com',
  },
  methods: {
    ccm: function () {
      const fetchingData = async () => {
        const res = fetch(melon.data.url + '/genre/song_list.htm?gnrCode=GN2100')
          .then(res => res.text())
          .then(body => {

          });
      };
      fetchingData();
    }
  }
}

// cron.schedule('0 * * * *', () => {
//   console.log('Fetching Melon CCM List Successfully');
// });

var app = express();
app.use(expressLogging(logger));
app.use(express.json())
app.listen(port, () => {
  console.log("Server running on port " + port);
});


app.get('/category', (req, res, next) => {
  const category = new Category()
  console.log(req.query.categoryCode)
  category.getCategory(req.query.categoryCode).then(body => {
    res.json({ code: 200, data: body })
  })
})

app.get('/categories', (req, res, next) => {
  const category = new Category()
  category.getCategories().then(body => {
    res.json({ code: 200, data: body })
  })
})

app.get('/product', (req, res, next) => {
  const product = new Product()
  product.getProductPage(req.query).then(body => {
    res.json({ code: 200, data: body })
  })
})

app.get('/product/:sellerProductId', (req, res, next) => {
  const product = new Product()
  product.getProduct(req.params).then(body => {
    res.json({ code: 200, data: body })
  })
})

app.post('/product', (req, res, next) => {
  const product = new Product()  
  product.postProduct(req.body.products).then(body => {
    res.json({ code: 200, data: body })
  })
})

app.delete('/product', (req, res, next) => {
  const product = new Product()  
  product.deleteProduct(req.body.products).then(body => {
    res.json({ code: 200, data: body })
  })
})

app.get('/orders', (req, res, next) => {
  const order = new Order()  
  order.getOrders(req.query).then(body => {
    res.json({ code: 200, data: body })
  })
})


