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')
    }
})

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')
    }
})



module.exports = router

 

<%- include('partials/header.ejs') %>
<link href="/styles/users/post.css" rel="stylesheet"></link>
<script src="/scripts/ckeditor/ckeditor.js"></script>
<script src="/scripts/users/post.js"></script>
  
<section class='Main region'>
    <div class='sidebar'>
        <%- include('partials/menu.ejs') %>
    </div>

    <div class='content'>
        <form action='/users/post/edit/<%= post.id %>' method='post'>
            <input type='text' name='title' value='<%= post.title %>' required />
            <textarea name="content" id="editor" ><%- post.content %></textarea>
            <div class='wrapper'>
                <select name='category' >
                    <% for(let v in categories){ %>
                    <option><%= categories[v].title %></option>
                    <% } %>
                </select>
                <input type='text' name='thumb' requred value="<%= post.thumb %>" />
                <input type='datetime-local' value='<%= datetime %>' name='datetime' required />
                <input type='submit' value='ចុះ​ផ្សាយ' />
            </div>
            <input name='entries' value='<%= post.video %>' type='hidden' />
        </form>

        <div class='form'>
            <select name='type'>
                <option>YouTube</option>
                <option>YouTubePlaylist</option>
                <option>Facebook</option>
                <option>OK</option>
                <option>Dailymotion</option>
                <option>Vimeo</option>
            </select>
            <input name='id' type='text' placeholder="អត្តសញ្ញាណវីដេអូ" required />
            <select name='ending'>
                <option>ចប់​ហើយ</option>
                <option>មិន​ទាន់ចប់</option>
            </select>
            <input onclick='genJson()' type="button" value="បញ្ចូល​វីដេអូ" />
        </div>

        <table class='viddata'></table>
        
        <div class='item'>
            <div class='thumb'>
                <a href='/<%= route %>/<%= post.id %>'><img src='<%- post.thumb %>' /></a>
            </div>

            <div class='title-date'>
                <a class='title' href='/<%= route %>/<%= post.id %>'><%= post.title %></a>
                <div><%= post.date.toLocaleDateString() %></div>
            </div>

            <div class='edit-delete'>
                <a href='/users/<%= route %>/delete/<%= post.id %>'>
                    <img src='/images/delete.png' />
                </a>
            </div>
        </div>

        <% if(post.video !== ''){ %>
            <script>
                var entries = JSON.parse('<%- post.video %>')
            </script>
        <% }else{ %>
            <script>
               var entries = []
            </script>
        <% } %>

        <script>
        if(entries.length > 0){

            let html = ``
            for(let v in entries){
                episode += 1
                html += `<tr>`
                html += `<td title="Delete" onClick="deleteRow(event)" class="episode">${episode}</td>`
                html += `<td class="td${episode}">${entries[v].type}</td>`
                html += `<td class="td${episode}">${entries[v].id}</td>`
                html += `<td class="td${episode}">${entries[v].ending}</td>`
                html += `</tr>`
            }

            if($('.viddata').html() === ''){
                $('.viddata').append('<tr>')
                $('.viddata').append('<th>ភាគ/លុប</th>')
                $('.viddata').append('<th>ប្រភេទ​</th>')
                $('.viddata').append('<th>អត្តសញ្ញាណ​</th>')
                $('.viddata').append('<th>ចប់ឬ​នៅ?</th>')
                $('.viddata').append('</tr>')
            }

            $('.viddata').append(`${html}`)

        }
        </script>
        
        <script src="/scripts/ckeditor/config.js"></script>
    </div>
</section>
    
<%- include('partials/footer.ejs') %>

 

const schema = require('./schema')



async function updatePost(req){
    const postSchema = await schema()

    const post = await postSchema.findOne({id: req.params.id})

    post.title = req.body.title
    post.content = req.body.content
    post.thumb = req.body.thumb
    post.video = req.body.entries
    post.category = req.body.category
    post.date = new Date(req.body.datetime)
  
    return await post.save()
  }



module.exports = updatePost

 

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

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

Responsive: http://www.responsinator.com