<commit_before><?php namespace Wardrobe\Core\Repositories;

use Config, Cache, DateTime, Validator;
use Wardrobe\Core\Models\Post;
use Wardrobe\Core\Models\Tag;

class DbPostRepository implements PostRepositoryInterface {

	/**
	 * Get all of the posts.
	 *
	 * @return array
	 */
	public function all()
	{
		return Post::with(array('tags', 'user'))->orderBy('publish_date', 'desc')->get();
	}

	/**
	 * Get all of the active posts.
	 *
	 * @param int $per_page
	 *
	 * @return array
	 */
	public function active($per_page)
	{
		$per_page = is_numeric($per_page) ? $per_page : 5;

		return Post::with(array('tags', 'user'))
			->where('active', 1)
			->where('publish_date', '<=', new DateTime)
			->orderBy('publish_date', 'desc')
			->paginate($per_page);
	}

	/**
	 * Get a Post by its primary key.
	 *
	 * @param  int   $id
	 * @return Post
	 */
	public function find($id)
	{
		return Post::with(array('tags', 'user'))->findOrFail($id);
	}

	/**
	 * Get a Post by its slug
	 *
	 * @param  string 	$slug
	 * @return Post
	 */
	public function findBySlug($slug)
	{
		return Post::with(array('tags', 'user'))
			->where('active', 1)
			->where('publish_date', '<=', new DateTime)
			->where('slug', $slug)->first();
	}

	/**
	 * Get all posts with a tag
	 *
	 * @param  string   $tag
	 * @param  int      $per_page
	 * @return array
	 */
	public function activeByTag($tag, $per_page)
	{
		$per_page = is_numeric($per_page) ? $per_page : 5;

		return Post::with(array('tags', 'user'))
			->select('posts.*')
			->join('tags', 'posts.id', '=', 'tags.post_id')
			->where('tags.tag', '=', $tag)
			->orderBy('posts.publish_date', 'desc')
			->where('posts.active', 1)
			->where('posts.publish_date', '<=', new DateTime)
			->distinct()
			->paginate($per_page);
	}

	/**
	 * Search all active posts
	 *
	 * @param  string   $tag
	 * @param  int      $per_page
	 * @return array
	 */
	public function search($search, $per_page)
	{
		$per_page = is_numeric($per_page) ? $per_page : 5;

		return Post::with(array('tags', 'user'))
			->select('posts.*')
			->join('tags', 'posts.id', '=', 'tags.post_id')
			->where(function($query) use ($search)
			{
				$query->orWhere('title', 'like', '%'.$search.'%')
							->orWhere('content', 'like', '%'.$search.'%');
			})
			->orderBy('posts.publish_date', 'desc')
			->where('posts.active', '=', 1)
			->where('posts.publish_date', '<=', new DateTime)
			->distinct()
			->paginate($per_page);
	}

	/**
	 * Search from the wardrobe facet
	 *<commit_msg>Fixes #65 - Adding a groupBy for searching.<commit_after><?php namespace Wardrobe\Core\Repositories;

use Config, Cache, DateTime, Validator;
use Wardrobe\Core\Models\Post;
use Wardrobe\Core\Models\Tag;

class DbPostRepository implements PostRepositoryInterface {

	/**
	 * Get all of the posts.
	 *
	 * @return array
	 */
	public function all()
	{
		return Post::with(array('tags', 'user'))->orderBy('publish_date', 'desc')->get();
	}

	/**
	 * Get all of the active posts.
	 *
	 * @param int $per_page
	 *
	 * @return array
	 */
	public function active($per_page)
	{
		$per_page = is_numeric($per_page) ? $per_page : 5;

		return Post::with(array('tags', 'user'))
			->where('active', 1)
			->where('publish_date', '<=', new DateTime)
			->orderBy('publish_date', 'desc')
			->paginate($per_page);
	}

	/**
	 * Get a Post by its primary key.
	 *
	 * @param  int   $id
	 * @return Post
	 */
	public function find($id)
	{
		return Post::with(array('tags', 'user'))->findOrFail($id);
	}

	/**
	 * Get a Post by its slug
	 *
	 * @param  string 	$slug
	 * @return Post
	 */
	public function findBySlug($slug)
	{
		return Post::with(array('tags', 'user'))
			->where('active', 1)
			->where('publish_date', '<=', new DateTime)
			->where('slug', $slug)->first();
	}

	/**
	 * Get all posts with a tag
	 *
	 * @param  string   $tag
	 * @param  int      $per_page
	 * @return array
	 */
	public function activeByTag($tag, $per_page)
	{
		$per_page = is_numeric($per_page) ? $per_page : 5;

		return Post::with(array('tags', 'user'))
			->select('posts.*')
			->join('tags', 'posts.id', '=', 'tags.post_id')
			->where('tags.tag', '=', $tag)
			->orderBy('posts.publish_date', 'desc')
			->where('posts.active', 1)
			->where('posts.publish_date', '<=', new DateTime)
			->distinct()
			->paginate($per_page);
	}

	/**
	 * Search all active posts
	 *
	 * @param  string   $tag
	 * @param  int      $per_page
	 * @return array
	 */
	public function search($search, $per_page)
	{
		$per_page = is_numeric($per_page) ? $per_page : 5;

		return Post::with(array('tags', 'user'))
			->select('posts.*')
			->join('tags', 'posts.id', '=', 'tags.post_id')
			->where(function($query) use ($search)
			{
				$query->orWhere('title', 'like', '%'.$search.'%')
							->orWhere('content', 'like', '%'.$search.'%');
			})
			->orderBy('posts.publish_date', 'desc')
			->where('posts.active', '=', 1)
			->where('posts.publish_date', '<=', new DateTime)
			->groupBy('id')
			->distinct()
			->paginate($per_page);
	}

	/**
	 * Search from the wardrobe facet
	 *
{'fraction_non_alphanumeric': 0.2083416087388282, 'fraction_numerical': 0.0031777557100297915, 'mean_word_length': 3.3982532751091705, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 62, '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': '3396002', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1778, 'n_words': 389}