var express = require('express')
var router = express.Router()
var settings = require('../../settings')



router.get('/', async function(req, res, next) {
    settings.dLogo = 'ទំព័រ​ការផ្សាយ'
  
    if(req.session.user){
        const today = new Date()
        const date = today.toLocaleDateString('fr-CA')
        const time = today.toLocaleTimeString('it-IT')
        settings.datetime = date + 'T' +  time

        settings.categories = await require('../../controllers/categories/read')('all')
        
        res.render('users/post', settings)
    }else{
        res.redirect('/users')
    }
})

router.post('/', async function(req, res, next) {
    if(req.session.user){
        await require('../../controllers/posts/create')(req)
        res.redirect('/users/post')
    }else{
        res.redirect('/users')
    }
})



module.exports = router

 

const setDbConnection = require('../setDbConnection')
const mongoose = require('mongoose')



async function setSchema(){
    await setDbConnection()

    const postSchema = new mongoose.Schema({
        id: {type: String, required: true},
        title: {type: String, required: true},
        content: {type: String, required: false},
        thumb: {type: String, required: true},
        video: {type: String, required: false},
        category: {type: String, required: false},
        date: {type: Date, required: true},
        author: {type: String, required: true},
    })

    mongoose.models = {}
    const post = mongoose.model('posts', postSchema)

    return post

}



module.exports = setSchema

 

const schema = require('./schema')



async function createPost(req){
    const psotSchema = await schema()

    const id = (new Date()).getTime().toString(36) + Math.random().toString(36).slice(2)
    const title = req.body.title
    const content = req.body.content
    const thumb = req.body.thumb
    const video = req.body.entries
    const category = req.body.category
    const date = new Date(req.body.datetime)
    const author = req.session.user.userid

    const post = new psotSchema({id: id, title: title, content: content, thumb: thumb, video: video, category: category, date: date, author: author})
    return await post.save()
}



module.exports = createPost

 

GitHub: https://github.com/Sokhavuth/tv

Vercel: https://tv-neon.vercel.app

Responsive: http://www.responsinator.com