{"size": 2040, "ext": "js", "max_stars_repo_path": "app/routes/api.js", "max_stars_repo_name": "nigelbritton/darkstar-auctionhouse-app", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/routes/api.js", "max_issues_repo_name": "nigelbritton/darkstar-auctionhouse-app", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/routes/api.js", "max_forks_repo_name": "nigelbritton/darkstar-auctionhouse-app", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.3333333333, "max_line_length": 62, "alphanum_fraction": 0.531372549}
/**
 * Created by Nigel.Britton on 23/07/2018.
 */

'use strict';

var express = require('express'),
    debug = require('debug')('darkstar-auctionhouse-app:api'),
    router = express.Router();

var loadContent = require('../lib/loadContent')();

router.post('/searchCharByName', function(req, res, next) {
    loadContent.searchCharByName( req.body.charname )
        .then(function (response) {
            debug(response);
            if (response.length === 1) {
                loadContent.searchChar( response[0].id )
                    .then(function (response) {
                        res.send(response);
                    }).catch(function (error) {
                        debug(error);
                        res.send([]);
                    });
            } else {
                res.send(response);
            }
        }).catch(function (error) {
            debug(error);
            res.send([]);
        });
});

router.post('/searchChar', function(req, res, next) {
    loadContent.searchChar( req.body.charid )
        .then(function (response) {
            res.send(response);
        }).catch(function (error) {
            debug(error);
            res.send([]);
        });
});

router.post('/searchItemByName', function(req, res, next) {
    loadContent.searchItemByName( req.body.itemname )
        .then(function (response) {
            res.send(response);
        }).catch(function (error) {
            debug(error);
            res.send([]);
        });
});

router.post('/searchItem', function(req, res, next) {
    loadContent.searchItem( req.body.itemid, req.body.stack )
        .then(function (response) {
            res.send(response);
        }).catch(function (error) {
            debug(error);
            res.send({});
        });
});

router.get('/getPopularCharacters', function(req, res, next) {
    res.send(loadContent.getPopularCharacters());
});

router.get('/getPopularItems', function(req, res, next) {
    res.send(loadContent.getPopularItems());
});

module.exports = router;