We can categorize job by its category, for example, certain jobs are in the category of software development, computer graphic, or web design. As we already created a number of categories for job, what we have to do is to pull those categories and put them in the job form.

 

// controller/admin/job.js
import config from '../../config.js'
import categories from '../../model/category.js'

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

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

        this.config.categories = await categories.getAllItem(req)

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

export default await new Job()

 

// 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 getAllItem(req){
        return await req.mydb.collection("categories").find().sort({title:1}).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})
    }

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

export default await new Category()

 

<!--view/admin/job.ejs-->
<link rel="stylesheet" href="/styles/admin/job.css" />
<script src="/scripts/ckeditor/ckeditor.js"></script>
<script src="/scripts/addCategory.js"></script>

<section class="Job">
    <form action="/admin/job" method="post">
        <input type="text" name="title" required placeholder="ចំណង​ជើង" />
        <textarea name="content" id="editor">​​​​​​​​​​​​​​</textarea>
        <input type="text" name="categories" required placeholder="ជំពូក" />
        <div class="wrapper">
            <select id="category" onChange="getCategory()">
                <option>ជ្រើសរើស​ជំពូក</option>
                <% for(let v in data.categories){ %>
                    <option><%= data.categories[v].title %></option>
                <% } %>
            </select>
            <input type="text" name="location" required placeholder="ទីកន្លែង" />
            <input type="datetime-local" name="datetime" required />
            <input type="email" name="email" required placeholder="Email" />
            <input type="submit" value="​​​​​​​​​​​​​បញ្ជូន" />
        </div>
    </form>
</section>
<script src="/scripts/ckeditor/config.js"></script>

 

// static/scripts/addCategory.js 
const getCategory = () => {
    const category = $('#category option:selected').text()
    $('select').prop('selectedIndex',0)
    let categories = $('[name=categories]').val()
    if(categories === ''){
        categories += category
    }else{
        categories += (`, ${category}`)
    }
    
    $('[name=categories]').val(categories)
}

 

 

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

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