Skip to content

Commit

Permalink
Merge pull request #20 from afif1400/main
Browse files Browse the repository at this point in the history
issue #13
  • Loading branch information
anasvakyathodi authored Jun 6, 2021
2 parents 8db1201 + 91639ec commit 32c10d1
Show file tree
Hide file tree
Showing 9 changed files with 470 additions and 300 deletions.
7 changes: 4 additions & 3 deletions blog-backend/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//This config file use: Store all configuration information about the server. A way of centralizing all the configuration for our server.
module.exports={
"secretKey":"12345-67890-09846-54123" //secret key to sign in our json web token.
}
module.exports = {
secretKey: '12345-67890-09846-54123',
MONGODB_URI: 'mongodb://localhost:27017/devpad', //secret key to sign in our json web token.
};
42 changes: 25 additions & 17 deletions blog-backend/models/postModel.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
const mongoose = require("mongoose");
const mongoose = require('mongoose');
const user = require('./users');

const postSchema = mongoose.Schema({
title: {
type: String,
required: true,
},
createdAt: {
type: Number,
required: true,
},
tags: {
type: [String],
},
html: {
type: String,
required: true,
},
// this is a reference to the user document and the
// author can be retreieved from the reference
authorId: {
type: mongoose.Schema.Types.ObjectId,
ref: user,
required: true,
},
title: {
type: String,
required: true,
},
createdAt: {
type: Number,
required: true,
},
tags: {
type: [String],
},
html: {
type: String,
required: true,
},
});

module.exports = Post = mongoose.model("post", postSchema);
module.exports = Post = mongoose.model('post', postSchema);
33 changes: 18 additions & 15 deletions blog-backend/models/users.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
var mongoose=require("mongoose");
var Schema=mongoose.Schema;
var passportLocalMongoose=require("passport-local-mongoose");
var User=new Schema({
username:{
type: String,
default:' '
},
password:{
type: String,
default:' '
},

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');
var User = new Schema({
fullName: {
type: String,
required: true,
},
username: {
type: String,
default: ' ',
},
password: {
type: String,
default: ' ',
},
});
User.plugin(passportLocalMongoose); //Adding passportLocalMongoose as a plugin which will add support to username and storage of hashed password.
User.plugin(passportLocalMongoose); //Adding passportLocalMongoose as a plugin which will add support to username and storage of hashed password.

//Exporting model with User Schema from this module.
module.exports=mongoose.model("User",User);
module.exports = mongoose.model('User', User);
147 changes: 147 additions & 0 deletions blog-backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion blog-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongoose": "^5.10.4"
"mongoose": "^5.10.4",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^6.1.0"
}
}
47 changes: 25 additions & 22 deletions blog-backend/routes/postRoutes.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
const express = require("express");
const express = require('express');
const router = express.Router();
const Post = require("../models/postModel");
const authenticate = require("../authenticate");
const Post = require('../models/postModel');
const authenticate = require('../authenticate');

router.post("/", authenticate.verifyUser,async (req, res) => {
const data = req.body;
const newPost = Post(data);
await newPost.save((error) => {
if (error) {
res.json({ msg: error });
return;
} else {
return res.json({
msg: "successfully saved !!!",
});
}
});
router.post('/', authenticate.verifyUser, async (req, res) => {
//here the data must also contain the authorId, need to send in the
// body along with other attributes.
const data = req.body;

const newPost = Post(data);
await newPost.save((error) => {
if (error) {
res.json({ msg: error });
return;
} else {
return res.json({
msg: 'successfully saved !!!',
});
}
});
});

router.get("/", async (req, res) => {
const data = await Post.find();
res.json(data);
router.get('/', async (req, res) => {
const data = await Post.find();
res.json(data);
});

router.get("/:id", async (req, res) => {
const data = await Post.findOne({ _id: req.params.id });
res.json(data);
router.get('/:id', async (req, res) => {
const data = await Post.findOne({ _id: req.params.id });
res.json(data);
});

module.exports = router;
Loading

0 comments on commit 32c10d1

Please sign in to comment.