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}`
        const read = await require('../../controllers/posts/read')

        if(req.session.user.role === 'Admin'){
            settings.items = await read(settings.dItemLimit)
        }else{
            settings.items = await read(settings.dItemLimit, null, null, req.session.user.userid)
        }
        
        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')
    }
})

router.get('/edit/:id', async function(req, res, next) {
    if(req.session.user){
        settings.dLogo = 'ទំព័រ​កែប្រែ​ការផ្សាយ'
        const post = await require('../../controllers/posts/read')(false, req.params.id)

        if((req.session.user.role === 'Admin') || (req.session.user.userid === post.author)){
            settings.categories = await require('../../controllers/categories/read')('all')
        
            const date = post.date.toLocaleDateString('fr-CA')
            const time = post.date.toLocaleTimeString('it-IT')
            settings.datetime = date + 'T' +  time
            settings.post = post
            settings.route = 'post'

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

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

router.post('/edit/:id', async function(req, res, next){
    if(req.session.user){
        await require('../../controllers/posts/update')(req)
        
        res.redirect('/users/post/edit/' + req.params.id)
    }else{
        res.redirect('/users')
    }
})

router.get('/delete/:id', async function(req, res, next){
    if(req.session.user){
        const post = await require('../../controllers/posts/read')(false, req.params.id)

        if((req.session.user.role === 'Admin') || (req.session.user.userid === post.author)){
            await require('../../controllers/posts/delete')(req)
        }
  
        res.redirect('/users/post')
  
    }else{
        res.redirect('/users')
    }
})

router.post('/paginate', async function(req, res, next){
    if(req.session.user){
        const read = await require('../../controllers/posts/read')

        if(req.session.user.role === 'Admin'){
            settings.items = await read(settings.dItemLimit, null, req.body.page)
        }else{
            settings.items = await read(settings.dItemLimit, null, req.body.page, req.session.user.userid)
        }

      res.json({items: settings.items})
    }else{
      res.redirect('/users')
    }
  })



module.exports = router

 

var page = 0

function paginate(route){
    $('.paginate img').attr('src', '/images/loading.gif')
    page += 1

    $.post(`/users/${route}/paginate`, {page: page}, function(data, status){
        appendItem(data.items, route)
    })
}

function appendItem(items, route){
    var html = ''

    for(let v in items){
        html += `<div class='item'>`
        html += `<div class='thumb'>`
        html += `<a href='/${ route }/${ items[v].id }'><img src='${ items[v].thumb }' /></a>`
        html += `</div>`

        html += `<div class='title-date'>`
        html += `<a class='title' href='/${ route }/${ items[v].id }'>${ items[v].title }</a>`
        html += `<div>${ (new Date(items[v].date)).toLocaleDateString() }</div>`
        html += `</div>`

        html += `<div class='edit-delete'>`
        html += `<a href='/users/${ route }/edit/${ items[v].id }'>`
        html += `<img src='/images/edit.png' />`
        html += `</a>`
        html += `<a href='/users/${ route }/delete/${ items[v].id }'>`
        html += `<img src='/images/delete.png' />`
        html += `</a>`
        html += `</div>`
        html += `</div>` 
    }
    
    $('.paginate img').attr('src', '/images/load-more.png')
    $('.item-wrapper').append(html)

}

 

const schema = require('./schema')



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

    if(userid){
        var querry = {author: userid}
    }else{
        var querry = {}
    }

    if(id){
        return await postSchema.findOne({id: id})
    }else if(page){
        return await postSchema.find(querry).skip(amount * page).sort({date: -1, _id: -1}).limit(amount)
    }else{
        return await postSchema.find(querry).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