//routes/backend/category.js

import express from 'express'
const categoryRouter = express.Router()
import category from '../../controllers/backend/category.js'

categoryRouter.get('/', async function(req,res){
    await category.getPage(req,res)
})

categoryRouter.post('/', async function(req,res){
    await category.createItem(req,res)
})

categoryRouter.get('/edit/:id', async function(req,res){
    await category.getPage(req,res)
})

categoryRouter.post('/edit/:id', async function(req,res){
    await category.updateItem(req,res)
})

export default categoryRouter

 

//controllers/backend/category.js

import category from '../../models/category.js'

class Category{
    async getPage(req,res){
        if(req.session.user){
            req.settings.pageTitle = 'ទំព័រ​ជំពូក'
            req.settings.message = ''
            req.settings.route = '/admin/category'

            req.settings.items = await category.getItem(req,req.settings.dItemLimit)
            req.settings.count = await category.countItem(req)

            if(req.params.id){
                req.settings.item = await category.getItem(req,req.settings.dItemLimit,req.params.id)
            }
  
            res.render('base', {data:req.settings})
        }else{
            res.redirect('/admin/login')
        }
    }

    async createItem(req,res){
        if(req.session.user){
            if(req.session.user.role == 'Admin'){
                category.createItem(req)
            }

            res.redirect('/admin/category')
        }else{
            res.redirect('/admin/login')
        }
    }

    async updateItem(req,res){
        if(req.session.user){
            if(req.session.user.role == 'Admin'){
                category.updateItem(req)
            }

            res.redirect('/admin/category')
        }else{
            res.redirect('/admin/login')
        }
    }
}

export default new Category()

 

//models/category.js

class Category{
    async createItem(req){
        const id = Date.now() + Math.round(Math.random() * 1E9).toString()
 
        let myCategory = {
            id: id, 
            title: req.body.label,
            thumb: req.body.thumb,
            date: req.body.datetime
        }
 
        req.mydb.collection("categories").insertOne(myCategory)
    }

    async countItem(req){
        return await req.mydb.collection('categories').countDocuments()
    }

    async getItem(req,amount=10,id=false,page=0){
        if(id){
            return await req.mydb.collection("categories").findOne({id:id})
        }else if(page){
            return await req.mydb.collection("categories").find().skip(amount*page).sort({date:-1,_id:-1}).limit(amount).toArray()
        }else if(amount === 'all'){
            return await req.mydb.collection("categories").find({}, {title:1,_id:0}).sort({title:1}).toArray()
        }else{
            return await req.mydb.collection("categories").find().sort({date:-1,_id:-1}).limit(amount).toArray()
        }
    }

    async updateItem(req){
        const myquery = {id:req.params.id}
        let newvalue = {$set: {
            title: req.body.label,
            thumb: req.body.thumb,
            date: req.body.datetime
        }}
     
        await req.mydb.collection("categories").updateOne(myquery,newvalue)
    }
}

export default new Category()

 

<!--views/backend/category.ejs-->

<link href="/styles/backend/category.css" rel="stylesheet"></link>
 
<section class='content'>
    <% if('item' in data){ %>
        <form action='/admin/category/edit/<%= data.item.id %>' method='post' >
            <a>ឈ្មោះជំពូកៈ</a><input type='text' value="<%= data.item.title %>" name='label' required />
            <a>រូបសញ្ញាៈ</a><input type='text' value="<%= data.item.thumb %>" name='thumb' required />
            <a>ពេល​បង្កើតៈ</a><input type='datetime-local' value='<%= data.item.date %>' name="datetime" required />
            <a></a><input type='submit' value='បញ្ជូនទិន្នន័យ' />
        </form>
        <% delete data.item %>
        <% }else{ %>
        <form action='/admin/category' method='post' >
            <a>ឈ្មោះជំពូកៈ</a><input type='text' name='label' required />
            <a>រូបសញ្ញាៈ</a><input type='text' name='thumb' required />
            <a>ពេល​បង្កើតៈ</a><input type='datetime-local' value='' name="datetime" required />
            <a></a><input type='submit' value='បញ្ជូនទិន្នន័យ' />
        </form>
    <% } %>
</section>

 

GitHub: https://github.com/Sokhavuth/Node.js_backend

Vercel: https://vercel.com/sokhavuth/node-js-backend