<commit_before><?php

namespace Coyote\Repositories\Eloquent;

use Coyote\Repositories\Contracts\PostRepositoryInterface;
use Coyote\Post\Subscriber;
use Coyote\Post\Attachment;
use DB;

class PostRepository extends Repository implements PostRepositoryInterface
{
    /**
     * @return \Coyote\Topic
     */
    public function model()
    {
        return 'Coyote\Post';
    }

    /**
     * @param int $userId
     * @return mixed
     */
    private function prepare($userId)
    {
        $sql = $this->model
                    ->selectRaw(
                        'DISTINCT ON(posts.id)
                        posts.*,
                        author.name AS author_name,
                        author.photo,
                        author.is_active,
                        author.is_blocked,
                        author.sig,
                        author.location,
                        author.posts AS author_posts,
                        author.allow_sig,
                        author.allow_smilies,
                        author.allow_count,
                        author.created_at AS author_created_at,
                        author.visited_at AS author_visited_at,
                        editor.name AS editor_name,
                        editor.name AS editor_is_active,
                        editor.name AS editor_is_blocked,
                        groups.name AS group_name,
                        sessions.updated_at AS session_updated_at,
                        pa.user_id AS accept_on'
                    )
                    ->leftJoin('sessions', 'sessions.user_id', '=', 'posts.user_id')
                    ->leftJoin('users AS author', 'author.id', '=', 'posts.user_id')
                    ->leftJoin('users AS editor', 'editor.id', '=', 'editor_id')
                    ->leftJoin('groups', 'groups.id', '=', 'author.group_id')
                    ->leftJoin('post_accepts AS pa', 'pa.post_id', '=', 'posts.id')
                    ->orderBy('posts.id')
                    ->orderBy('sessions.updated_at', 'DESC');

        if ($userId) {
            // pobieramy wartosc "id" a nie "created_at" poniewaz kiedys created_at nie bylo zapisywane
            $sql = $sql->addSelect(['pv.id AS vote_on', 'ps.id AS subscribe_on'])
                        ->leftJoin('post_votes AS pv', function ($join) use ($userId) {
                            $join->on('pv.post_id', '=', 'posts.id')->on('pv.user_id', '=', DB::raw($userId));
                        })
                        ->leftJoin('post_subscribers AS ps', function ($join) use ($userId) {
                            $join->on('ps.post_id', '=', 'posts.id')->on('ps.user_id', '=', DB::raw($userId));
                        });
        }

        return $sql;
    }

    /**<commit_msg>brak sortowania w znaczym stopniu przyspieszy zapytanie<commit_after><?php

namespace Coyote\Repositories\Eloquent;

use Coyote\Repositories\Contracts\PostRepositoryInterface;
use Coyote\Post\Subscriber;
use Coyote\Post\Attachment;
use DB;

class PostRepository extends Repository implements PostRepositoryInterface
{
    /**
     * @return \Coyote\Topic
     */
    public function model()
    {
        return 'Coyote\Post';
    }

    /**
     * @param int $userId
     * @return mixed
     */
    private function prepare($userId)
    {
        $sql = $this->model
                    ->selectRaw(
                        'DISTINCT ON(posts.id)
                        posts.*,
                        author.name AS author_name,
                        author.photo,
                        author.is_active,
                        author.is_blocked,
                        author.sig,
                        author.location,
                        author.posts AS author_posts,
                        author.allow_sig,
                        author.allow_smilies,
                        author.allow_count,
                        author.created_at AS author_created_at,
                        author.visited_at AS author_visited_at,
                        editor.name AS editor_name,
                        editor.name AS editor_is_active,
                        editor.name AS editor_is_blocked,
                        groups.name AS group_name,
                        sessions.updated_at AS session_updated_at,
                        pa.user_id AS accept_on'
                    )
                    ->leftJoin('sessions', 'sessions.user_id', '=', 'posts.user_id')
                    ->leftJoin('users AS author', 'author.id', '=', 'posts.user_id')
                    ->leftJoin('users AS editor', 'editor.id', '=', 'editor_id')
                    ->leftJoin('groups', 'groups.id', '=', 'author.group_id')
                    ->leftJoin('post_accepts AS pa', 'pa.post_id', '=', 'posts.id')
                    ->orderBy('posts.id');

        if ($userId) {
            // pobieramy wartosc "id" a nie "created_at" poniewaz kiedys created_at nie bylo zapisywane
            $sql = $sql->addSelect(['pv.id AS vote_on', 'ps.id AS subscribe_on'])
                        ->leftJoin('post_votes AS pv', function ($join) use ($userId) {
                            $join->on('pv.post_id', '=', 'posts.id')->on('pv.user_id', '=', DB::raw($userId));
                        })
                        ->leftJoin('post_subscribers AS ps', function ($join) use ($userId) {
                            $join->on('ps.post_id', '=', 'posts.id')->on('ps.user_id', '=', DB::raw($userId));
                        });
        }

        return $sql;
    }

    /**
{'fraction_non_alphanumeric': 0.12102711438319268, 'fraction_numerical': 0.0, 'mean_word_length': 1.3531896915927335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, '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': '2361135', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1487, 'n_words': 290}