Posts

Showing posts from January, 2021

Adding User Pagination Using Ajax Call

Image
// controllers/dashboard/author.js class Author{ constructor(){ this.deepcopy = require('deepcopy'); this.vdict = require('../../config'); this.utility = require('../../utility'); this.usersdb = require('../../models/usersdb'); this.emailCheck = require('email-check'); this.bcrypt = require('bcryptjs'); } async getAuthor(req, res){ const self = this; const data = this.deepcopy(this.vdict); data.site_title = 'ទំព័រ​អ្នក​និពន្ធ'; data.date = this.utility.setDate(); data.authors = await this.usersdb.selectUser(this.vdict.dashboardLimit); data.thumbs = self.utility.getThumbUrl(data.authors, 'author'); data.count = await self.usersdb.countUser(); if(req.params.authorId){ data.edited = await self.usersdb.selectUser(self.vdict.dashboardLimit, req.params.authorId); res.render('dashboard/author', data); }else res.render('...

Adding «DELETE» User Functionality

Image
// controllers/dashboard/author.js class Author{ constructor(){ this.deepcopy = require('deepcopy'); this.vdict = require('../../config'); this.utility = require('../../utility'); this.usersdb = require('../../models/usersdb'); this.emailCheck = require('email-check'); this.bcrypt = require('bcryptjs'); } async getAuthor(req, res){ const self = this; const data = this.deepcopy(this.vdict); data.site_title = 'ទំព័រ​អ្នក​និពន្ធ'; data.date = this.utility.setDate(); data.authors = await this.usersdb.selectUser(this.vdict.dashboardLimit); data.thumbs = self.utility.getThumbUrl(data.authors, 'author'); data.count = await self.usersdb.countUser(); if(req.params.authorId){ data.edited = await self.usersdb.selectUser(self.vdict.dashboardLimit, req.params.authorId); res.render('dashboard/author', data); }else res.render('...

Refactoring the Code Using Promise/Async/Await

Image
// controllers/dashboard/author.js class Author{ constructor(){ this.deepcopy = require('deepcopy'); this.vdict = require('../../config'); this.utility = require('../../utility'); this.usersdb = require('../../models/usersdb'); this.emailCheck = require('email-check'); this.bcrypt = require('bcryptjs'); } async getAuthor(req, res){ const self = this; const data = this.deepcopy(this.vdict); data.site_title = 'ទំព័រ​អ្នក​និពន្ធ'; data.date = this.utility.setDate(); data.authors = await this.usersdb.selectUser(this.vdict.dashboardLimit); data.thumbs = self.utility.getThumbUrl(data.authors, 'author'); data.count = await self.usersdb.countUser(); if(req.params.authorId){ data.edited = await self.usersdb.selectUser(self.vdict.dashboardLimit, req.params.authorId); res.render('dashboard/author', data); }else res.render('...

Adding «UPDATE» User Functionality

Image
// controllers/dashboard/author.js class Author{ constructor(){ this.deepcopy = require('deepcopy'); this.vdict = require('../../config'); this.utility = require('../../utility'); this.usersdb = require('../../models/usersdb'); this.emailCheck = require('email-check'); this.bcrypt = require('bcryptjs'); } getAuthor(req, res){ const self = this; if(req.session.user){ const data = this.deepcopy(this.vdict); data.site_title = 'ទំព័រ​អ្នក​និពន្ធ'; data.date = this.utility.setDate(); this.usersdb.selectUser(this.vdict.dashboardLimit, function(authors){ data.authors = authors; data.thumbs = self.utility.getThumbUrl(authors, 'author'); self.usersdb.countUser(function(count){ data.count = count; if(req.params.authorId){ self.usersdb.selectUser(self.vdict.dashboardLimit, function(author){ data.edit...

Adding «READ» User Functionality

Image
// controllers/dashboard/author.js class Author{ constructor(){ this.deepcopy = require('deepcopy'); this.vdict = require('../../config'); this.utility = require('../../utility'); this.usersdb = require('../../models/usersdb'); this.emailCheck = require('email-check'); } getAuthor(req, res){ const self = this; if(req.session.user){ const data = this.deepcopy(this.vdict); data.site_title = 'ទំព័រ​អ្នក​និពន្ធ'; data.date = this.utility.setDate(); this.usersdb.selectUser(this.vdict.dashboardLimit, function(authors){ data.authors = authors; data.thumbs = self.utility.getThumbUrl(authors, 'author'); self.usersdb.countUser(function(count){ data.count = count; res.render('dashboard/author', data); }); }); }else{ res.redirect('/admin/login'); } } postAuthor(req, res){ const self = t...

Adding «CREATE» User Functionality

Image
// routes/admin.js const express = require('express'); const router = express.Router(); const utility = require('../utility'); router.get('/', function(req, res, next){ if(req.session.user) res.redirect('/admin/dashboard'); else res.redirect('/admin/login'); }); router.get('/login', function(req, res, next){ const login = require('../controllers/login'); login.getLogin(res); }); router.post('/login', function(req, res, next){ const login = require('../controllers/login'); login.postLogin(req, res); }); router.get('/dashboard', function(req, res, next){ const index = require('../controllers/dashboard/index'); index.getIndex(req, res); }); router.get('/logout', function(req, res, next){ if(req.session.user){ req.session.destroy(function (err) { res.redirect('/'); }); }else{ res.redirect('/'); } }); router.get('/auth...

Adding Login Functionality

Image
// routes/users.js const express = require('express'); const vdict = require('../config'); const router = express.Router(); const usersdb = require('../models/usersdb') router.get('/', function(req, res, next){ if(req.session.user) res.send('dashboard'); else res.redirect('/users/login'); }); router.get('/login', function(req, res, next){ res.render('login', vdict); }); router.post('/login', function(req, res, next){ usersdb.users.findOne({username:req.body.username}, function (err, user){ if (err) return console.error(err); if(user){ if(usersdb.bcrypt.compareSync(req.body.password, user.password)){ req.session.user = user.username; vdict.message = ''; res.send('dashboard'); }else{ vdict.message = 'ពាក្យ​សំងាត់​មិនត្រឹមត្រូវ​ទេ'; res.redirect('/users/login'); } }else{ vdict.message = ...

Creating User Database

Image
// app.js var createError = require('http-errors'); var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser'); var logger = require('morgan'); const mongoose = require('mongoose'); const vdict = require('./config'); const databaseAccess = vdict.databaseAccess; mongoose.connect(databaseAccess, {useNewUrlParser: true, useUnifiedTopology: true}); process.env.TZ = "Asia/Phnom_Penh"; var indexRouter = require('./routes/index'); var usersRouter = require('./routes/users'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', indexRouter); ap...

Connecting to YouTube Channel

Image
<!--views/index.ejs--> <%- include('partials/head') %> <%- include('partials/facebookapi') %> <%- include('partials/header') %> <link rel='stylesheet' href='/stylesheets/index.css' /> <script src='/javascripts/utility.js'></script> <section class="panel region"> <script src="https://apis.google.com/js/api.js"></script> <script src="/javascripts/index.js"></script> <div id="video-screen"></div> <div id="navigation"> <img onclick="index.getVidContent(index.yt_prevPageToken)" src="/images/left.png" /> <img onclick="index.getVidContent()" src="/images/home.png" /> <img onclick="index.getVidContent(index.yt_nextPageToken)" src="/ima...

Creating Menu for the Frontend

Image
// routes/index.js const express = require('express'); const router = express.Router(); var vdict = require('../config'); const utility = require('../utility'); router.use(function(req, res, next){ vdict.khDate = utility.toKhDate(new Date()); next(); }); router.get('/', function(req, res, next) { res.render('index', vdict); }); module.exports = router; <!--views/index.ejs--> <%- include('partials/head') %> <%- include('partials/facebookapi') %> <%- include('partials/header') %> <link rel='stylesheet' href='/stylesheets/index.css' /> <script src='/javascripts/utility.js'></script> <section id="panel"></section> <section id="main"></section> <%- include('partials/footer.ejs') %> <!--views/partials/header.ejs--> <section id="header">...

Boilerplate Code

Image
// app.js var createError = require('http-errors'); var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser'); var logger = require('morgan'); var indexRouter = require('./routes/index'); var usersRouter = require('./routes/users'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', indexRouter); app.use('/users', usersRouter); // catch 404 and forward to error handler app.use(function(req, res, next) { next(createError(404)); }); // error handler app.use(function(err, req, res, next) { // set locals, only providing error in development res.loca...

Adding Search Functionality

Image
<!--templates/search.html--> {% extends "base.html" %} {% block head %} {{ super() }} <script src="/static/scripts/search.js"></script> <link href="/static/styles/search.css" rel="stylesheet"> {% endblock %} {% block ad %} <div class="ad region"> <img src="/static/images/ad.jpg" /> </div> <div style="clear: both;"></div> {% endblock %} {% block content %} <div id='content'> <h2 class="search-title">លទ្ធផល​នៃ​ការស្វែង​រកៈ</h2> {% if 'q' in data %} <script> search.q = '{{ data["q"] }}'; gapi.load('client', initAPI); </script> {% endif %} <ul id="s...

Creating Video Code Generator

Image
<script src="//cdn.ckeditor.com/4.15.1/basic/ckeditor.js"></script> <script> function setPostBody(){ var DEVobj = getFormData(); var postBody = ''; if(DEVobj.videoType=='youtube') postBody = '<a><img border="0" src="https://i.ytimg.com/vi/'+DEVobj.videoID+'/mqdefault.jpg" /></a>'; postBody += '<div class=" __video-id__" data-id="'+DEVobj.videoType+'{'+DEVobj.videoID+'}"></div>'; var pl =(DEVobj.playlist) ? 'pl':''; if(pl) postBody += '<div class="__playlist__" data-pl="'+pl+'['+DEVobj.title+']"></div>'; else postBody += '<div class="__playlist__" data-pl=""></div>'; postBody += '<div class="__descripti...

Creating Static Page Template

Image
#index.py from flask import render_template from flask_classful import FlaskView, route from models.postdb import Postdb from models.pagedb import Pagedb class Index(FlaskView): @route('/') def index(self): self.postdb = Postdb() vdict = self.postdb.get_post() return render_template('index.html', data=vdict) @route('/post/<id>/') def get_single_post(self, id): self.postdb = Postdb() vdict = self.postdb.get_post(id) return render_template('post.html', data=vdict) @route('/page/<id>/') def get_single_page(self, id): self.pagedb = Pagedb() vdict = self.pagedb.get_page(id) return render_template('page.html', data=vdict) @route('/category/<cat>/') def get_post_category(self, cat): self.postdb = Postdb() vdict = self.postdb.get_post(cat=cat) return render_template('index.html', data=vdict) #models/pagedb.py...

Adding Video Blogging and Video Playlist Functionalities

Image
<!--templates/index.html--> {% extends "base.html" %} {% block head %} {{ super() }} <script src="/static/scripts/index.js"></script> <link href="/static/styles/index.css" rel="stylesheet"> {% endblock %} {% block panel %} {% if 'cat' in data %} <script> index.label = '{{ data["cat"] }}'; </script> {% endif %} <div id="panel" class="panel region"></div> <div class="navigation region"> <img onclick="index.navPrevious()" src="/static/images/left.png" /> <img id="nav-home" onclick="index.navHome()" src="/static/images/home.png" /> <img onclick="index.navNext()" src="/static/images/right.png" /> </div> {% endblock %} {% block ad %} <div class="ad region"> ...