// 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)
})

indexRouter.get('/category/:name',async (req,res)=>{
    home.getPostByCategory(req,res)
})

indexRouter.post('/navigate',async (req,res)=>{
    home.navigateCategory(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)
        this.config.items = await postdb.getRelatedItem(req,this.config.item.categories,this.config.relatedPosts)
    
        res.render('base',{data:this.config})
    }

    async getPostByCategory(req,res){
        this.config = await config()
        this.config.pageTitle = 'ទំព័រប្រភេទ​ទំនិញ'
        this.config.route = '/category'
        this.config.name = req.params.name

        this.config.categories = await categorydb.getAllItem(req)
        this.config.items = await postdb.getRelatedItem(req,[req.params.name],this.config.maxCategories)
        
        res.render('base',{data:this.config})
    }

    async navigateCategory(req,res){
        const items = await postdb.navigateCategory(req,[req.body.name],this.config.maxCategories)
        res.json(items)
    }
}

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()
    }

    async getRelatedItem(req,mycategories,amount){
        const myquery = {categories:{$in:mycategories},id:{$ne:req.params.id}}
        return await req.mydb.collection("posts").find(myquery).sort({postdate:-1,_id:-1}).limit(amount).toArray()
    }

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

export default new Post()

 

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