'use strict';

var Quotes = require('./models');

var path = require('path');
var express = require('express');
var app = express();
var morgan = require('morgan');
const fs = require('fs');
const repository = require('./models/Repository');

app.use(express.static(path.resolve(__dirname)));
app.use(morgan('combined'));

var PORT = 3000;
var DATA_PATH = path.resolve(__dirname + '/../data', 'quotes.json');
var PAGE_SIZE = 5;

function addTag(req, res) {
  const tag = req.body.tag;
  const number = req.params.number;

  const quote = req.repository.quotes().get(number);
  quote.tag(number, tag);
  res.status(200);
}

function addAllTags(req, res) {
  res.status(200).send(req.repository.tags().all());
}

function addQuote(path, callback) {
  fs.readFile(path, 'utf-8', (error, res) => {
    if (error) callback(error);

    const quotes = new Quotes();
    res = JSON.parse(res);
    for (const quote of res) {
      quotes.add(quote);
    }
    callback(null, quotes);
  });
}

function getQuotes(req, res, query) {
  addQuote(DATA_PATH, function (error, quotes) {
    if (error) throw error;
    var page = req.query.page || 0;
    var skip = page * PAGE_SIZE;
    res.json(quotes.all().splice(skip, PAGE_SIZE));
  });
}

app.use(function (req, res, next) {
  req.repository = repository;
  next();
});

// Quote specific routes
app.route('/api/v1/quotes').get(function (req, res) {
  getQuotes(req, res);
});

app.route('/api/v1/quotes/tags').get(function (req, res) {
  getAllTags(req, res);
});

app.route('/api/v1/quotes/:id/tags').post(function (req, res) {
  addTag(req, res);
});

app.route('/api/v1/quotes/:id/tags').get(function (req, res) {
  getTags(req, res);
});

app.get('*', function (req, res) {
  res.sendFile(path.resolve(__dirname + '/../templates', 'index.html'));
});

app.listen(PORT, function () {
  console.log('App listening on port ' + PORT);
});

@@ -12,129 +12,75 @@
 ';%0A%0A
-var Quotes = require('./models');%0A%0Avar path = require('path');%0Avar express = require('express');%0Avar app = express();%0Avar
+const express = require('express');%0Aconst fs = require('fs');%0Aconst
  mor
@@ -102,34 +102,36 @@
 morgan');%0Aconst 
-fs
+path
  = require('fs')
@@ -122,30 +122,33 @@
  = require('
-fs
+path
 ');%0A
+%0A
 const reposi
@@ -185,16 +185,106 @@
 itory');
+%0Aconst app = express();%0Aconst router = express.Router();%0Avar Quotes = require('./models');
 %0A%0Aapp.us
@@ -1376,48 +1376,30 @@
 tes%0A
-app.
+%0A
 route
-('/api/v1/quotes').get(function
+r.get('/quotes',
  (re
@@ -1397,32 +1397,35 @@
 tes', (req, res)
+ =%3E
  %7B%0A  getQuotes(r
@@ -1439,34 +1439,28 @@
 );%0A%7D);%0A%0A
-app.
 route
-('/api/v1
+r.get('
 /quotes/
@@ -1460,38 +1460,25 @@
 quotes/tags'
-).get(function
+,
  (req, res) 
@@ -1468,32 +1468,35 @@
 ags', (req, res)
+ =%3E
  %7B%0A  getAllTags(
@@ -1511,34 +1511,28 @@
 );%0A%7D);%0A%0A
-app.
 route
-('/api/v1
+r.get('
 /quotes/
@@ -1544,23 +1544,9 @@
 ags'
-).post(function
+,
  (re
@@ -1556,19 +1556,23 @@
 res)
+ =%3E
  %7B%0A  
-add
+get
 Tag
+s
 (req
@@ -1588,26 +1588,21 @@
 );%0A%0A
-app.
 route
-('/api/v1
+r.post('
 /quo
@@ -1618,22 +1618,9 @@
 ags'
-).get(function
+,
  (re
@@ -1626,28 +1626,30 @@
 eq, res)
+ =%3E
  %7B%0A  
-get
+add
 Tag
-s
 (req, re
@@ -1653,24 +1653,53 @@
  res);%0A%7D);%0A%0A
+app.use('/api/v1', router);%0A%0A
 app.get('*',

Use routing path prefix.