//routes/admin/book.js
import express from 'express'
const bookRoute = express.Router()

bookRoute.get('/',async function(req,res){
    if(req.session.user){
        const module = await import('../../controllers/admin/book/read.js')
        module.default(req,res)
    }else{
        res.redirect('/admin/login')
    }
})

bookRoute.post('/',async function(req,res){
    if(req.session.user){
        const module = await import('../../controllers/admin/book/create.js')
        module.default(req,res)
    }else{
        res.redirect('/admin/login')
    }
})

bookRoute.get('/edit/:id',async function(req,res){
    if(req.session.user){
        const module = await import('../../controllers/admin/book/read.js')
        module.default(req,res)
    }else{
        res.redirect('/admin/login')
    }
})

bookRoute.post('/edit/:id',async function(req,res){
    if(req.session.user){
        const module = await import('../../controllers/admin/book/update.js')
        module.default(req,res)
    }else{
        res.redirect('/admin/login')
    }
})

export default bookRoute

 

//controllers/admin/book/update.js
import settings from '../../../settings.js'
import read from '../../../models/book/read.js'
import update from '../../../models/book/update.js'

let mySettings = JSON.parse(JSON.stringify(settings))

export default async (req,res)=>{
    let item = await read(req,mySettings.dItemLimit,req.params.id)
    if((req.session.user.role === 'Admin')||(req.session.user.userid === item.userid)){
        await update(req)
    }
    res.redirect('/admin/book')
}

 

//models/book/update.js

export default async (req)=>{
    const myquery = {id:req.params.id}
    let newvalue = {$set: {
        title: req.body.title,
        content: req.body.content,
        thumb: req.body.thumb,
        video: req.body.entries,
        category: req.body.category,
        date: new Date(req.body.datetime),
        bookTitle: req.body.bookTitle,
        bookCover: req.body.bookCover,
        bookChapter: req.body.bookChapter
    }}
    
    await req.mydb.collection("books").updateOne(myquery,newvalue)
}

 

<!--views/admin/book.ejs-->
<link rel='stylesheet' href='/styles/admin/post.css' />
<script src="/scripts/ckeditor/ckeditor.js"></script>
<script src="/scripts/admin/video.js"></script>

<section class='Main'>
    <div class='content'>
        
        <% if(data.item){ %>
        <form action='/admin/book/edit/<%= data.item.id %>' method='post' >
            <input type='text' name='title' value='<%= data.item.title %>' placeholder='ចំណងជើង' required />
            <textarea name="content" id="editor" ><%= data.item.content %></textarea>
            <div class='wrapper'>
                <select name='category' class='category' >
                    <% for(let v in data.categories){ %>
                    <option><%= data.categories[v].title %></option>
                    <% } %>
                </select>
                <script>$(".category").val("<%= data.item.category %>").change()</script>
                <input type='text' name='thumb' value='<%= data.item.thumb %>' 
                    required placeholder="តំណរ​ភ្ជាប់​រូប​តំណាង" />
                <input type='datetime-local' value='<%= data.item.date %>' name='datetime' 
                    required />
                <input type='submit' value='ចុះ​ផ្សាយ' />
                <input type="text" value="<%= data.item.bookTitle %>" name="bookTitle" 
                    required placeholder="ឈ្មោះ​សៀវភៅ" />
                <input type="text" value="<%= data.item.bookCover %>" name="bookCover" 
                    required placeholder="រូប​ក្រប​សៀវភៅ" />
                <input type="text" value="<%= data.item.bookChapter %>" name="bookChapter" 
                    required placeholder="ជំពូក​សៀវភៅ" />
                <input type="button" value="" />
            </div>
            <input name='entries' value='<%- data.item.video %>' type='hidden' />
        </form>
        <% }else{ %>
        <form action='/admin/book' method='post' >
            <input type='text' name='title' placeholder='ចំណងជើង' required />
            <textarea name="content" id="editor" ></textarea>
            <div class='wrapper'>
                <select name='category'>
                    <% for(let v in data.categories){ %>
                    <option><%= data.categories[v].title %></option>
                    <% } %>
                </select>
                <input type='text' name='thumb' required placeholder="តំណរ​ភ្ជាប់​រូប​តំណាង" />
                <input type='datetime-local' value='' name='datetime' required />
                <input type='submit' value='ចុះ​ផ្សាយ' />
                <input type="text" value="" name="bookTitle" 
                    required placeholder="ឈ្មោះ​សៀវភៅ" />
                <input type="text" value="" name="bookCover" 
                    required placeholder="រូប​ក្រប​សៀវភៅ" />
                <input type="text" value="" name="bookChapter" 
                    required placeholder="ជំពូក​សៀវភៅ" />
                <input type="button" value="" required />
            </div>
            <input name='entries' value='' 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>
                <option>~ ចប់</option>
            </select>
            <input onclick='genJson()' type="button" value="បញ្ចូល​វីដេអូ" />
        </div>
 
        <table class='viddata'></table>
        
        <% if(data.item){ %>
            <script>
                var entries = JSON.parse('<%- data.item.video %>')
            </script>
        <% }else{ %>
            <script>
                var entries = ''
            </script>
        <% } %>
        <% delete data.item %>

        <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>

 

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

Vercel: https://khmerweb-blog.vercel.app/