<commit_before>//
//  personGet.swift
//  staffDirectory
//
//  Created by <NAME> on 2017-02-20.
//  Copyright (C) 2017 PerfectlySoft, Inc.
//
//  Modified by Clingon: https://github.com/iamjono/clingon
//
import PerfectHTTP

extension Handlers {
    /// API endpoint for handling the fetch of a record, or a list
    static func personGet(data: [String:Any]) throws -> RequestHandler {
        return {
        request, response in

			let people = Person()

			if let id = request.urlVariables["id"] {
				people.id = Int(id) ?? 0
			}
			if people.id > 0 {
				do {
					try people.get()
					let _ = try? response.setBody(json: people.asDataDict())
				} catch {
					print(error)
				}
			} else {
				do {
					try people.findAll()
				} catch {
					print(error)
				}

				var data = [String:Any]()
				for obj in people.rows() {
					data["\(obj.id)"] = obj.asDataDict()
				}
				let _ = try? response.setBody(json: ["data": data])
			}
            response.completed()
        }
    }
}<commit_msg>comment code, plus a few small refinements<commit_after>//
//  personGet.swift
//  staffDirectory
//
//  Created by <NAME> on 2017-02-20.
//  Copyright (C) 2017 PerfectlySoft, Inc.
//
//  Modified by Clingon: https://github.com/iamjono/clingon
//
import PerfectHTTP

extension Handlers {
    /// API endpoint for handling the fetch of a record, or a list
    static func personGet(data: [String:Any]) throws -> RequestHandler {
        return {
        request, response in

			// Initial stub object.
			// Holds either the individual response, or the list response
			let people = Person()

			// Look for {id} url var in request
			if let id = request.urlVariables["id"] {
				do {
					// Set id via "get" method (aka "Find")
					try people.get(Int(id) ?? 0)

					// If id is populated, then we have a successful fetch
					if people.id > 0 {
						let _ = try? response.setBody(json: people.asDataDict())
					} else {
						// Otherwise, exit out of the request
						throw(ErrorCode.invalidRequest)
					}
				} catch {
					// Return an error specifying ID fetch failed
					Handlers.error(request, response, error: "Incorrect ID", code: .badRequest)
					return
				}
			} else {
				// Execute a list fetch
				do {
					// Basic findall method
					try people.findAll()
				} catch {
					// Return an informative error
					Handlers.error(request, response, error: "\(error)", code: .badRequest)
					return
				}

				// Container for our processed results
				var data = [String:Any]()
				for obj in people.rows() {
					data["\(obj.id)"] = obj.asDataDict()
				}
				let _ = try? response.setBody(json: ["data": data])
			}
            response.completed()
        }
    }
}
{'fraction_non_alphanumeric': 0.12847093669011478, 'fraction_numerical': 0.01036653091447612, 'mean_word_length': 2.216666666666667, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '4034746', 'n_tokens_mistral': 1070, 'n_tokens_neox': 830, 'n_words': 256}