-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from afif1400/main
issue #13
- Loading branch information
Showing
9 changed files
with
470 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.