Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blank post #510

Open
wants to merge 93 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
5093058
Update README.md
Lou-Martin Jun 12, 2023
8a6ff19
testing by adding colour
Jun 13, 2023
ea53192
testing - changed colour
Jun 13, 2023
203ad9a
/new changed to signup, sessions changed to login
YousefHafz Jun 13, 2023
907a363
Merge pull request #1 from Pheebs32/dev_comments
Pheebs32 Jun 13, 2023
4f9adbd
corrected the tests
Barlowi2023 Jun 13, 2023
b2212b6
Merge pull request #2 from Pheebs32/corrected_tests
Pheebs32 Jun 13, 2023
2587cad
branch test
Lou-Martin Jun 14, 2023
ccc11e7
add cypress function to clearDB for clean test environments. add task…
Lou-Martin Jun 14, 2023
be8cc58
Added authentication logic to dynamically update navbar links
Jun 14, 2023
8282c16
updated user.js to redirect to login
Jun 14, 2023
233cc00
Delete test_spec.js
Lou-Martin Jun 14, 2023
041e00e
Merge pull request #3 from Pheebs32/test
Lou-Martin Jun 14, 2023
a3ba65a
Merge pull request #4 from Pheebs32/navbar-layout-page
Lou-Martin Jun 14, 2023
8210f44
fixed navbar to redirect to post/ not log user out
Jun 14, 2023
bf80daf
Merge pull request #5 from Pheebs32/navbar-layout-page
KingJessie Jun 14, 2023
025d341
change user_can_sign_in_spec.js to not rely on posts to pass
Lou-Martin Jun 14, 2023
dfb108e
authentication added to posts/new
Barlowi2023 Jun 14, 2023
63fd8a3
change test name to match spec and code
Lou-Martin Jun 14, 2023
be9cea3
add log in tests
Lou-Martin Jun 14, 2023
c77d476
add test for clear button on log in page
Lou-Martin Jun 14, 2023
803c234
add homepage tests
Jun 14, 2023
1a97646
Pushing Test Additions
Pheebs32 Jun 14, 2023
5df5180
Merge branch 'test' of https://github.com/Pheebs32/acebook-node-proje…
Jun 14, 2023
23f6117
Merge pull request #7 from Pheebs32/test
KingJessie Jun 14, 2023
c3e3831
Merge pull request #6 from Pheebs32/navbar-update
Lou-Martin Jun 15, 2023
b73956e
add clearDb task
Lou-Martin Jun 15, 2023
67d00b2
added profile
YousefHafz Jun 15, 2023
517390e
Merge branch 'main' into profile
Jun 15, 2023
84178ed
profile update
YousefHafz Jun 15, 2023
cc9f3a1
Merge branch 'main' into profile
Barlowi2023 Jun 15, 2023
536187f
Merge branch 'profile' of https://github.com/Pheebs32/acebook-node-pr…
Barlowi2023 Jun 15, 2023
dd8095a
changes made to connect user details in browser bar/link
Jun 15, 2023
72a3bbc
Merge branch 'profile' of https://github.com/Pheebs32/acebook-milton …
Jun 15, 2023
cceeb70
Merge branch 'profile' of https://github.com/Pheebs32/acebook-node-pr…
Barlowi2023 Jun 15, 2023
64903a9
add all log in and out tests to dedicated folder
Lou-Martin Jun 15, 2023
1dbb1bf
Pushing Test Additions
Pheebs32 Jun 15, 2023
16e799d
change folder to remove whitespace
Lou-Martin Jun 15, 2023
ea925f3
Merge branch 'test' of https://github.com/Pheebs32/acebook-milton int…
Lou-Martin Jun 15, 2023
73074a7
added remaining posts test
Jun 15, 2023
289d64c
Merge branch 'test' of https://github.com/Pheebs32/acebook-node-proje…
Jun 15, 2023
8a3c311
add two timeline tests
Jun 15, 2023
924c854
added username to user.js in models and index.hbs
Barlowi2023 Jun 15, 2023
0d39a85
add entry point tests
Lou-Martin Jun 15, 2023
33e3fce
add filing system for tests
Lou-Martin Jun 15, 2023
013c181
final posts tests added
Jun 15, 2023
f4bffed
Merge branch 'test' of https://github.com/Pheebs32/acebook-node-proje…
Jun 15, 2023
b8ee9c7
added username and date/time to show in /posts
Jun 15, 2023
ce89ecd
Merge pull request #8 from Pheebs32/profile
Lou-Martin Jun 15, 2023
461ae91
Merge branch 'main' of https://github.com/Pheebs32/acebook-milton int…
Lou-Martin Jun 15, 2023
1af8980
Merge pull request #9 from Pheebs32/test
Pheebs32 Jun 15, 2023
7df0cb3
file organization
Lou-Martin Jun 15, 2023
007a9b4
Upadted Homepage and Posts tests
Jun 16, 2023
178b80a
add scripts to run all sub directories with npm command
Lou-Martin Jun 16, 2023
99e4bdd
add username to all log in/out tests
Lou-Martin Jun 16, 2023
bf972d6
Pushing Test Additions
Pheebs32 Jun 16, 2023
236d261
Added post from profile test
Jun 16, 2023
3d6d7be
Merge pull request #10 from Pheebs32/test
Pheebs32 Jun 16, 2023
93350e0
added the likes functionality and fixed broken profile link
Barlowi2023 Jun 16, 2023
d0d2dca
Merge branch 'main' of https://github.com/Pheebs32/acebook-node-proje…
Barlowi2023 Jun 16, 2023
bd004ca
Merge pull request #11 from Pheebs32/add_likes
Barlowi2023 Jun 16, 2023
b55a535
posts now appear on the profile page
YousefHafz Jun 16, 2023
f8f3c1e
minor change
Saamiya96 Jun 18, 2023
f5c5379
made changes to signup and authentication
Saamiya96 Jun 18, 2023
a81c4ae
minor change
Saamiya96 Jun 18, 2023
5ada18f
minor change
Saamiya96 Jun 18, 2023
502d091
attempting to ensure user login in details are correct
Saamiya96 Jun 18, 2023
beb7b90
Merge branch 'profile' into add_likes
Barlowi2023 Jun 19, 2023
90992df
changes before merge
YousefHafz Jun 19, 2023
babad4b
completed logout and signup func
Saamiya96 Jun 19, 2023
12246fc
creadte find mongoose command needs ammending
Saamiya96 Jun 19, 2023
78fd00a
commented out email and username func
Saamiya96 Jun 19, 2023
77ca388
added reset type to cancel buttons
Saamiya96 Jun 19, 2023
c20523c
added func to email and user so its unique in the db
Saamiya96 Jun 19, 2023
6e4fd4c
minor changes
Saamiya96 Jun 19, 2023
6d67447
minor change
Saamiya96 Jun 19, 2023
df36571
Update sessions.js
Lou-Martin Jun 19, 2023
a2a5e71
Merge pull request #12 from Pheebs32/login/signup_v2
Lou-Martin Jun 19, 2023
39284e3
Merge branch 'main' into add_likes
Barlowi2023 Jun 20, 2023
7e9a6a4
updated likes functionality and sorted posts by date
Barlowi2023 Jun 20, 2023
1d92fb5
Merge pull request #13 from Pheebs32/add_likes
Barlowi2023 Jun 20, 2023
5613a46
edit button, first steps
YousefHafz Jun 20, 2023
a0ffc7a
resolved conflict
YousefHafz Jun 20, 2023
f19f2e6
push after conflit
YousefHafz Jun 20, 2023
ffcfd94
working on edit function
YousefHafz Jun 20, 2023
4f434b2
edit function now working
YousefHafz Jun 20, 2023
beaa2e9
removed everything related to userPosts
Barlowi2023 Jun 20, 2023
93868f0
Merge branch 'profile' into blankPost
Barlowi2023 Jun 20, 2023
3c1b0be
Creates an error message if a new post is submitted empty
Barlowi2023 Jun 20, 2023
406911d
first attempt at delete
Barlowi2023 Jun 21, 2023
fbec533
added express helper for likes and delete method
Barlowi2023 Jun 21, 2023
407aef9
delete method added , new sort on profile page, new date format added…
Barlowi2023 Jun 21, 2023
89b8f7c
small changes to the nav bar to include a logged in user, new posts, …
Barlowi2023 Jun 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ It uses:

## Card wall

REPLACE THIS TEXT WITH A LINK TO YOUR CARD WALL
https://trello.com/b/u8jfD7dB/milton-acebook

## Quickstart

Expand Down
43 changes: 41 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,59 @@ const methodOverride = require("method-override");
const homeRouter = require("./routes/home");
const postsRouter = require("./routes/posts");
const sessionsRouter = require("./routes/sessions");
const usersRouter = require("./routes/users");
const usersRouter = require("./routes/users");
const expressHbs = require('express-handlebars');


const app = express();

app.engine('.hbs', expressHbs.engine({ defaultLayout: 'layout', extname: '.hbs',runtimeOptions: {
allowProtoPropertiesByDefault: true,
allowProtoMethodsByDefault: true
} }))

// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "hbs");

const hbs = expressHbs.create({});

//Date format helper
hbs.handlebars.registerHelper('formatDate', function(posts) {
const options = { weekday: 'long', day: 'numeric', month: 'long', hour: 'numeric', minute: 'numeric' };
const formattedDate = posts.date.toLocaleDateString('en-US', options);
return formattedDate;
});

//A simple helper to determine whether there has been one 'like' or multiple 'likes'
hbs.handlebars.registerHelper('if_equal', function(posts) {
if (posts.like.length === 1) {
return "like";
} else {
return "likes";
}
});

//Like button helper this checks if the session user has liked a post and if so displays the like
// button with a coloured spade if not displays the like button with a blank spade
// hbs.handlebars.registerHelper('likeButton', function(posts) {
// if (posts.like.length === 1) {
// return "like";
// } else {
// return "likes";
// }
// });



app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use(methodOverride("_method"));


app.use(
session({
key: "user_sid",
Expand All @@ -47,7 +85,7 @@ app.use((req, res, next) => {
// middleware function to check for logged-in users
const sessionChecker = (req, res, next) => {
if (!req.session.user && !req.cookies.user_sid) {
res.redirect("/sessions/new");
res.redirect("/sessions/login");
} else {
next();
}
Expand All @@ -59,6 +97,7 @@ app.use("/posts", sessionChecker, postsRouter);
app.use("/sessions", sessionsRouter);
app.use("/users", usersRouter);


// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
Expand Down
3 changes: 3 additions & 0 deletions controllers/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ const HomeController = {
};

module.exports = HomeController;


// add to this page to make the appearance more dynamic :)
107 changes: 97 additions & 10 deletions controllers/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,110 @@ const PostsController = {
if (err) {
throw err;
}

res.render("posts/index", { posts: posts });
// const dateFormatted = `${posts[0].date.getHours()}:${posts[0].date.getMinutes()}, ${posts[0].date.toDateString()}`;
// console.log(dateFormatted);

const sortedPosts = posts.flat().sort((a, b) => b.date - a.date);
// sortedPosts[0].date = dateFormatted;
// Implemented authentication logic to dynamically update navbar links based on the user's login status.
res.render("posts/index", { posts: sortedPosts, user: req.session.user, isAuthenticated: true});
});

},

New: (req, res) => {
res.render("posts/new", {});
res.render("posts/new", {user: req.session.user, isAuthenticated: true});
},

Create: (req, res) => {
const post = new Post(req.body);
post.save((err) => {
if (err) {
throw err;
}
const { message, likes } = req.body;
const username = req.session.user.username;
const post = new Post({ username, message, likes });

console.log(`message ${post.message}`)

if (post.message == '' ) {
res.render('posts/new', {error: "The post cannot be left blank!"});
} else {
post.save((err) => {
if (err) {
throw err;
}

res.status(201).redirect("/posts");
});
}
},

Edit: async (req, res) => {
const post_id = req.params.id;
console.log(`Post_id ${post_id}`);
const user_id = req.session.user._id;
console.log(`user_id ${user_id}`);

const post = await Post.findOne({ _id: post_id });
console.log(`post ${post}`);

res.render("posts/edit", { post: post , user: req.session.user, isAuthenticated: true});

},

Update: async (req, res) => {
console.log("update");
const post_id = req.params.id;
console.log(`Post_id ${post_id}`);
const user_id = req.session.user._id;
console.log(`user_id ${user_id}`);

const post = await Post.findOne({ _id: post_id });
console.log(`post ${post}`);
post.message = req.body.content
await post.save()
res.status(201).redirect(`/users/${req.session.user.username}`);

res.status(201).redirect("/posts");
});
},

Delete: async (req, res) => {
console.log("delete");
const post_id = req.params.id;
console.log(`Post_id ${post_id}`);
const user_id = req.session.user._id;
console.log(`user_id ${user_id}`);

const post = await Post.findOne({ _id: post_id })
console.log(`post = ${post}`);
await Post.deleteOne({ _id: post_id});
res.status(201).redirect(`/users/${req.session.user.username}?deleted=true`);
},

AddLike: async (req, res) => {
// get the post_id for the
const post_id = req.params.id;
console.log(`Post_id ${post_id}`);
const user_id = req.session.user._id;
console.log(`user_id ${user_id}`);
const post = await Post.findOne({ _id: post_id });
console.log(`post ${post}`);
const likes = post.like;
console.log(`likes ${likes}`);
const liked = post.like.map((like) => like.likeAuthor).includes(user_id);
console.log(`liked ${liked}`);

if (!liked) {
likes.push({ likeAuthor: user_id });
post.save(
() => {res.status(201).redirect("/posts");}
);
} else {
likes.splice(likes.map((like) => like.likeAuthor).indexOf(user_id), 1);
post.save(
() => {res.status(201).redirect("/posts");}
);
}
// res.json({ post: post_id, likes: updated_post.like.length });
// () => {res.status(201).redirect("/posts");
},

};

module.exports = PostsController;
31 changes: 20 additions & 11 deletions controllers/sessions.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
const User = require("../models/user");
const User = require('../models/user');
const bcrypt = require('bcrypt');

const SessionsController = {

New: (req, res) => {
res.render("sessions/new", {});
res.render('sessions/login', {});
},

Create: (req, res) => {
console.log("trying to log in");
console.log('trying to log in');
const email = req.body.email;
const password = req.body.password;

User.findOne({ email: email }).then((user) => {
if (!user) {
res.redirect("/sessions/new");
} else if (user.password != password) {
res.redirect("/sessions/new");
res.render('sessions/login', {error: "Invalid email or password"});
} else {
req.session.user = user;
res.redirect("/posts");
bcrypt.compare(password, user.password, function (err, result) {
if (err) {
throw err;
}
if (result === true) {
req.session.user = user;
res.redirect('/posts');
} else {
res.render('sessions/login', {error: "Invalid email or password"});
}
});
}
});
},

Destroy: (req, res) => {
console.log("logging out");
if (req.session.user && req.cookies.user_sid) {
res.clearCookie("user_sid");
}
res.redirect("/sessions/new");
res.redirect("/sessions/login");
},
};

Expand Down
22 changes: 22 additions & 0 deletions controllers/userPosts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// const User = require("../models/user");

// const UserPostsController = {
// Create: (req, res) => {
// const username = req.params.username;
// const content = req.body.content;

// User.findOneAndUpdate( { username }, { $push: { posts: { content } } }, { new: true },
// (err) => {
// if (err) {
// throw err;
// }
// res.status(201).redirect(`/users/${username}`);
// }
// );
// },
// };

// module.exports = UserPostsController;