Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions config/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ const mongoose = require("mongoose");

const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.DB_STRING, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
});
const conn = await mongoose.connect(process.env.DB_STRING);

console.log(`MongoDB Connected: ${conn.connection.host}`);
} catch (err) {
Expand Down
19 changes: 19 additions & 0 deletions controllers/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const Comment = require("../models/Comment");

module.exports = {
createComment: async (req, res) => {
try {
await Comment.create({
comment: req.body.comment,
likes: 0,
post: req.params.id,
});
console.log(req.body);
console.log("Comment has been added!");
res.redirect("/post/"+req.params.id);
} catch (err) {
console.log(err);

}
},
};
8 changes: 6 additions & 2 deletions controllers/posts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const cloudinary = require("../middleware/cloudinary");
const Post = require("../models/Post");
const Comment = require("../models/Comment");

module.exports = {
getProfile: async (req, res) => {
Expand All @@ -21,7 +22,8 @@ module.exports = {
getPost: async (req, res) => {
try {
const post = await Post.findById(req.params.id);
res.render("post.ejs", { post: post, user: req.user });
const comments = await Comment.find({post: req.params.id}).sort({ createdAt: "desc" }).lean();
res.render("post.ejs", { post: post, user: req.user, comments: comments });
} catch (err) {
console.log(err);
}
Expand All @@ -30,7 +32,7 @@ module.exports = {
try {
// Upload image to cloudinary
const result = await cloudinary.uploader.upload(req.file.path);

console.log("BODY:", req.body)
await Post.create({
title: req.body.title,
image: result.secure_url,
Expand All @@ -39,10 +41,12 @@ module.exports = {
likes: 0,
user: req.user.id,
});
console.log(req.body);
console.log("Post has been added!");
res.redirect("/profile");
} catch (err) {
console.log(err);

}
},
likePost: async (req, res) => {
Expand Down
20 changes: 12 additions & 8 deletions middleware/multer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ const path = require("path");

module.exports = multer({
storage: multer.diskStorage({}),
fileFilter: (req, file, cb) => {
let ext = path.extname(file.originalname);
if (ext !== ".jpg" && ext !== ".jpeg" && ext !== ".png") {
cb(new Error("File type is not supported"), false);
return;
}
cb(null, true);
},
fileFilter: (req, file, cb) => {
const ext = path.extname(file.originalname).toLowerCase();
const allowedExt = [".jpg", ".jpeg", ".png", ".webp"];

const allowedMime = ["image/jpeg", "image/png", "image/webp"];

if (!allowedExt.includes(ext) || !allowedMime.includes(file.mimetype)) {
return cb(new Error("File type is not supported"), false);
}

cb(null, true);
}
});
23 changes: 23 additions & 0 deletions models/Comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const mongoose = require("mongoose");

const CommentSchema = new mongoose.Schema({
comment: {
type: String,
required: true,
},

likes: {
type: Number,
required: true,
},
post: {
type: mongoose.Schema.Types.ObjectId,
ref: "Post",
},
createdAt: {
type: Date,
default: Date.now,
},
});

module.exports = mongoose.model("Comment", CommentSchema);
Loading