<commit_before>#    galegis-api -- Structured Importing and RESTful providing of GA General Assembly Activity.
#    Copyright (C) 2013 <NAME>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
ObjectId = require('mongodb').ObjectID;

module.exports = (app, db) ->
  # GET /api/v1/members - Retrieve all members for a particular session.
  # (Defaults to current session.)
  app.get '/api/v1/members', (req, res) ->
    db.collection("sessions").findOne {current: true}, (err, currentSession) ->
      selectedSessionIdStr = req.query.sessionId || currentSession._id.toString()
      selectedSessionId = new ObjectId(selectedSessionIdStr)

      db.collection("members").find({sessions: selectedSessionId}).toArray (err, results) ->
        if err
          errorId = Math.random().toString(36).substring(7)
          console.error("Error " + errorId + ": " + err)

          res.json
            id: errorId,
            error: err
          , 500

          return

        res.json(results)

  # GET /api/v1/member/:id - Retrieve all information on a particular member.
  app.get '/api/v1/member/:id', (req, res) ->
    try
      memberObjectId = new ObjectId req.params.id
    catch
      errorOutput =
        fieldId: "memberId",
        error: "invalid",
        message: "The member id you requested was not a valid identifier. Identifiers should conform to MongoDB's ObjectID format."

      res.json errorOutput, 417
      return

    db.collection("members").findOne {_id: memberObjectId}, (err, member) ->
      if err
        errorId = Math.random().toString(36).substring(7)
        console.error("Error " + errorId + ": " + err)

        res.json
          id: errorId,
          error: err
        , 500

        return

      if member?
        res.json member
      else
        res.send 404

  # GET /api/v1/member/:id/votes - Retrieve all votes for a member.
  app.get '/api/v1/member/:id/votes', (req, res) ->
    res.send(501)
<commit_msg>Handle the case we can't find the current session for members<commit_after>#    galegis-api -- Structured Importing and RESTful providing of GA General Assembly Activity.
#    Copyright (C) 2013 <NAME>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
ObjectId = require('mongodb').ObjectID;

module.exports = (app, db) ->
  # GET /api/v1/members - Retrieve all members for a particular session.
  # (Defaults to current session.)
  app.get '/api/v1/members', (req, res) ->
    db.collection("sessions").findOne {current: true}, (err, currentSession) ->
      unless currentSession?
        err = "Could not find current session."
        errorId = Math.random().toString(36).substring(7)
        console.error("Error " + errorId + ": " + err)

        res.json
          id: errorId,
          error: err
        , 500

        return

      selectedSessionIdStr = req.query.sessionId || currentSession._id.toString()
      selectedSessionId = new ObjectId(selectedSessionIdStr)

      db.collection("members").find({sessions: selectedSessionId}).toArray (err, results) ->
        if err
          errorId = Math.random().toString(36).substring(7)
          console.error("Error " + errorId + ": " + err)

          res.json
            id: errorId,
            error: err
          , 500

          return

        res.json(results)

  # GET /api/v1/member/:id - Retrieve all information on a particular member.
  app.get '/api/v1/member/:id', (req, res) ->
    try
      memberObjectId = new ObjectId req.params.id
    catch
      errorOutput =
        fieldId: "memberId",
        error: "invalid",
        message: "The member id you requested was not a valid identifier. Identifiers should conform to MongoDB's ObjectID format."

      res.json errorOutput, 417
      return

    db.collection("members").findOne {_id: memberObjectId}, (err, member) ->
      if err
        errorId = Math.random().toString(36).substring(7)
        console.error("Error " + errorId + ": " + err)

        res.json
          id: errorId,
          error: err
        , 500

        return

      if member?
        res.json member
      else
        res.send 404

  # GET /api/v1/member/:id/votes - Retrieve all votes for a member.
  app.get '/api/v1/member/:id/votes', (req, res) ->
    res.send(501)

{'fraction_non_alphanumeric': 0.10472541507024266, 'fraction_numerical': 0.01277139208173691, 'mean_word_length': 2.4762206721623334, 'pattern_counts': {'":': 5, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2461710', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1532, 'n_words': 534}