/home/mip/mip/app/Modules/Client/Repositories/Posts/PostsRepository.php
<?php

namespace QxCMS\Modules\Client\Repositories\Posts;
use Illuminate\Support\Str;
use QxCMS\Modules\AbstractRepository;
use QxCMS\Modules\Client\Models\Posts\Posts;
use QxCMS\Modules\Client\Models\Settings\UserLogs\UserLogs as Log;
use Illuminate\Http\Request;
use Carbon\Carbon;

class PostsRepository extends AbstractRepository implements PostsRepositoryInterface
{
    protected $model;
    protected $log;

    function __construct(Posts $model, Log $log)
    {
        $this->model = $model;
        $this->log = $log;
    }
    
    public function create(Request $request)
    {
        $user = auth()->user();
        $model = $this->model->fill($request->all());
        $model->author_id = $user->id;
        $model->slug = Str::slug($request['title']);
        $model->post_type = 'Post';

        $model->save();
        $this->log->saveLog(['action' => 'Create', 'module_id' => $this->getModuleId(), 'user_id' => $user->id, 'data_id' => $model->id]);
        return $model;
    }

    public function update($id, Request $request)
    {
        $model = $this->findById($id);
        $user = auth()->user();
        $model->fill($request->all());
        $model->slug = Str::slug($request['title']);

        if(count($model->getDirty()) > 0) {
            $model->save();
            session()->flash('success', 'Successfully edited.');
            $this->log->saveLog(['action' => 'Update', 'module_id' => $this->getModuleId(), 'user_id' => $user->id, 'data_id' => $model->id]);
        }

        return $model;
    }

    public function delete($id)
    {
        $model = $this->findById($id);
        $user = auth()->user();
        $model->delete();
        $this->log->saveLog(['action' => 'Delete', 'module_id' => $this->getModuleId(), 'user_id' => $user->id, 'data_id' => $model->id]);

        return $this->getAjaxResponse('success', 'Successfully deleted.');
    }

    public function getAllPost($limit = 10)
    {
        $post = $this->model;
        return $this->getActivePost($post)->latest()->paginate($limit);
    }

    public function getLatestPost($limit = 10)
    {
        $post = $this->model;
        return $this->getActivePost($post)->latest()->take($limit)->get();
    }

     public function findBySlug($slug)
    {
        return $this->model->where('slug', '=', $slug)->get();
    }

    public function getLatestByCategory($slug, $limit = 4)
    {
        $post = $this->model
            ->whereHas('category', function($query) use($slug) {
                $query->where('slug', '=', $slug);
            });

        return $this->getActivePost($post)->latest()->paginate($limit);
    }

    public function getAllPage()
    {
        return $this->model->where('status', 'Publish')->where('post_type', 'Page')->get();
    }

    protected function getActivePost($model)
    {
        $date_today = Carbon::now();
        return $model
            ->where('published_at', '<=', $date_today)
            ->where('status', 'Publish')
            ->where('post_type', 'Post')
            ->where(function($query) use ($date_today){
                $query->orWhere('expired_at', '>', $date_today);
                $query->orWhere('expired_at', '=', '0000-00-00');
            });
    }
}