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

Testing Get (Green: Success passing) #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
# blog-tdd
# blog-tdd

#### List of article routes:

**Route** | **HTTP** | **Description**
---------------------|---------------|------------------------
/article | GET | get all article
/article | POST | create an article
/article/:slug | GET | get one data article
/article/:slug | PUT | update data article
/article/:slug | DELETE | delete data article

#### List of user routes:

**Route** | **HTTP** | **Description**
---------------------|---------------|------------------------
/user/signup | POST | create new user
/user/signin | POST | sign in user
/user | GET | get all user
/user/:username | GET | get one data user
/user/:username | PUT | update data user
/user/:username | DELETE | delete data user

### **USAGE**
#### With only npm:

>npm install express nodemon chai chai-http cors dotenv cors <br>
>mongodb mongoose passport passport-local passport-local-mongoose slug<br>

>npm start <br>
>npm run dev <br>

#### Running mongod:
> sudo service mongod start <br>
> check connection @robomongo <br>

Access the website via http://localhost:3000/
40 changes: 40 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var express = require('express');
var bodyParser = require('body-parser');
var morgan = require('morgan')
var cors = require('cors');

var User = require('./models/user');

var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

var index = require('./routes/index');
var article = require('./routes/article');
var user = require('./routes/user');

var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/blog')

var app = express();

app.use(morgan())
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

//LOCAL STRATEGY
passport.use(new LocalStrategy(User.authenticate()));

app.use(passport.initialize());
app.use(passport.session());

app.use(cors());

app.use('/', index);
app.use('/article', article);
app.use('/user', user);

passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.listen(3000)
72 changes: 72 additions & 0 deletions controllers/controllerArticle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict'
var Article = require('../models/article.js');
var slug = require('slug')

module.exports = {
createArticle : (req, res, next) => {
var newArticle = Article(
{
title: req.body.title,
content: req.body.content,
category: req.body.category,
slug: slug(req.body.title).toLowerCase()
})

newArticle.save((err, data) => {
if (err) {
res.send(err)
} else {
res.status(200).json(data)
}
})
},

getArticles : (req, res, next) => {
Article.find({}, (err, articles) => {
if (err) {
res.send(err)
} else {
res.status(200).json(articles);
}
})
},

getOneArticle : (req, res, next) => {
Article.find({
slug: req.params.slug
}, (err, article) => {
if (err) {
res.send(err)
} else {
res.status(200).json(article);
}
})
},

updateArticle : (req, res, next) => {
Article.findOneAndUpdate(
{
slug: req.params.slug
}, req.body, {new: true}, (err, article) => {
if (err) {
res.send(err)
} else {
res.status(200).json(article);
}
})
},

deleteArticle : (req, res, next) => {
Article.findOneAndRemove(
{
slug: req.params.slug
}, (err, data) => {
if (err) {
res.send(err)
} else {
res.status(200).json(data);
}
})
}

}
92 changes: 92 additions & 0 deletions controllers/controllerUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
var passport = require('passport');
var jwt = require('jsonwebtoken');
var passwordHash = require('password-hash')
var User = require('../models/user');
var LocalStrategy = require('passport-local').Strategy;

module.exports = {
register : (req, res, next) => {
var hashed = passwordHash.generate(req.body.password)
let user = {
username : req.body.username,
email : req.body.email,
password : hashed
}
User.create(user,(err, user) => {
if (err) {
res.json({ err : err })
} else {
res.json({ data : user });
}
})
},

signin : (req, res, next) => {
var pwd = req.body.password
var mail = req.body.email
var secret = 'qwerty123'
User.find({ where : { email : mail } })
.then(function (data) {
if(passwordHash.verify(pwd, data.password)) {
let token = jwt.sign({
email: data.mail,
username: data.username
}, secret, {});
res.json({token : token})
} else {
res.json({passerror: true})
}
}).catch(function (err) {
if (err) {
res.json({ err: err })
}
})
},

getUsers : (req, res, next) => {
User.find({}, (err, users) => {
if (err) {
res.json({ err : err })
} else {
res.json({ data : users });
}
})
},

getOneUser : (req, res, next) => {
User.findOne({
username: req.params.username
}, (err, user) => {
if (err) {
res.json(err)
} else {
res.json(user);
}
})
},

updateUser : (req, res, next) => {
User.findOneAndUpdate({
username: req.params.username
}, req.body, {new: true}, (err, user) => {
if (err) {
res.json(err)
} else {
res.json(user)
}
})
},

deleteUser : (req, res, next) => {
User.findOneAndRemove({
username: req.params.username
}, (err, user) => {
if (err) {
res.json(err)
} else {
res.json(user)
}
})
}

}
13 changes: 13 additions & 0 deletions models/article.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'
var mongoose = require('mongoose');

var articleSchema = mongoose.Schema({
title: String,
content: String,
category: String,
slug: String
})

var article = mongoose.model('Article', articleSchema)

module.exports = article
14 changes: 14 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'
var mongoose = require('mongoose');
var passportLocalMongoose = require('passport-local-mongoose');

var userSchema = mongoose.Schema({
username: String,
email: String,
password: String
})

userSchema.plugin(passportLocalMongoose)
var user = mongoose.model('User', userSchema)

module.exports = user
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "blog-tdd",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon app.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/isumizumi/blog-tdd.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/isumizumi/blog-tdd/issues"
},
"homepage": "https://github.com/isumizumi/blog-tdd#readme",
"dependencies": {
"bcrypt": "^1.0.2",
"body-parser": "^1.17.1",
"cors": "^2.8.1",
"express": "^4.15.2",
"jsonwebtoken": "^7.3.0",
"mongodb": "^2.2.24",
"mongoose": "^4.8.7",
"morgan": "^1.8.1",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^4.0.0",
"password-hash": "^1.2.2",
"slug": "^0.9.1"
},
"devDependencies": {
"chai": "^3.5.0",
"chai-http": "^3.0.0",
"mocha": "^3.2.0",
"nodemon": "^1.11.0"
}
}
Loading