<commit_before><?php

/**
 * Main budget functions and routing
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

/**
 * This is how we will call classes
 * autoload will make sure everything is available
 * @todo Move this.
 */

use \Charcoal\App\App;
use \Charcoal\App\AppConfig;
use \Charcoal\App\AppContainer;
use \Utils\RandomStringGenerator;

require_once '../vendor/autoload.php';
require_once '../config/helper.php';
require_once '../config/mustache-view.php';

/**
 * Charcoal configuration
 */
$helper = new CharcoalHelper();

$config = new AppConfig();
$config->addFile(__DIR__.'/../config/config.php');
$config->set('ROOT', dirname(__DIR__) . '/');

// Create container and configure it (with charcoal-config)
$container = new AppContainer([
    'settings' => [
        'displayErrorDetails' => true
    ],
    'config' => $config
]);

$charcoalapp = App::instance($container);

// $container->register(new \Charcoal\Model\ServiceProvider\ModelServiceProvider);
// var_dump($container['model/factory']);

if (!session_id()) {
    session_start();
}

$container = new \Slim\Container([
    'settings' => [
        'displayErrorDetails' => true,
    ],
    'view' => function ($c) {
        $view = new \Slim\Views\PhpRenderer('assets/templates');
        return $view;
    }
]);

$app = new \Slim\App($container);

/**
 * Simple, catchall routing for prototyping.
 */
$app->get('/[{foo}]', function ($request, $response, $args) use ($helper) {
    return $this->view->render($response, '/index.html', $args);
});

/*
=============================

URL                          HTTP Method      Operation
/api/v1/transations          GET              Returns an array of transactions
/api/v1/transations/:id      GET              Returns the transaction with id of :id
/api/v1/transations          POST             Adds a new transaction and returns it with an id attribute added
/api/v1/transations/:id      PUT              Updates the transaction with id of :id

=============================
*/

/**
 * Main API group
 * @param $app  Application
 * @param $db   Database connection
 */
$app->group('/api', function () use ($helper) {

    /**
     * API group v1
     * @param $app  Application
     * @param $db   Database connection
     */
    $this->group('/v1', function () use ($helper) {

        /**
        * Fetch all transactions
        * @param $db   Database connection
        * @todo Add authentification
        */
        $this->get('/transactions', function ($request, $response, $args) use ($helper) {
            $status = 200;
            $count = (isset($args['count']) && is_integer($args['count'])) ? $args['count'] : false;

            try {
                $transactions = $helper
                    ->fetch_collection('budget/object/transaction')
                    ->addFilter('active', true)
                    ->addOrder('creation_date', 'ASC');

                if ($count) {
                    $transactions = $transactions->setNumPerPage($count);
                }

                $transactions = $transactions
                    ->load()
                    ->objects();

                $data = [
                    'message' => 'List of transactions',
                    'results' => $transactions,
                    'status' => 'ok'
                ];
            } catch (Exception $e) {
                $status = 404;
                $data = [
                    'message' => 'An error occured : ' . $e->getMessage(),
                    'results' => [],
                    'status' => 'error'
                ];
            }

            return $response->withStatus($status)
                    ->withHeader('Content-Type', 'application/json')
                    ->write(json_encode($data));
        });

        /**
         * Fetch a single transaction
         * @param $db   Database connection
         * @todo Add authentification
         */
        $this->get('/transactions/{id}', function ($request, $response, $args) use ($helper) {<commit_msg>Added a few basic filtering and sorting capabilities to GET transactions<commit_after><?php

/**
 * Main budget functions and routing
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

/**
 * This is how we will call classes
 * autoload will make sure everything is available
 * @todo Move this.
 */

use \Charcoal\App\App;
use \Charcoal\App\AppConfig;
use \Charcoal\App\AppContainer;
use \Utils\RandomStringGenerator;

require_once '../vendor/autoload.php';
require_once '../config/helper.php';
require_once '../config/mustache-view.php';

/**
 * Charcoal configuration
 */
$helper = new CharcoalHelper();

$config = new AppConfig();
$config->addFile(__DIR__.'/../config/config.php');
$config->set('ROOT', dirname(__DIR__) . '/');

// Create container and configure it (with charcoal-config)
$container = new AppContainer([
    'settings' => [
        'displayErrorDetails' => true
    ],
    'config' => $config
]);

$charcoalapp = App::instance($container);

// $container->register(new \Charcoal\Model\ServiceProvider\ModelServiceProvider);
// var_dump($container['model/factory']);

if (!session_id()) {
    session_start();
}

$container = new \Slim\Container([
    'settings' => [
        'displayErrorDetails' => true,
    ],
    'view' => function ($c) {
        $view = new \Slim\Views\PhpRenderer('assets/templates');
        return $view;
    }
]);

$app = new \Slim\App($container);

/**
 * Simple, catchall routing for prototyping.
 */
$app->get('/[{foo}]', function ($request, $response, $args) use ($helper) {
    return $this->view->render($response, '/index.html', $args);
});

/*
=============================

URL                          HTTP Method      Operation
/api/v1/transations          GET              Returns an array of transactions
/api/v1/transations/:id      GET              Returns the transaction with id of :id
/api/v1/transations          POST             Adds a new transaction and returns it with an id attribute added
/api/v1/transations/:id      PUT              Updates the transaction with id of :id

=============================
*/

/**
 * Main API group
 * @param $app  Application
 * @param $db   Database connection
 */
$app->group('/api', function () use ($helper) {

    /**
     * API group v1
     * @param $app  Application
     * @param $db   Database connection
     */
    $this->group('/v1', function () use ($helper) {

        /**
        * Fetch all transactions
        * @param $db   Database connection
        * @todo Add authentification
        */
        $this->get('/transactions', function ($request, $response, $args) use ($helper) {
            $status = 200;

            // Params are used for filtering and sorting transaction list
            $params = $request->getQueryParams();
            // Default : 20
            $count = (isset($params['count']) && is_numeric($params['count'])) ? $params['count'] : 20;
            // Default : 1
            $page = (isset($params['page']) && is_numeric($params['page'])) ? $params['page'] : 1;
            // Default : sorted from newest to oldest
            $order = (isset($params['order'])) ? $params['order'] : 'DESC';

            try {
                $transactions = $helper
                    ->fetch_collection('budget/object/transaction')
                    ->addFilter('active', true)
                    ->addOrder('creation_date', $order)
                    ->setPage(1)
                    ->setNumPerPage($count)
                    ->load()
                    ->objects();

                $data = [
                    'message' => 'List of transactions',
                    'results' => $transactions,
                    'status' => 'ok'
                ];
            } catch (Exception $e) {
                $status = 404;
                $data = [
                    'message' => 'An error occured : ' . $e->getMessage(),
                    'results' => [],
                    'status' => 'error'
                ];
            }

            return $response->withStatus($status)
                    ->withHeader('Content-Type', 'application/json')
                    ->write(json_encode($data));
        });

        /**
         * Fetch a single transaction
         * @param $db   Database connection
         * @todo Add authentification
         */
        $this->get('/transactions/{id}', function ($request, $response, $args) use ($helper) {
{'fraction_non_alphanumeric': 0.16485335856196784, 'fraction_numerical': 0.0036660359508041626, 'mean_word_length': 1.9872836453549982, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 81, 'https://': 0, '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': '1917343', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2277, 'n_words': 570}