var express = require('express')
var router = express.Router()
var settings = require('../settings')
var session = require('express-session')
const MongoStore = require('connect-mongo')
require('dotenv').config()

router.use(session({
  secret: process.env.SECRET_KEY,
  resave: false,
  saveUninitialized: false,
  store: MongoStore.create({ mongoUrl: process.env.DATABASE_URI })
}))
  
router.get('/', async function(req, res, next) {
  settings.dLogo = 'ទំព័រ​គ្រប់គ្រង'
  settings.message = ''

  if(req.session.user){
    res.render('users/index', settings)
  }else{
    res.render('login', settings)
  }
})

router.post('/', async function(req, res, next){
  const checkUser = require('../controllers/users/checkUser')
  const result = await checkUser(req)
  settings.message = result.message

  if(result.success){
    res.render('users/index', settings)
  }else{
    res.render('login', settings)
  }

})

router.get('/logout', async function(req, res, next) {
  req.session.destroy(function(err) {
      console.log('Session was destroyed')
   })
  res.redirect('/')
})

router.get('/post', async function(req, res, next) {
  settings.dLogo = 'ទំព័រ​ការផ្សាយ'

  if(req.session.user){
    res.render('users/post', settings)
  }else{
    res.redirect('/login')
  }
})

router.get('/category', async function(req, res, next) {
  if(req.session.user){
    settings.dLogo = 'ទំព័រ​ជំពូក'

    const today = new Date()
    const date = today.toLocaleDateString('fr-CA')
    const time = today.toLocaleTimeString('it-IT')
    settings.datetime = date + 'T' +  time

    const count = await require('../controllers/categories/count')()
    settings.message = `ចំនួន​ជំពូក​សរុបៈ ${count}`
    settings.route = 'category'
    settings.items = await require('../controllers/categories/read')(settings.dItemLimit)

    res.render('users/category', settings)
  }else{
    res.redirect('/login')
  }
}) 

router.post('/category', async function(req, res, next){
  if(req.session.user){
    if(req.session.user.role === 'Admin'){
      await require('../controllers/categories/create')(req)
    }

    res.redirect('/users/category')

  }else{
    res.redirect('/login')
  }
})

router.get('/category/edit/:id', async function(req, res, next){
  if(req.session.user){
    if(req.session.user.role === 'Admin'){
      settings.dLogo = 'ទំព័រ​កែប្រែ​ជំពូក'
      
      const category = await require('../controllers/categories/read')(false, req.params.id)

      const date = category.date.toLocaleDateString('fr-CA')
      const time = category.date.toLocaleTimeString('it-IT')
      settings.datetime = date + 'T' +  time
      settings.category = category
      settings.route = 'category'
    }

    res.render('users/edit_category', settings)

  }else{
    res.redirect('/login')
  }
})

router.post('/category/edit/:id', async function(req, res, next){
  if(req.session.user){
    if(req.session.user.role === 'Admin'){
      settings.dLogo = 'ទំព័រ​កែប្រែ​ជំពូក'
      
      const category = await require('../controllers/categories/update')(req)

      const date = category.date.toLocaleDateString('fr-CA')
      const time = category.date.toLocaleTimeString('it-IT')
      settings.datetime = date + 'T' +  time
      settings.category = category
      settings.route = 'category'
    }

    res.render('users/edit_category', settings)

  }else{
    res.redirect('/login')
  }
})

module.exports = router

 

const schema = require('./schema')


async function updateCategory(req){
    const categorySchema = await schema()

    const category = await categorySchema.findOne({id: req.params.id})
    category.label = req.body.label,
    category.thumb = req.body.thumb,
    category.date = new Date(req.body.datetime)
  
    return await category.save()
  }


module.exports = updateCategory

 

<%- include('partials/header.ejs') %>
<link href="/styles/users/category.css" rel="stylesheet"></link>
  
<section class='Main region'>
    <div class='sidebar'>
        <%- include('partials/menu.ejs') %>
    </div>

    <div class='content'>
        <form action='/users/category/edit/<%= category.id %>' method='post' >
            <a>ឈ្មោះជំពូកៈ</a><input type='text' value='<%= category.label %>' name='label' required />
            <a>តំណរភ្ជាប់រូបៈ</a><input type='text' value='<%= category.thumb %>' name='thumb' required />
            <a>ពេល​បង្កើតៈ</a><input type='datetime-local' value='<%= datetime %>' name="datetime" required />
            <a></a><input type='submit' value='បញ្ជូនទិន្នន័យ' />
        
            <a></a>

            <div class='item'>
                <div class='thumb'>
                    <a href='/<%= route %>/<%= category.id %>'><img src='<%- category.thumb %>' /></a>
                </div>
    
                <div class='title-date'>
                    <a class='title' href='/<%= route %>/<%= category.id %>'><%= category.label %></a>
                    <div><%= category.date.toLocaleDateString() %></div>
                </div>
    
                <div class='edit-delete'>
                    <a href='/users/<%= route %>/delete/<%= category.id %>'>
                        <img src='/images/delete.png' />
                    </a>
                </div>
            </div>

        </form>

    </div>

</section>
    
<%- include('partials/footer.ejs') %>

 

.Main .content form{
    width: 60%;
    display: grid;
    grid-template-columns: 20% 80%;
    grid-gap: 5px;
    align-items: center;
}

.Main .content form input{
    padding: 5px 10px;
}

.Main .content form input[type=submit]{
    font: var(--body-font);
}

.Main .content form a{
    text-align: right;
}

.Main .item{
    display: grid;
    grid-template-columns: 20% auto 20%;
    grid-gap: 10px;
    align-items: center;
    background: var(--background-light);
}

.Main .item .thumb{
    position: relative;
    padding-top: 56.25%
}

.Main .item .thumb img{
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
}

.Main .item .title-date, .Main .item .title-date a{
    color: black;
}

.Main .item .edit-delete{
    text-align: right;
    padding-right: 10px;
    visibility: hidden;
}

.Main .item .edit-delete img{
    width: 35px;
}

.Main .item:hover .edit-delete{
    visibility: visible;
}

 

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

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

Responsive: http://www.responsinator.com