var express = require('express')
var router = express.Router()
var session = require('express-session')
const MongoStore = require('connect-mongo')
require('dotenv').config()



router.use(session({
  secret: process.env.SECRET_KEY,
  resave: false,
  saveUninitialized: false,
  store: MongoStore.create({ mongoUrl: process.env.DATABASE_URI })
}))

const loginRoute = require('./users/login')
router.use('/', loginRoute) 

const postRoute = require('./users/post')
router.use('/post', postRoute) 

const categoryRoute = require('./users/category')
router.use('/category', categoryRoute) 



module.exports = router

 

var express = require('express')
var router = express.Router()
var settings = require('../../settings')



router.get('/', async function(req, res, next) {
  if(req.session.user){
    settings.dLogo = 'ទំព័រ​ជំពូក'

    const today = new Date()
    const date = today.toLocaleDateString('fr-CA')
    const time = today.toLocaleTimeString('it-IT')
    settings.datetime = date + 'T' +  time

    const count = await require('../../controllers/categories/count')()
    settings.message = `ចំនួន​ជំពូក​សរុបៈ ${count}`
    settings.route = 'category'
    settings.items = await require('../../controllers/categories/read')(settings.dItemLimit)

    res.render('users/category', settings)
  }else{
    res.redirect('/users')
  }
}) 

router.post('/', async function(req, res, next){
  if(req.session.user){
    if(req.session.user.role === 'Admin'){
      await require('../../controllers/categories/create')(req)
    }

    res.redirect('/users/category')

  }else{
    res.redirect('/users')
  }
})

router.get('/edit/:id', async function(req, res, next){
  if(req.session.user){
    if(req.session.user.role === 'Admin'){
      settings.dLogo = 'ទំព័រ​កែប្រែ​ជំពូក'
      
      const category = await require('../../controllers/categories/read')(false, req.params.id)

      const date = category.date.toLocaleDateString('fr-CA')
      const time = category.date.toLocaleTimeString('it-IT')
      settings.datetime = date + 'T' +  time
      settings.category = category
      settings.route = 'category'
    }

    res.render('users/edit_category', settings)

  }else{
    res.redirect('/users')
  }
})

router.post('/edit/:id', async function(req, res, next){
  if(req.session.user){
    if(req.session.user.role === 'Admin'){
      settings.dLogo = 'ទំព័រ​កែប្រែ​ជំពូក'
      
      const category = await require('../../controllers/categories/update')(req)

      const date = category.date.toLocaleDateString('fr-CA')
      const time = category.date.toLocaleTimeString('it-IT')
      settings.datetime = date + 'T' +  time
      settings.category = category
      settings.route = 'category'
    }

    res.render('users/edit_category', settings)

  }else{
    res.redirect('/users')
  }
})

router.get('/delete/:id', async function(req, res, next){
  if(req.session.user){
    if(req.session.user.role === 'Admin'){
      await require('../../controllers/categories/delete')(req)
    }

    res.redirect('/users/category')

  }else{
    res.redirect('/users')
  }
})



module.exports = router

 

GitHub: https://github.com/Sokhavuth/tv

Vercel: https://tv-neon.vercel.app

Responsive: http://www.responsinator.com