{"size": 2365, "ext": "js", "max_stars_repo_path": "Golf.js/routes/tournaments.js", "max_stars_repo_name": "judgie79/iGolfTournament", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "Golf.js/routes/tournaments.js", "max_issues_repo_name": "judgie79/iGolfTournament", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "Golf.js/routes/tournaments.js", "max_forks_repo_name": "judgie79/iGolfTournament", "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": 25.989010989, "max_line_length": 93, "alphanum_fraction": 0.5843551797}
﻿var express = require("express");
var router = express.Router();

var tournamentRepository = require('../db/tournamentRepository.js');

var error = new require('./error')();
/*  "/"
 *    GET: finds all tournaments
 *    POST: creates a new tournament
 */
router.get("/", function (req, res) {

    tournamentRepository.findAll(function (err, tournaments) {

        if (err) {
            error.handleError(res, err.message, "Failed to get tournaments.");
        } else {
            res.status(200).json(tournaments);
        }
    });
    
});

router.post("/", function (req, res) {

    var newtournament = req.body;
    
    tournamentRepository.create(newtournament, function (err, tournament) {
        if (err) {
            error.handleError(res, err.message, "Failed to create new tournament.");
        } else {
            res.status(201).json(tournament.ops[0]);
        }
    });
});

/*  "/:id"
 *    GET: find tournament by id
 *    PUT: update tournament by id
 *    DELETE: deletes tournament by id
 */

router.get("/:id", function (req, res) {

    tournamentRepository.findById(req.params.id, function (err, tournament) {

        if (err) {
            error.handleError(res, err.message, "Failed to get tournament");
        } else {
            res.status(200).json(tournament);
        }
    });
});

router.put("/:id", function (req, res) {

    var updateTournament = req.body;

    tournamentRepository.update(req.params.id, updateTournament, function (err, tournament) {
        if (err) {
            error.handleError(res, err.message, "Failed to update tournament");
        } else {
            res.status(204).json(tournament);
        }
    });
});

router.put("/:id/start", function (req, res) {

    var updateTournament = req.body;

    tournamentRepository.start(req.params.id, updateTournament, function (err, tournament) {
        if (err) {
            error.handleError(res, err.message, "Failed to update tournament");
        } else {
            res.status(204).json(tournament);
        }
    });
});

router.delete("/:id", function (req, res) {
    tournamentRepository.delete(req.params.id, function (err, result) {
        if (err) {
            error.handleError(res, err.message, "Failed to delete tournament");
        } else {
            res.status(204).end();
        }
    });
});

module.exports = router;