// routes/front/home.js
import express from 'express'
const indexRouter = express.Router()
import home from '../../controllers/front/home.js'

indexRouter.get('/',async (req,res)=>{
    home.getItem(req,res)
})

indexRouter.post('/',async (req,res)=>{
    home.navigateItem(req,res)
})

indexRouter.get('/post/:id',async (req,res)=>{
    home.getSingle(req,res)
})

export default indexRouter

 

// controllers/front/home.js
import config from "../../config.js"
import categorydb from '../../models/category.js'
import postdb from '../../models/post.js'

class Home{
    async getItem(req,res){
        this.config = await config()
        this.config.pageTitle = 'ទំព័រដើម'
        this.config.route = '/'

        this.config.categories = await categorydb.getAllItem(req)
        this.config.items = await postdb.getItem(req,this.config.frontPosts)

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

    async navigateItem(req,res){
        this.config = await config()
        this.config.items = await postdb.paginateItem(req,this.config.frontPosts)
        res.json(this.config)
    }

    async getSingle(req,res){
        this.config = await config()
        this.config.pageTitle = 'ទំព័រ​ទំនិញ'
        this.config.route = '/post'

        this.config.categories = await categorydb.getAllItem(req)
        this.config.item = await postdb.editItem(req,req.params.id)
        
        res.render('base',{data:this.config})
    }
}

export default new Home()

 

// models/post.js

class Post{
    async count(req,query={}){
        return await req.mydb.collection('posts').countDocuments(query)
    }

    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,
            location: req.body.location,
            postdate: req.body.datetime,
            video: req.body.entries,
            userid: req.session.user.id,
        }
 
        await req.mydb.collection("posts").insertOne(newPost)
    }

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

    async editItem(req){
        return await req.mydb.collection('posts').findOne({id:req.params.id})
    }

    async updateItem(req){
        let myquery = {id: req.params.id}

        if(req.body.categories.includes(',')){
            var categories = req.body.categories.split(',')
        }else{
            var categories = [req.body.categories]
        }

        let newvalue = {$set: {
            title: req.body.title,
            content: req.body.content,
            categories: categories,
            price: req.body.price,
            thumb: req.body.thumb,
            phone: req.body.phone,
            location: req.body.location,
            postdate: req.body.datetime,
            video: req.body.entries
        }}
 
        await req.mydb.collection("posts").updateOne(myquery,newvalue)
    }

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

    async paginateItem(req,amount){
        const page = req.body.page
        return await req.mydb.collection("posts").find().skip(amount*page).sort({postdate:-1,_id:-1}).limit(amount).toArray()
    }
}

export default new Post()

 

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