Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express'),
- router = express.Router(),
- md5 = require('md5'),
- mongoose = require('mongoose'),
- db = require('../model/db');
- function CheckLogin(req, res, next){
- if(req.session.users){
- next();
- }else {
- return res.status(401).send();
- }
- }
- router.get('/', function(req, res, next){
- return res.json({
- register: {
- method: 'POST',
- url : 'http://localhost:3000/api/register'
- },
- login: {
- method: 'POST',
- url : 'http://localhost:3000/api/login'
- },
- logout: {
- method: 'GET',
- url : 'http://localhost:3000/api/logout'
- },
- article_add: {
- method: 'POST',
- url : 'http://localhost:3000/api/article'
- },
- article_list: {
- method: 'GET',
- url : 'http://localhost:3000/api/article'
- },
- article_most_vote: {
- method: 'GET',
- url : 'http://localhost:3000/api/article/most/vote'
- },
- article_last: {
- method: 'GET',
- url : 'http://localhost:3000/api/article/most/comment'
- },
- article_last_comment: {
- method: 'GET',
- url : 'http://localhost:3000/api/article/last'
- },
- article_read: {
- method: 'GET',
- url : 'http://localhost:3000/api/read/:id'
- },
- article_read_comment: {
- method: 'GET',
- url : 'http://localhost:3000/api/article/read/:id/comment'
- },
- article_vote: {
- method: 'GET',
- url : 'http://localhost:3000/api/article/vote/:id'
- }
- });
- });
- // Post Register
- router.post('/register', function(req, res, next){
- if (req.session.username) {
- return res.json({message: 'is Logged'});
- }
- db.users.findOne({$or:[{username: req.body.username},{email:req.body.email}]},function(err,data){
- if (data) {
- if (data.username == req.body.username) {
- return res.json({message: 'This username is available'});
- }
- if (data.email == req.body.email) {
- return res.json({message: 'This email is available'});
- }
- }else {
- var add = new db.users({
- username : req.body.username,
- password : md5(req.body.password),
- email : req.body.email,
- phone : '62'+req.body.phone,
- birthday : req.body.birthday
- });
- add.save(function(err){
- if(err) throw err;
- db.users.findOne({username: req.body.username}, function(err, data){
- req.session.users = data;
- });
- return res.json({message: 'Succes Register ' + req.session.users.username});
- });
- }
- });
- });
- // Post login
- router.post('/login',function(req, res, next){
- if (req.session.users) {
- return res.json({message: 'is Logged'});
- }
- db.users.findOne({username: req.body.username}, function(err, data){
- if (data) {
- if (data.password !== md5(req.body.password)) {
- return json({message: 'password not match'});
- }
- req.session.users = data;
- return res.json({message: 'succes login '+ req.session.users.username});
- }
- return res.json({message: 'This username is available'});
- });
- });
- // get Logout
- router.get('/logout', function(req, res, next){
- if (!req.session.users) {
- return res.status(401).send();
- }
- req.session.destroy();
- return res.json({message: 'succes logout'});
- });
- // post Article
- router.post('/article', CheckLogin, function(req, res, next){
- var add = new db.article({
- title : req.body.title,
- desciption : req.body.desciption,
- tag : req.body.tag.split(','),
- price : req.body.price,
- author : req.session.users.username
- });
- add.save(function(err){
- if(err) throw err;
- return res.json({message: 'succes insert article ' + req.session.users.username});
- });
- });
- // Get article
- router.get('/article', CheckLogin, function(req, res, next){
- db.article.find({}, function(err, data){
- if(err) throw err;
- res.json(data);
- });
- });
- // Get One Article
- router.get('/article/read/:id', CheckLogin, function(req, res, next){
- db.article.findOne({_id:req.params.id}, function(err, data){
- if(err) throw err;
- res.json(data);
- });
- });
- // Post comment
- router.post('/article/read/:id/comment', CheckLogin, function(req, res, next){
- var data = {
- username : req.session.users.username,
- body : req.body.body
- };
- db.article.update({_id:req.params.id},{$addToSet:{comment:data}}, function(err){
- if(err) throw err;
- res.json({message: 'succes comment'});
- });
- });
- // Get vote
- router.get('/article/vote/:id', CheckLogin, function(req, res, next){
- db.article.findOne({_id: req.params.id},function(err, data){
- if(err) throw err;
- if (data) {
- for (var i = 0; i < data.voter.length; i++) {
- // console.log(data.voter[i]);
- if (data.voter[i] == req.session.users.username) {
- return res.json({message: 'kamu sudah melakukan vote sebelum nya'});
- }
- }
- var vote = data.vote++;
- db.article.findOneAndUpdate({_id: req.params.id}, {vote: vote++, $push:{voter:req.session.users.username}}, function(err, data){
- if(err) throw err;
- return res.json({message: 'succes vote'});
- });
- }
- });
- });
- // Get most vote
- router.get('/article/most/vote', CheckLogin, function(req, res, next){
- db.article.find({}).sort({vote: -1}).exec(function(err, data){
- // if(err) throw err;
- return res.json(data);
- });
- });
- // Get last comment
- router.get('/article/most/comment', CheckLogin, function(req, res, next){
- db.article.find().sort({comment: -1}).exec(function(err, data){
- if(err) throw err;
- return res.json(data);
- });
- });
- // Get last thread
- router.get('/article/last', CheckLogin, function(req, res, next){
- db.article.find().sort({date: -1}).exec(function(err, data){
- if(err) throw err;
- return res.json(data);
- });
- });
- module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement