"use strict";

var express = require("express");
var bodyParser = require("body-parser");
var _ = require("lodash");
var Promise = require("bluebird");
var log4js = require("log4js");
var sockjs = require("sockjs")
var log = log4js.getLogger("backend");




function createGroup( req, res ) {
    res.status(201).send();
};

function getUnitsForGroup( req, res ) {
    var groupId = req.params.groupId;
    log.info("Getting units for group %s...", groupId);
    res.status(200).send();
};

function getMarkersForGroup( req, res ) {
    var groupId = req.params.groupId;
    log.info("Getting units for group %s...", groupId);
    res.status(200).send();
};

function addUnitToGroup( req, res ) {
    var groupId = req.params.groupId;
    log.info("Getting units for group %s...", groupId);
    res.status(201).send();
};

function addMarkerToGroup( req, res ) {
    var groupId = req.params.groupId;
    log.info("Getting units for group %s...", groupId);
    res.status(201).send();
};

function updateUnit( res, req ) {
    var groupId = req.params.groupId;
    var unitId = req.params.unitId;
    log.info("Updating unit %s for group %s...", unitId, groupId);
    res.status(201).send();
};

function startWeb() {
    var port = 3000;
    log.info("Starting web server on port %s...", port);
    var server = express();

    server.use( bodyParser.json() );
    server.get( "/api/1.0.0/groups/:groupId/units", getUnitsForGroup );
    server.get( "/api/1.0.0/groups/:groupId/markers", getMarkersForGroup );
    server.post( "/api/1.0.0/groups", createGroup );
    server.post( "/api/1.0.0/groups/:groupId/markers", addMarkerToGroup );
    server.post( "/api/1.0.0/groups/:groupId/units", addUnitToGroup );
    server.put( "/api/1.0.0/groups/:groupId/units/:unitId", updateUnit );
    server.use( "/", express.static(__dirname + "/web"));
    
    var webserver = server.listen(port);
    log.info("done.");
};

function init() {
    startWeb();
    process.on("SIGINT", shutdown);
    process.on("quit", shutdown);
};

function shutdown() {
    log.warn("Shutting down...");
    process.exit(0);
};

init();

"use strict";

var express = require("express");
var bodyParser = require("body-parser");
var crypto = require("crypto");
var _ = require("lodash");
var Promise = require("bluebird");
var log4js = require("log4js");
var sockjs = require("sockjs")
var log = log4js.getLogger("backend");


var groups = [];


function createGroup( req, res ) {    
    var groupCreateRequest = req.body;

    log.info("Creating group...");
    var newGroup = {
        id: crypto.randomBytes(20).toString('hex'),
        key: crypto.randomBytes(20).toString('hex')
    };
    var newGroupJSON = JSON.stringify(newGroup,null,4);
    log.info("Created group\n%s", newGroupJSON);;
    groups.push(newGroup);

    res.set("Content-type", "application/json");
    res.status(201).send(newGroupJSON);
};

function getGroups( req, res ) {    

    log.info("Getting groups...");
    var groupsJSON = JSON.stringify({groups:groups},null,4);

    res.set("Content-type", "application/json");
    res.status(201).send(groupsJSON);
};

function getUnitsForGroup( req, res ) {
    var groupId = req.params.groupId;
    log.info("Getting units for group %s...", groupId);
    res.status(200).send();
};

function getMarkersForGroup( req, res ) {
    var groupId = req.params.groupId;
    log.info("Getting units for group %s...", groupId);
    res.status(200).send();
};

function addUnitToGroup( req, res ) {
    var groupId = req.params.groupId;
    log.info("Getting units for group %s...", groupId);
    res.status(201).send();
};

function addMarkerToGroup( req, res ) {
    var groupId = req.params.groupId;
    log.info("Getting units for group %s...", groupId);
    res.status(201).send();
};

function updateUnit( res, req ) {
    var groupId = req.params.groupId;
    var unitId = req.params.unitId;
    log.info("Updating unit %s for group %s...", unitId, groupId);
    res.status(201).send();
};

function startWeb() {
    var port = 3000;
    log.info("Starting web server on port %s...", port);
    var server = express();

    server.use( bodyParser.json() );
    server.get( "/api/1.0.0/groups/:groupId/units", getUnitsForGroup );
    server.get( "/api/1.0.0/groups/:groupId/markers", getMarkersForGroup );
    server.get( "/api/1.0.0/groups", getGroups );
    server.post( "/api/1.0.0/groups", createGroup );
    server.post( "/api/1.0.0/groups/:groupId/markers", addMarkerToGroup );
    server.post( "/api/1.0.0/groups/:groupId/units", addUnitToGroup );
    server.put( "/api/1.0.0/groups/:groupId/units/:unitId", updateUnit );
    server.use( "/", express.static(__dirname + "/web"));
    
    var webserver = server.listen(port);
    log.info("done.");
};

function init() {
    startWeb();
    process.on("SIGINT", shutdown);
    process.on("quit", shutdown);
};

function shutdown() {
    log.warn("Shutting down...");
    process.exit(0);
};

init();

Add routes for creating and view groups.