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
        
        const count = await require('../../controllers/books/count')()
        settings.message = `ចំនួន​សៀវភៅ​​សរុបៈ ${count}`
        const read = await require('../../controllers/books/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 = 'book'
        
        res.render('users/book', settings)
    }else{
        res.redirect('/users')
    }
})

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

router.get('/edit/:id', async function(req, res, next) {
    if(req.session.user){
        settings.dLogo = 'ទំព័រ​កែប្រែ​សៀវភៅ'
        const book = await require('../../controllers/books/read')(false, req.params.id)

        if((req.session.user.role === 'Admin') || (req.session.user.userid === book.author)){
        
            const date = book.date.toLocaleDateString('fr-CA')
            const time = book.date.toLocaleTimeString('it-IT')
            settings.datetime = date + 'T' +  time
            settings.book = book
            settings.route = 'book'

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

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

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



module.exports = router

 

const schema = require('./schema')



async function updateBook(req){
    const bookSchema = await schema()

    const book = await bookSchema.findOne({id: req.params.id})

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



module.exports = updateBook

 

<%- 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/book/edit/<%= book.id %>' method='post'>
            <input type='text' name='title' value='<%= book.title %>' required />
            <textarea name="content" id="editor" ><%- book.content %></textarea>
            <div class='wrapper'>
                <input type='text' name='chapter' value="<%= book.chapter %>" required />
                <input type='text' name='thumb' requred value="<%= book.thumb %>" />
                <input type='datetime-local' value='<%= datetime %>' name='datetime' required />
                <input type='submit' value='ចុះ​ផ្សាយ' />
            </div>
            <input name='entries' value='<%= book.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>

        <style>
        .Main .item .thumb{
            position: relative;
            padding-top: 140%
        }
        </style>
        
        <div class='item'>
            <div class='thumb'>
                <a href='/<%= route %>/<%= book.id %>'><img src='<%- book.thumb %>' /></a>
            </div>

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

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

        <% if(book.video !== ''){ %>
            <script>
                var entries = JSON.parse('<%- book.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') %>

 

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

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

Responsive: http://www.responsinator.com