diff --git a/.gitignore b/.gitignore index 1dcef2d..d2abad2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -.env \ No newline at end of file +.env +db.sqlite diff --git a/database/db.js b/database/db.js index 12bd836..8d07d51 100644 --- a/database/db.js +++ b/database/db.js @@ -1,5 +1,5 @@ const { readFileSync } = require("node:fs"); -const { join } = requite("node:path"); +const { join } = require("node:path"); const Database = require("better-sqlite3"); const db = new Database(process.env.DB_FILE); @@ -9,6 +9,4 @@ const schema = readFileSync(schemaPath, "utf-8"); db.exec(schema); -//hello!! - module.exports = db; \ No newline at end of file diff --git a/database/schema.sql b/database/schema.sql index 1909b50..04a0ea0 100644 --- a/database/schema.sql +++ b/database/schema.sql @@ -3,9 +3,9 @@ BEGIN; CREATE TABLE IF NOT EXISTS blog_posts ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, - blog_post TEXT, - created_at DATETIME + blogpost TEXT, + created_at DATETIME, likes INTEGER DEFAULT 0 -) +); COMMIT; \ No newline at end of file diff --git a/database/seed.sql b/database/seed.sql index 610268a..df3a878 100644 --- a/database/seed.sql +++ b/database/seed.sql @@ -1,9 +1,10 @@ BEGIN; -INSERT INTO tasks VALUES - (1, 'Create my first post', '2022-09-16 01:01:01', 1), - (2, 'Life is good', '2022-09-16 11:10:07', 0), - (3, 'Today is a sunny day', '2022-09-16 23:59:59', 1) +INSERT INTO blog_posts VALUES + (1, 'Fran', 'Create my first post', '2022-09-16 01:01:01', 1), + (2, 'Lucy', 'Life is good', '2022-09-16 11:10:07', 0), + (3, 'Lucien', 'Today is a sunny day', '2022-09-16 23:59:59', 1), + (4, 'Phoebe', 'Happy coding guys', '2022-09-26 23:59:59', 1) ON CONFLICT(id) DO NOTHING; COMMIT; \ No newline at end of file diff --git a/model/blogs.js b/model/blogs.js index 77e9d2c..d9a29e9 100644 --- a/model/blogs.js +++ b/model/blogs.js @@ -3,18 +3,18 @@ const db = require("../database/db") //create functions here: const create_blog = db.prepare(/*sql*/ ` - INSERT into blog_posts (name, blog_post) - VALUES($name, $blogPost) - RETURNING name, blog_post, created_at, likes + INSERT into blog_posts (name, blogpost) + VALUES($name, $blogpost) + RETURNING name, blogpost, created_at, likes `) function createBlog (blog) { - return createBlog.get(blog) + return create_blog.get(blog) } //read functions here: const read_blogs = db.prepare(/*sql*/ ` - SELECT name, blog_post, created_at, likes + SELECT name, blogpost, created_at, likes FROM blog_posts `) diff --git a/src/Routes/posts.js b/src/Routes/posts.js index d94ea86..a9b4225 100644 --- a/src/Routes/posts.js +++ b/src/Routes/posts.js @@ -1,11 +1,26 @@ const express = require("express"); const router = express.Router(); const helper = require("../helper"); +const model = require("../../model/blogs") const blogPosts = []; const errorsObject = {}; +//old server route +// router.get("/", (request, response) => { +// const requestBody = {}; +// response.render("posts", { +// title: "posts", +// blogPosts, +// errorsObject, +// requestBody, +// helper, +// }); +// }); + +//new server route- need to finish working this out!! router.get("/", (request, response) => { - const requestBody = {}; + const blogs = model.displayBlogs(); + // const requestBody = {}; response.render("posts", { title: "posts", blogPosts, @@ -15,37 +30,50 @@ router.get("/", (request, response) => { }); }); -router.post("/", (request, response) => { - const requestBody = request.body; - const name = request.body.name; - const blogpost = request.body.blogpost; +//old server route +// router.post("/", (request, response) => { +// const requestBody = request.body; + // const name = request.body.name; + // const blogpost = request.body.blogpost; //validation and error handling - const errorsObject = {}; - if (!name) { - errorsObject.nameError = "please enter your name"; - } - if (!blogpost) { - errorsObject.postError = "please enter a message"; - } + // const errorsObject = {}; + // if (!name) { + // errorsObject.nameError = "please enter your name"; + // } + // if (!blogpost) { + // errorsObject.postError = "please enter a message"; + // } - if (Object.keys(errorsObject).length > 0) { - response.render("posts", { - title: "posts", - blogPosts, - errorsObject, - requestBody, - helper, - }); - } else { - const date = new Date(); - let displayDate = date.toDateString(); - const postId = date.getTime(); - blogPosts.push({ name, blogpost, postId, displayDate }); - response.redirect("/posts"); + // if (Object.keys(errorsObject).length > 0) { + // response.render("posts", { + // title: "posts", + // blogPosts, + // errorsObject, + // requestBody, + // helper, + // }); + // } else { + // const date = new Date(); + // let displayDate = date.toDateString(); + // const postId = date.getTime(); + // blogPosts.push({ name, blogpost, postId, displayDate }); + //response.redirect("/posts"); + // } +// }); + +//new server route +router.post("/", (request, response) => { + const requestBody = request.body; + const blogEntry = { + name: requestBody.name, + blogpost: requestBody.blogpost } + model.createBlog(blogEntry); + response.redirect("/posts"); }); + router.post("/delete/:id", (req, res) => { const postId = Number(req.params.id); const index = blogPosts.findIndex((post) => post.postId === postId);