Advertisement
ardyhim

api router

Apr 5th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express   = require('express'),
  2.     router    = express.Router(),
  3.     md5       = require('md5'),
  4.     mongoose  = require('mongoose'),
  5.     db        = require('../model/db');
  6.  
  7. function CheckLogin(req, res, next){
  8.   if(req.session.users){
  9.     next();
  10.   }else {
  11.     return res.status(401).send();
  12.   }
  13. }
  14.  
  15. router.get('/', function(req, res, next){
  16.   return res.json({
  17.     register: {
  18.       method: 'POST',
  19.       url   : 'http://localhost:3000/api/register'
  20.     },
  21.     login: {
  22.       method: 'POST',
  23.       url   : 'http://localhost:3000/api/login'
  24.     },
  25.     logout: {
  26.       method: 'GET',
  27.       url   : 'http://localhost:3000/api/logout'
  28.     },
  29.     article_add: {
  30.       method: 'POST',
  31.       url   : 'http://localhost:3000/api/article'
  32.     },
  33.     article_list: {
  34.       method: 'GET',
  35.       url   : 'http://localhost:3000/api/article'
  36.     },
  37.     article_most_vote: {
  38.       method: 'GET',
  39.       url   : 'http://localhost:3000/api/article/most/vote'
  40.     },
  41.     article_last: {
  42.       method: 'GET',
  43.       url   : 'http://localhost:3000/api/article/most/comment'
  44.     },
  45.     article_last_comment: {
  46.       method: 'GET',
  47.       url   : 'http://localhost:3000/api/article/last'
  48.     },
  49.     article_read: {
  50.       method: 'GET',
  51.       url   : 'http://localhost:3000/api/read/:id'
  52.     },
  53.     article_read_comment: {
  54.       method: 'GET',
  55.       url   : 'http://localhost:3000/api/article/read/:id/comment'
  56.     },
  57.     article_vote: {
  58.       method: 'GET',
  59.       url   : 'http://localhost:3000/api/article/vote/:id'
  60.     }
  61.   });
  62. });
  63.  
  64. // Post Register
  65. router.post('/register', function(req, res, next){
  66.   if (req.session.username) {
  67.     return res.json({message: 'is Logged'});
  68.   }
  69.   db.users.findOne({$or:[{username: req.body.username},{email:req.body.email}]},function(err,data){
  70.     if (data) {
  71.       if (data.username == req.body.username) {
  72.         return res.json({message: 'This username is available'});
  73.       }
  74.       if (data.email == req.body.email) {
  75.         return res.json({message: 'This email is available'});
  76.       }
  77.     }else {
  78.       var add = new db.users({
  79.         username  : req.body.username,
  80.         password  : md5(req.body.password),
  81.         email     : req.body.email,
  82.         phone     : '62'+req.body.phone,
  83.         birthday  : req.body.birthday
  84.       });
  85.       add.save(function(err){
  86.         if(err) throw err;
  87.         db.users.findOne({username: req.body.username}, function(err, data){
  88.           req.session.users = data;
  89.         });
  90.         return res.json({message: 'Succes Register ' + req.session.users.username});
  91.       });
  92.     }
  93.   });
  94. });
  95.  
  96. // Post login
  97. router.post('/login',function(req, res, next){
  98.   if (req.session.users) {
  99.     return res.json({message: 'is Logged'});
  100.   }
  101.   db.users.findOne({username: req.body.username}, function(err, data){
  102.     if (data) {
  103.       if (data.password !== md5(req.body.password)) {
  104.         return json({message: 'password not match'});
  105.       }
  106.       req.session.users = data;
  107.       return res.json({message: 'succes login '+ req.session.users.username});
  108.     }
  109.     return res.json({message: 'This username is available'});
  110.   });
  111. });
  112.  
  113. // get Logout
  114. router.get('/logout', function(req, res, next){
  115.   if (!req.session.users) {
  116.     return res.status(401).send();
  117.   }
  118.   req.session.destroy();
  119.   return res.json({message: 'succes logout'});
  120. });
  121.  
  122. // post Article
  123. router.post('/article', CheckLogin, function(req, res, next){
  124.   var add = new db.article({
  125.     title       : req.body.title,
  126.     desciption  : req.body.desciption,
  127.     tag         : req.body.tag.split(','),
  128.     price       : req.body.price,
  129.     author      : req.session.users.username
  130.   });
  131.   add.save(function(err){
  132.     if(err) throw err;
  133.     return res.json({message: 'succes insert article ' + req.session.users.username});
  134.   });
  135. });
  136.  
  137. // Get article
  138. router.get('/article', CheckLogin, function(req, res, next){
  139.   db.article.find({}, function(err, data){
  140.     if(err) throw err;
  141.     res.json(data);
  142.   });
  143. });
  144.  
  145. // Get One Article
  146.  
  147. router.get('/article/read/:id', CheckLogin, function(req, res, next){
  148.   db.article.findOne({_id:req.params.id}, function(err, data){
  149.     if(err) throw err;
  150.     res.json(data);
  151.   });
  152. });
  153.  
  154. // Post comment
  155. router.post('/article/read/:id/comment', CheckLogin, function(req, res, next){
  156.   var data = {
  157.     username  : req.session.users.username,
  158.     body      : req.body.body
  159.   };
  160.   db.article.update({_id:req.params.id},{$addToSet:{comment:data}}, function(err){
  161.     if(err) throw err;
  162.     res.json({message: 'succes comment'});
  163.   });
  164. });
  165.  
  166. // Get vote
  167. router.get('/article/vote/:id', CheckLogin, function(req, res, next){
  168.   db.article.findOne({_id: req.params.id},function(err, data){
  169.     if(err) throw err;
  170.     if (data) {
  171.       for (var i = 0; i < data.voter.length; i++) {
  172.         // console.log(data.voter[i]);
  173.         if (data.voter[i] == req.session.users.username) {
  174.           return res.json({message: 'kamu sudah melakukan vote sebelum nya'});
  175.         }
  176.       }
  177.       var vote = data.vote++;
  178.       db.article.findOneAndUpdate({_id: req.params.id}, {vote: vote++, $push:{voter:req.session.users.username}}, function(err, data){
  179.         if(err) throw err;
  180.         return res.json({message: 'succes vote'});
  181.       });
  182.     }
  183.   });
  184. });
  185.  
  186. // Get most vote
  187. router.get('/article/most/vote', CheckLogin, function(req, res, next){
  188.   db.article.find({}).sort({vote: -1}).exec(function(err, data){
  189.     // if(err) throw err;
  190.     return res.json(data);
  191.   });
  192. });
  193.  
  194. // Get last comment
  195. router.get('/article/most/comment', CheckLogin, function(req, res, next){
  196.   db.article.find().sort({comment: -1}).exec(function(err, data){
  197.     if(err) throw err;
  198.     return res.json(data);
  199.   });
  200. });
  201.  
  202. // Get last thread
  203. router.get('/article/last', CheckLogin, function(req, res, next){
  204.   db.article.find().sort({date: -1}).exec(function(err, data){
  205.     if(err) throw err;
  206.     return res.json(data);
  207.   });
  208. });
  209.  
  210. module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement