Skip to content

Commit

Permalink
Merge pull request #43 from FAC29A/update-server-route
Browse files Browse the repository at this point in the history
Update server route
  • Loading branch information
PAZHames authored Feb 5, 2024
2 parents ff00db6 + ada228c commit 58ed2b0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.env
.env
db.sqlite
3 changes: 2 additions & 1 deletion database/db.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -8,4 +8,5 @@ const schemaPath = join("database", "schema.sql");
const schema = readFileSync(schemaPath, "utf-8");
db.exec(schema);


module.exports = db;
6 changes: 3 additions & 3 deletions database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
9 changes: 5 additions & 4 deletions database/seed.sql
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 5 additions & 5 deletions model/blogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
`)

Expand Down
80 changes: 54 additions & 26 deletions src/Routes/posts.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit 58ed2b0

Please sign in to comment.