const Good = require('good');
const Joi = require('joi');

const anecdotes = require('../data/anecdotes.json');

function getAnecdote(id) {
  return anecdotes.find(a => a.id === id);
}

module.exports = function(server) {

    server.route({
        method: 'GET',
        path: '/anecdotes',
        handler: (request, reply) => {
            reply(anecdotes);
        }
    });

    server.route({
        method: 'GET',
        path: '/anecdotes/{anecdoteid}',
        handler : (request, reply) => {
          const id = encodeURIComponent(request.params.anecdoteid);
          const anecdote = getAnecdote(id);
          if (anecdote===undefined) {
            reply("This anecdote was not found").code(404);
          } else {
            reply(anecdote);
          }
        }
    })


};

@@ -104,16 +104,61 @@
 .json');
+%0Aconst users = require('../data/users.json');
 %0A%0Afuncti
@@ -178,16 +178,18 @@
 e(id) %7B%0A
+  
   return
@@ -226,16 +226,162 @@
 id);%0A%7D%0A%0A
+function getUser(id) %7B%0A    return users.find(u =%3E u.id == id);%0A%7D%0A%0Afunction checkAllActors(actors) %7B%0A    return !users.find(id =%3E !!users%5Bid%5D);%0A%7D%0A%0A
 module.e
@@ -665,17 +665,16 @@
  handler
- 
 : (reque
@@ -681,32 +681,34 @@
 st, reply) =%3E %7B%0A
+  
           const 
@@ -755,24 +755,26 @@
 necdoteid);%0A
+  
           co
@@ -815,16 +815,18 @@
         
+  
 if (anec
@@ -829,19 +829,21 @@
 anecdote
+ 
 ===
+ 
 undefine
@@ -855,24 +855,28 @@
             
+ 
+   
 reply(%22This 
@@ -921,16 +921,18 @@
         
+  
 %7D else %7B
@@ -932,16 +932,20 @@
  else %7B%0A
+    
         
@@ -965,16 +965,1171 @@
 cdote);%0A
+            %7D%0A        %7D%0A    %7D)%0A%0A    server.route(%7B%0A        method: 'POST',%0A        path: '/anecdotes',%0A        config: %7B%0A            validate: %7B%0A                payload: %7B%0A                    title: Joi.string().required().min(1),%0A                    message: Joi.string().required().min(1),%0A                    author: Joi.number().required(),%0A                    tags: Joi.array().items(Joi.string()),%0A                    happenedOn: Joi.number().required(),%0A                    actors: Joi.array().items(Joi.number())%0A                %7D%0A            %7D%0A        %7D,%0A        handler: (request, reply) =%3E %7B%0A            const anecdote = request.payload;%0A            const author = getUser(parseInt(anecdote.author));%0A            console.log(author);%0A            if (author === undefined) %7B%0A                reply(%22The author don't exists%22).code(404);%0A            %7D else if (!checkAllActors(anecdote.actors)) %7B%0A                reply(%22At least one of actors don't exists%22).code(404);%0A            %7D else %7B%0A                anecdote.id = anecdotes.length + 1;%0A                anecdotes.push(anecdote);%0A                reply(%60/anecdotes/$%7Banecdote.id%7D%60).code(201);%0A  
         

Add POST for anecdotes