<commit_before><?php
/**
 * Simple API for short URLs.
 *
 * @author chris
 * @created 16/04/15 13:05
 */

namespace Shorty\Controller;

use PDO;
use Silex\Application;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class ApiController
{
    /**
     * The silex app.
     *
     * @var Application
     */
    private $app;

    /**
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    /**
     * Gets a list of URLs.
     *
     * @return JsonResponse
     */
    public function getLinksAction()
    {
        /** @var \Doctrine\DBAL\Connection $db */
        $db = $this->app['db'];

        $links = $db->query(
            <<<EOT
select
    u.id, u.url, u.created, count(v.shorty_url_id) as clicks
from shorty_url u
left join shorty_url_visit v on v.shorty_url_id = u.id
order by u.id desc limit 10
EOT
        )->fetchAll(PDO::FETCH_ASSOC);

        foreach ($links as $key => $url) {
            $links[$key] = $this->formatResult($links[$key]);
        }

        return new JsonResponse($links);
    }

    /**
     * Gets the details for a single short URL.
     *
     * @param string $id
     *
     * @return JsonResponse
     */
    public function getLinkAction($id)
    {
        /** @var \Doctrine\DBAL\Connection $db */
        $db = $this->app['db'];

        $statement = $db->prepare(
            <<<EOT
select u.id, u.url, u.created, count(v.shorty_url_id) as clicks
from shorty_url u<commit_msg>Added missing group by clause :(.<commit_after><?php
/**
 * Simple API for short URLs.
 *
 * @author chris
 * @created 16/04/15 13:05
 */

namespace Shorty\Controller;

use PDO;
use Silex\Application;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class ApiController
{
    /**
     * The silex app.
     *
     * @var Application
     */
    private $app;

    /**
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    /**
     * Gets a list of URLs.
     *
     * @return JsonResponse
     */
    public function getLinksAction()
    {
        /** @var \Doctrine\DBAL\Connection $db */
        $db = $this->app['db'];

        $links = $db->query(
            <<<EOT
select
    u.id, u.url, u.created, count(v.shorty_url_id) as clicks
from shorty_url u
left join shorty_url_visit v on v.shorty_url_id = u.id
group by u.id
order by u.id desc limit 10
EOT
        )->fetchAll(PDO::FETCH_ASSOC);

        foreach ($links as $key => $url) {
            $links[$key] = $this->formatResult($links[$key]);
        }

        return new JsonResponse($links);
    }

    /**
     * Gets the details for a single short URL.
     *
     * @param string $id
     *
     * @return JsonResponse
     */
    public function getLinkAction($id)
    {
        /** @var \Doctrine\DBAL\Connection $db */
        $db = $this->app['db'];

        $statement = $db->prepare(
            <<<EOT
select u.id, u.url, u.created, count(v.shorty_url_id) as clicks
from shorty_url u
{'fraction_non_alphanumeric': 0.13636363636363635, 'fraction_numerical': 0.007575757575757576, 'mean_word_length': 2.140733399405352, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '6599598', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1039, 'n_words': 259}