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

Done :D #9

Open
wants to merge 4 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
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
# blog-components-layout
# blog-tdd-layout

> A Vue.js project

## Build Setup

``` bash
# install dependencies
npm install

# serve with hot reload at localhost:8080
npm run dev

# build for production with minification
npm run build

# build for production and view the bundle analyzer report
npm run build --report

# run unit tests
npm run unit

# run e2e tests
npm run e2e

# run all tests
npm test
```

For detailed explanation on how things work, checkout the [guide](http://vuejs-templates.github.io/webpack/) and [docs for vue-loader](http://vuejs.github.io/vue-loader).
1 change: 1 addition & 0 deletions back-end-tdd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions back-end-tdd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# blog-tdd
59 changes: 59 additions & 0 deletions back-end-tdd/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var cors = require('cors')
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/blog-tdd');

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

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error :'))
db.once('open', function(){
console.log(`Connected`)
})

app.use('/', index);
app.use('/api/artikel', artikel);
app.use('/api/user', user);
app.use('/api/login', login);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
90 changes: 90 additions & 0 deletions back-end-tdd/bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('blog-tdd:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
87 changes: 87 additions & 0 deletions back-end-tdd/controller/cArtikel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const Artikel = require("../model/Artikel");

let artikelPost = function(req,res){
let data = req.body;

let newArtikel = new Artikel({
judul : data.judul,
pesan_singkat : data.pesan_singkat,
pesan : data.pesan,
autor : data.autor
});

newArtikel.save(function(err,artikel){
if (err) {
console.log(err)
} else {
res.status(200).send(artikel)
}
})
}

let artikelDelete = function(req,res) {
let id = req.params.id;
Artikel.findById(id, function(err, artikel){
if(err){
res.status(502).send(`Something Wrong with your Mongodb : ${err}`);
} else {
artikel.remove(function(err) {
if(err){
res.status(501).send(`Something Wrong with your Mongodb : ${err}`);
} else{
res.status(200).send(`1 Document Artikel Deleted`)
}
})
}
})
}

let artikelFindById = function(req,res) {
let id = req.params.id;
Artikel.findById(id, function(err, artikel){
if(err){
res.status(502).send(`Something Wrong with your Mongodb : ${err}`);
} else {
res.status(200).send(artikel);
}
})
}

let artikelGet = function(req,res){
Artikel.find({}, function(err, artikel){
if(err){
res.status(502).send(`Something Wrong with your Mongodb : ${err}`);
} else {
res.status(200).send(artikel);
}
})
}

let artikelPut = function(req,res){
Artikel.findById(req.params.id, (err,artikel) =>{
if(err){
res.status(504).send(`Something Wrong with your Mongodb : ${err}`);
} else {
let data = req.body;
artikel.judul = data.judul;
artikel.pesan = data.pesan;
artikel.autor = data.autor;

artikel.save(function(err,newartikel){
if (err) {
res.status(506).send(`Something Wrong with your Mongodb : ${err}`);
} else {
res.status(200).send(newartikel);
}
})
}
})
}

module.exports = {
artikelPost,
artikelDelete,
artikelGet,
artikelPut,
artikelFindById
};
39 changes: 39 additions & 0 deletions back-end-tdd/controller/cLogin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const User = require('../model/User');
const bcrypt = require('bcrypt');
const saltRounds = 10;
const jwt = require('jsonwebtoken');

let login_post = function (req,res){
let input = req.body;
User.find({})
.where('username').equals(input.username)
.exec(function(err,user){
if (err) {
res.status(501).send(`Something Wrong with your login Error : ${err}`)
} else {
if (user.length <= 0) {
res.status(401).send('Username Wrong, check your username again');
} else {
bcrypt.compare(input.password, user[0].password, function(err,respone){
if(err){
res.status(501).send(`Something Wrong with your login Error : ${err}`);
} else {
if(respone){
jwt.sign({
username : user.username,
name : user.name,
email : user.email
},"scret", function(err,token){
res.status(200).send(token);
})
} else {
res.status(401).send(`Password Wrong, check your username again ${err}`);
}
}
})
}
}
})
}

module.exports = login_post
Loading