Skip to content

Commit

Permalink
Implemented Database Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan-slither committed Oct 3, 2024
1 parent 315e159 commit a93395d
Show file tree
Hide file tree
Showing 7 changed files with 1,679 additions and 56 deletions.
134 changes: 133 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,133 @@
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Database
*.sqlite
25 changes: 25 additions & 0 deletions database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sqlite3 from "sqlite3";

const DBSOURCE = "db.sqlite";

const db = new sqlite3.Database(DBSOURCE, (err) => {
if (err) {
console.log(err);
throw err;
} else {
console.log("Connected");
db.run(`CREATE TABLE blogs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
content TEXT
)`, (err) => {
if (err) {
console.log("Database Already Created");
} else {
console.log("Database Created");
}
})
}
})

export default db;
65 changes: 39 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,47 @@ import express from "express";
import bodyParser from "body-parser";
import { dirname } from "path";
import { fileURLToPath } from "url";
import { title } from "process";
import db from "./database.js";

const __dirname = dirname(fileURLToPath(import.meta.url));

let blogs = [];
let editID = NaN;
const app = express();
const port = 3000;
var blogs = [];
var editID = NaN;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));

app.get("/", (req, res) => {
res.render("index.ejs", {
homeSelected : true,
editId: editID,
blogList : blogs
db.all("SELECT * FROM blogs", (err, rows) => {
if (err) {
console.log(err);
throw err;
} else {
blogs.splice(0, blogs.length);
rows.forEach(row => {
blogs.push({ [row.title]: row.content, blogId: row.id });
});
console.log("Loading Blogs");
console.log(blogs);
res.render("index.ejs", {
homeSelected : true,
editId: editID,
blogList : blogs
});
}
});
console.log(blogs);
})

app.get("/Delete/:id", (req, res) => {
const blogID = req.params.id;
console.log(blogID);
console.log(editID);
if (blogID < editID) {
editID = editID - 1;
}
blogs.splice(blogID, 1);
res.render("index.ejs", {
editId: editID,
blogList : blogs,
homeSelected : true
const deleteId = parseInt(req.params.id);
console.log(deleteId);
blogs.forEach(blog => {
if (blog.blogId === deleteId) {
db.run("DELETE FROM blogs WHERE id = (?)", [deleteId]);
}
})
res.redirect("/");
})

app.get("/Edit/:id", (req, res) => {
Expand All @@ -48,12 +57,9 @@ app.get("/Edit/:id", (req, res) => {
app.post("/Save", (req, res) => {
var changedBlog = {};
changedBlog[req.body["editTitle"]] = req.body["editContent"];
blogs[editID] = changedBlog;
res.render("index.ejs", {
homeSelected : true,
blogList : blogs
});
db.run("UPDATE blogs SET title = ?, content = ? WHERE id = ?", [req.body["editTitle"], req.body["editContent"], editID]);
editID = NaN;
res.redirect("/");
})

app.get("/Create", (req, res) => {
Expand All @@ -67,7 +73,14 @@ app.post("/Create/Submit", (req, res) => {
var newBlog = {};
newBlog[req.body["title"]] = req.body["content"];
blogs.push(newBlog);
console.log("Created");
db.run("INSERT INTO blogs (title, content) VALUES (?, ?)", [req.body["title"], req.body["content"]], (err) => {
if (err) {
console.log(err);
throw err;
} else {
console.log("Blog Inserted");
}
});
} else if (req.body.button === "clearButton") {
req.body["title"] = "";
req.body["content"] = "";
Expand All @@ -78,4 +91,4 @@ app.post("/Create/Submit", (req, res) => {

app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
});
Loading

0 comments on commit a93395d

Please sign in to comment.