// route/admin/category.js
import express from "express"
const categoryRoute = express.Router()
import category from '../../controller/admin/category.js'

categoryRoute.get('/',async (req,res,next) => {
    if(req.session.user){
        category.getItem(req,res)
    }else{
        res.redirect('/login')
    }
})

categoryRoute.post('/',async (req,res,next) => {
    if(req.session.user){
        category.postItem(req,res)
    }else{
        res.redirect('/login')
    }
})

categoryRoute.get('/edit/:id',async (req,res,next) => {
    if(req.session.user){
        category.getEditItem(req,res)
    }else{
        res.redirect('/login')
    }
})

categoryRoute.post('/edit/:id',async (req,res,next) => {
    if(req.session.user){
        category.postEditItem(req,res)
    }else{
        res.redirect('/login')
    }
})

categoryRoute.get('/delete/:id',async (req,res,next) => {
    if(req.session.user){
        category.deleteItem(req,res)
    }else{
        res.redirect('/login')
    }
})

export default categoryRoute

 

// controller/admin/category.js
import config from '../../config.js'
import categoryDB from '../../model/category.js'

class Category{
    constructor(){
        (async () => {
            this.config = await config()
        })()
    }

    async getItem(req,res){
        this.config.pageTitle = 'ទំព័រ​ជំពូក'
        this.config.route = '/admin/category'
        this.config.type = 'category'

        this.config.count = await categoryDB.count(req)
        this.config.items = await categoryDB.getItem(req,this.config.maxPosts)

        res.render('base',{data:this.config})
    }

    async postItem(req,res){
        await categoryDB.insertItem(req,res)
        res.redirect('/admin/category')
    }

    async getEditItem(req,res){
        this.config.pageTitle = 'ទំព័រ​កែប្រែជំពូក'
        this.config.route = '/admin/category'
        this.config.type = 'category'

        this.config.count = await categoryDB.count(req)
        this.config.items = await categoryDB.getItem(req,this.config.maxPosts)
        this.config.item = await categoryDB.getEditItem(req)

        res.render('base',{data:this.config})
    }

    async postEditItem(req,res){
        await categoryDB.postEditItem(req)

        res.redirect('/admin/category')
    }

    async deleteItem(req,res){
        await categoryDB.deleteItem(req)

        res.redirect('/admin/category')
    }

}

export default await new Category()

 

// model/category.js

class Category{
    async count(req){
        return await req.mydb.collection('categories').countDocuments()
    }

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

    async getItem(req,amount){
        return await req.mydb.collection("categories").find().sort({date:-1,_id:-1}).limit(amount).toArray()
    }

    async getEditItem(req){
        return await req.mydb.collection("categories").findOne({id:req.params.id})
    }

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

    async deleteItem(req){
        await req.mydb.collection("categories").deleteOne({id: req.params.id})
    }
}

export default await new Category()

 

GitHub: https://github.com/Sokhavuth/khmerweb-job

Heroku: https://khmerweb-job.herokuapp.com