// routes/admin/post.js
import express from 'express'
const postRouter = express.Router()
import post from '../../controllers/admin/post.js'
postRouter.get('/',async (req,res)=>{
if(req.session.user){
post.getItem(req,res)
}else{
res.redirect('/login')
}
})
postRouter.post('/',async (req,res)=>{
if(req.session.user){
post.postItem(req,res)
}else{
res.redirect('/login')
}
})
postRouter.get('/edit/:id',async (req,res)=>{
if(req.session.user){
post.editItem(req,res)
}else{
res.redirect('/login')
}
})
export default postRouter
// controllers/admin/post.js
import config from '../../config.js'
import categorydb from '../../models/category.js'
import postdb from '../../models/post.js'
class Post{
async getItem(req,res){
this.config = await config()
this.config.pageTitle = 'ទំព័រទំនិញ'
this.config.route = '/admin/post'
this.config.type = 'post'
this.config.categories = await categorydb.getAllItem(req)
this.config.count = await postdb.count(req)
this.config.items = await postdb.getItem(req,this.config.maxPosts)
res.render('base',{data:this.config})
}
async postItem(req,res){
await postdb.postItem(req)
res.redirect('/admin/post')
}
async editItem(req,res){
this.config = await config()
this.config.pageTitle = 'ទំព័រកែប្រែទំនិញ'
this.config.route = '/admin/post'
this.config.type = 'post'
this.config.categories = await categorydb.getAllItem(req)
this.config.count = await postdb.count(req)
this.config.item = await postdb.editItem(req)
this.config.items = await postdb.getItem(req,this.config.maxPosts)
res.render('base',{data:this.config})
}
}
export default new Post()
// models/post.js
class Post{
async count(req){
return await req.mydb.collection('posts').countDocuments()
}
async postItem(req){
const id = Date.now() + Math.round(Math.random() * 1E9).toString()
if(req.body.categories.includes(',')){
var categories = req.body.categories.split(',')
}else{
var categories = [req.body.categories]
}
let newPost = {
id: id,
title: req.body.title,
content: req.body.content,
categories: categories,
price: req.body.price,
thumb: req.body.thumb,
phone: req.body.phone,
email: req.body.email,
postdate: req.body.datetime,
video: req.body.entries,
userid: req.session.user.id,
}
await req.mydb.collection("posts").insertOne(newPost)
}
async getItem(req,amount){
return await req.mydb.collection("posts").find().sort({date:-1,_id:-1}).limit(amount).toArray()
}
async editItem(req){
return await req.mydb.collection('posts').findOne({id:req.params.id})
}
}
export default new Post()
<!--views/admin/post.ejs-->
<link rel="stylesheet" href="/styles/admin/post.css" />
<script src="/scripts/ckeditor/ckeditor.js"></script>
<script src="/scripts/addCategory.js"></script>
<script src="/scripts/video.js"></script>
<section class="Post">
<% if(data.item){ %>
<form action="/admin/post/edit/<%= data.item.id %>" method="post">
<input type="text" name="title" value="<%= data.item.title %>"
required placeholder="ឈ្មោះទំនិញ" />
<textarea name="content" id="editor"><%= data.item.content %></textarea>
<input type="text" name="categories" value="<%= data.item.categories.toString() %>"
required placeholder="ប្រភេទទំនិញ" />
<div class="wrapper">
<select id="category" onChange="getCategory()">
<option>ជ្រើសរើសប្រភេទទំនិញ</option>
<% for(let category of data.categories){ %>
<option><%= category.title %></option>
<% } %>
</select>
<input type="number" name="price" value="<%= data.item.price %>"
required placeholder="$00.00" />
<input type="text" name="thumb" value="<%= data.item.thumb %>"
required placeholder="តំណរភ្ជាប់រូបតំណាង" />
<input type="submit" value="ចុះផ្សាយ" />
<input type="tel" name="phone" value="<%= data.item.phone %>"
pattern="^[0-9-+\s()]*$" required placeholder="លេខទូរស័ព្ទ" />
<input type="email" name="email" value="<%= data.item.email %>"
placeholder="Email" />
<input type="datetime-local" name="datetime" value="<%= data.item.postdate %>"
required />
<input type="button" />
</div>
<input name='entries' value='<%= data.item.video %>' type='hidden' />
</form>
<% }else{ %>
<form action="/admin/post" method="post">
<input type="text" name="title" required placeholder="ឈ្មោះទំនិញ" />
<textarea name="content" id="editor"></textarea>
<input type="text" name="categories" required placeholder="ប្រភេទទំនិញ" />
<div class="wrapper">
<select id="category" onChange="getCategory()">
<option>ជ្រើសរើសប្រភេទទំនិញ</option>
<% for(let category of data.categories){ %>
<option><%= category.title %></option>
<% } %>
</select>
<input type="number" name="price" required placeholder="$00.00" />
<input type="text" name="thumb" required placeholder="តំណរភ្ជាប់រូបតំណាង" />
<input type="submit" value="ចុះផ្សាយ" />
<input type="tel" name="phone" pattern="^[0-9-+\s()]*$" required placeholder="លេខទូរស័ព្ទ" />
<input type="email" name="email" placeholder="Email" />
<input type="datetime-local" name="datetime" required />
<input type="button" />
</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>
</section>
<script src="/scripts/ckeditor/config.js"></script>