// 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)
}
async searchItem(req,res){
this.config = await config()
this.config.pageTitle = 'ទំព័រស្វែងរក'
this.config.route = '/search'
this.config.categories = await categorydb.getAllItem(req)
this.config.items = await postdb.searchItem(req,this.config.maxSearch)
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()
}
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()
}
async searchItem(req,amount){
if(req.body.fsearch === 'ទាំងអស់'){
var myquery = {$or:[{title:{$regex:req.body.q}},{content:{$regex:req.body.q}}]}
}else{
var myquery = {categories:{$in:[req.body.fsearch]},$or:[{title:{$regex:req.body.q}},{content:{$regex:req.body.q}}]}
}
return await req.mydb.collection("posts").find(myquery).limit(amount).toArray()
}
}
export default new Post()