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



router.get('/', async function(req, res, next) {
    settings.dLogo = 'ទំព័រ​ការផ្សាយ'
  
    if(req.session.user){
        const today = new Date()
        const date = today.toLocaleDateString('fr-CA')
        const time = today.toLocaleTimeString('it-IT')
        settings.datetime = date + 'T' +  time

        settings.categories = await require('../../controllers/categories/read')('all')

        const count = await require('../../controllers/posts/count')()
        settings.message = `ចំនួន​ការផ្សាយ​សរុបៈ ${count}`
        settings.items = await require('../../controllers/posts/read')(settings.dItemLimit)
        settings.route = 'post'
        
        res.render('users/post', settings)
    }else{
        res.redirect('/users')
    }
})

router.post('/', async function(req, res, next) {
    if(req.session.user){
        await require('../../controllers/posts/create')(req)
        res.redirect('/users/post')
    }else{
        res.redirect('/users')
    }
})



module.exports = router

 

const schema = require('./schema')



async function getPost(amount=10, id=false, page=0){
    const postSchema = await schema()

    if(id){
        return await postSchema.findOne({id: id})
    }else if(page){
        return await postSchema.find().skip(amount * page).sort({date: -1, _id: -1}).limit(amount)
    }else{
        return await postSchema.find().sort({date: -1, _id: -1}).limit(amount)
    }
}



module.exports = getPost

 

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

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

Responsive: http://www.responsinator.com