Guest User

Untitled

a guest
Sep 29th, 2016
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const _ = require('lodash');
  3.  
  4. const nextId = table => {
  5.   let id = 1;
  6.  
  7.   let currentData = JSON.parse(fs.readFileSync('data.json'), 'utf8');
  8.  
  9.   if (!currentData[table]) {
  10.     return id;
  11.   }
  12.  
  13.   return currentData[table][currentData[table].length - 1].id + 1;
  14. };
  15.  
  16. const newPost = (author, title, body) => {
  17.   let post = {
  18.     author,
  19.     title,
  20.     body,
  21.     date: new Date().toLocaleString(),
  22.     votes: 1,
  23.     id: nextId('posts')
  24.   };
  25.  
  26.   let currentData = JSON.parse(fs.readFileSync('data.json', 'utf8'));
  27.  
  28.   if (!currentData.posts) {
  29.     currentData.posts = [];
  30.   }
  31.  
  32.   currentData.posts.push(post);
  33.  
  34.   fs.writeFileSync('data.json', JSON.stringify(currentData));
  35.  
  36.   return post;
  37. };
  38.  
  39. const newComment = (author, post, comment) => {
  40.   let ncomment = {
  41.     author,
  42.     post,
  43.     comment,
  44.     date: new Date().toLocaleString(),
  45.     id: nextId('comments')
  46.   };
  47.  
  48.   let currentData = JSON.parse(fs.readFileSync('data.json', 'utf8'));
  49.  
  50.   if (!currentData.comments) {
  51.     currentData.comments = [];
  52.   }
  53.  
  54.   currentData.comments.push(ncomment);
  55.  
  56.   fs.writeFileSync('data.json', JSON.stringify(currentData));
  57.  
  58.   return ncomment;
  59. };
  60.  
  61. const newUser = (name, email, password) => {
  62.   let user = {
  63.     name,
  64.     email,
  65.     password,
  66.     registration: new Date().toLocaleString(),
  67.     id: nextId('users')
  68.   };
  69.  
  70.   let currentData = JSON.parse(fs.readFileSync('data.json', 'utf8'));
  71.  
  72.   if (!currentData.users) {
  73.     currentData.users = [];
  74.   }
  75.  
  76.   currentData.users.push(user);
  77.  
  78.   fs.writeFileSync('data.json', JSON.stringify(currentData));
  79.  
  80.   return user;
  81. };
  82.  
  83. const checkUser = (name, password) => {
  84.   let currentData = JSON.parse(fs.readFileSync('data.json', 'utf8'));
  85.   let users = currentData.users || [];
  86.  
  87.   return users.find(u => u.name === name && u.password === password);
  88. };
  89.  
  90. const vote = (post, amount) => {
  91.   let currentData = JSON.parse(fs.readFileSync('data.json', 'utf8'));
  92.   let posts = currentData.posts;
  93.  
  94.   let found = posts.findIndex(p => p.id === post);
  95.  
  96.   if (found < 0) return;
  97.  
  98.   currentData.posts[found].votes += amount;
  99.  
  100.   fs.writeFileSync('data.json', JSON.stringify(currentData));
  101. }
  102.  
  103. const upvotePost = post => {
  104.   vote(post, +1);
  105. };
  106.  
  107. const downvotePost = post => {
  108.   vote(post, -1);
  109. };
  110.  
  111. const getPosts = () => {
  112.   return JSON.parse(fs.readFileSync('data.json', 'utf8')).posts || [];
  113. }
  114.  
  115. const getUsers = () => {
  116.   return JSON.parse(fs.readFileSync('data.json', 'utf8')).users || [];
  117. }
  118.  
  119. const getComments = () => {
  120.   return JSON.parse(fs.readFileSync('data.json', 'utf8')).comments || [];
  121. }
  122.  
  123. const express = require('express');
  124. const app = express();
  125. const cors = require('cors');
  126. const bodyParser = require('body-parser');
  127.  
  128. app.use(bodyParser.json());
  129. app.use(bodyParser.urlencoded({ extended: true }));
  130. app.use(cors());
  131. app.set('json spaces', 40);
  132.  
  133. app.get('/posts', function (req, res) {
  134.   res.send(getPosts());
  135. });
  136.  
  137. app.get('/users', function (req, res) {
  138.   res.send(getUsers());
  139. });
  140.  
  141. app.get('/comments', function (req, res) {
  142.   res.send(getComments());
  143. });
  144.  
  145. app.post('/addpost', function (req, res) {
  146.   let post = newPost(req.body.author.id, req.body.title, req.body.body);
  147.  
  148.   res.send(post);
  149. });
  150.  
  151. app.post('/adduser', function (req, res) {
  152.   let user = newUser(req.body.username, req.body.email, req.body.password);
  153.  
  154.   res.send(user);
  155. });
  156.  
  157. app.post('/addcomment', function (req, res) {
  158.   let comment = newComment(req.body.user, req.body.post, req.body.body);
  159.  
  160.   res.send(comment);
  161. });
  162.  
  163. app.post('/upvote', function (req, res) {
  164.   let current = upvotePost(req.body.post);
  165.  
  166.   res.send(current);
  167. });
  168.  
  169. app.post('/downvote', function (req, res) {
  170.   let current = downvotePost(req.body.post);
  171.  
  172.   res.send(current);
  173. });
  174.  
  175. app.post('/login', function (req, res) {
  176.   let user = checkUser(req.body.username, req.body.password);
  177.  
  178.   res.send(user);
  179. });
  180.  
  181. app.listen(3000, () => {
  182.   console.log('server listening at localhost:3000');
  183. });
Add Comment
Please, Sign In to add comment