Skip to content

Commit

Permalink
mistake correcting and server routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Frannerz committed Jan 30, 2024
1 parent 80cac30 commit ada228c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 42 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
4 changes: 1 addition & 3 deletions 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 @@ -9,6 +9,4 @@ const schema = readFileSync(schemaPath, "utf-8");
db.exec(schema);


//hello!!

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 ada228c

Please sign in to comment.