Skip to content

Commit

Permalink
Installation page and system update.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunny Jirakit authored and Sunny Jirakit committed May 25, 2023
1 parent edc8d57 commit 9c0c973
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 28 deletions.
44 changes: 24 additions & 20 deletions routes/install.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
module.exports = (app,sha256) => {
let db = require('../database')
const timeStamp = require('./modules/timestamp')
//default admin account
const username = "admin"
const password = sha256(process.env.DEFAULT_PASSWORD)

//SQL Check Install Commands
contents_check = "SELECT * FROM contents"
Expand All @@ -17,7 +14,9 @@ module.exports = (app,sha256) => {
admin_account_install = "INSERT INTO admin(username,password) VALUES('admin','"+sha256(process.env.DEFAULT_PASSWORD)+"')"
profile_install = "CREATE TABLE profile(id INT(6) AUTO_INCREMENT PRIMARY KEY, admin INT(6) NOT NULL, name VARCHAR(50) NOT NULL, bio VARCHAR(500), picture VARCHAR(200) DEFAULT '/image/default_profile.png')"
pages_install = "CREATE TABLE pages(id INT(6) AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, address VARCHAR(50) NOT NULL,HTML MEDIUMTEXT NOT NULL)"

default_contents = "INSERT INTO `contents` (`id`, `title`, `contents`, `writter`, `is_show`, `cover`, `category`)VALUES(1, 'Test Article 1', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', '1', 0, '/image/default.jpeg', 'test'),(2, 'Test Article 2', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', '1', 0, '/image/default.jpeg', 'test'),(3, 'Test Article 3', '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>\r\n', '1', 0, '/image/default.jpeg', 'test');"
default_profile = "INSERT INTO `profile` (`id`, `admin`, `name`, `bio`, `picture`)VALUES(1, 1, 'Admin', '<p>Default Admin Profile</p>\r\n', '/image/default_profile.png')"

let tables_status = {
contents: false,
admin: false,
Expand All @@ -37,12 +36,15 @@ module.exports = (app,sha256) => {
})
}

function check(sql) {
db.query(sql, (err,result) => {
if(err && err.code == "ER_NO_SUCH_TABLE") {
return false
}
return true
function check_table(sql) {
return new Promise((resolve) => {
db.query(sql, (err,result) => {
if(err && err.code == "ER_NO_SUCH_TABLE") {
resolve(false)
} else {
resolve(true)
}
})
})
}

Expand All @@ -52,26 +54,28 @@ module.exports = (app,sha256) => {
await promise_install(profile_install).catch(reject => {timeStamp(reject)})
await promise_install(pages_install).catch(reject => {timeStamp(reject)})
await promise_install(admin_account_install).catch(reject => {timeStamp(reject)})
await promise_install(default_contents).catch(reject => {timeStamp(reject)})
await promise_install(default_profile).catch(reject => {timeStamp(reject)})
}

function check_install(tables_status) {
tables_status.contents = check(contents_check)
tables_status.admin = check(admin_check)
tables_status.profile = check(profile_check)
tables_status.pages = check(pages_check)
async function check_install() {
await check_table(contents_check).then(resolve => {tables_status.contents = resolve})
await check_table(admin_check).then(resolve => {tables_status.admin = resolve})
await check_table(profile_check).then(resolve => {tables_status.profile = resolve})
await check_table(pages_check).then(resolve => {tables_status.pages = resolve})
}


app.get("/install", (req,res) => {
console.log(tables_status)
res.render("install", {tables_status:tables_status})
res.end()
check_install().then(() => {
res.render("install", {tables_status:tables_status})
res.end()
})
})

app.get("/install/start", (req,res) => {
install().then(() => {
res.cookie('alert', 'successfullyinstall')
res.redirect('/')
res.redirect('/install')
res.end()
})
})
Expand Down
5 changes: 4 additions & 1 deletion routes/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ module.exports = (app,sha256) => {
const is_admin = require('./modules/check_admin')(req,res)
var alert
db.query("SELECT * FROM contents ORDER BY id DESC LIMIT 0,6", (err,contents) => {
if(err) throw err;
if(err && err.code == "ER_NO_SUCH_TABLE") {
res.redirect('/install')
res.end()
}
db.query("SELECT category FROM contents GROUP BY category", (err,category) => {
if(err) throw err;
if(req.cookies.alert != undefined) {
Expand Down
8 changes: 8 additions & 0 deletions routes/modules/default_page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = (err) => {
var timeStamp = require('../modules/timestamp')

if(err.code == ER_NO_SUCH_TABLE) {
res.render('default')
res.end()
}
}
16 changes: 16 additions & 0 deletions views/default.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Default Pages | Sunny-Framework</title>
<%-include('include/head')%>
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
</head>
<body>
<%-include('include/nav')%>
<br>
<div class="container card white rounded p-20" id="mainpage">
<h1>This is a default page of Sunny-Framework.</h1>
<p>if contents were added to the databases. Home page will be change.</p>
</div>
</body>
</html>
1 change: 1 addition & 0 deletions views/home.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</head>
<body>
<%-include('include/nav')%>
<%-include('include/alert')%>
<div class="container white p-30" id="mainpage">
<div class="media-x" id="big">
<div class="cover" style="height: auto;"><img class="w-100" src="<%=contents[0].cover%>" /></div>
Expand Down
2 changes: 1 addition & 1 deletion views/include/alert.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
Swal.fire({
icon: 'success',
title: 'Successfully Installed!',
text: 'Default sql tables has been created!',
text: 'Default SQL Tables has been created!',
})
</script>
<% } %>
Expand Down
19 changes: 13 additions & 6 deletions views/install.ejs
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<title>Home | Sunny-Framework</title>
<title>Installation Page | Sunny-Framework</title>
<%-include('include/head')%>
<meta name="robots" content="noindex">
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
</head>
<body>
<%-include('include/nav')%>
<%
let installed = false
if(tables_status.contents == true && tables_status.admin == true && tables_status.profile == true && tables_status.pages == true) {
installed = true
}
%>
<br>
<div class="container card rounded white p-20" id="mainpage">
<h2 class="ml-10">Installation.</h2>
<ul>
<li>Contents table status: </li>
<li>Admin table status: </li>
<li>Profile table status: </li>
<li>Pages table status: </li>
<li>Contents table status: <%if(tables_status.contents == true) {%><span class="text-green">Installed</span><%} else {%><span class="text-red">Not Installed</span><%}%></li>
<li>Admin table status: <%if(tables_status.admin == true) {%><span class="text-green">Installed</span><%} else {%><span class="text-red">Not Installed</span><%}%></li>
<li>Profile table status: <%if(tables_status.profile == true) {%><span class="text-green">Installed</span><%} else {%><span class="text-red">Not Installed</span><%}%></li>
<li>Pages table status: <%if(tables_status.pages == true) {%><span class="text-green">Installed</span><%} else {%><span class="text-red">Not Installed</span><%}%></li>
</ul>
<button class="btn darkblue" onclick="window.location.href='/install/start'">Install</button>
<button class="btn darkblue" onclick="window.location.href='/install/start'" <%if(installed == true) {%>disabled<%}%> >Install</button>
</div>
</body>
</html>

0 comments on commit 9c0c973

Please sign in to comment.