<file_sep><?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Repositories\CommentRepository;
use App\Repositories\UserRepository;
use App\Repositories\NewsRepository;
use App\Http\Resources\Resource as NewsResource;

class ResourceController extends SiteController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct(NewsRepository $news_rep, UserRepository $user_rep, CommentRepository $comment_rep)
    {

        $this->user_rep = $user_rep;
        $this->news_rep = $news_rep;
        $this->comment_rep = $comment_rep;

    }

    public function index()
    {
        $news = $this->news_rep->get();

        return NewsResource::collection($news);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $news = $this->news_rep->getOne($id);

        return new NewsResource($news);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function getNews()
    {
        $news = $this->news_rep->get();

        return $news;
    }
}
<file_sep><?php

namespace App\Http\Controllers;

use App\Repositories\MenusRepository;
use Illuminate\Http\Request;
use App\Repositories\NewsRepository;

class SiteController extends Controller
{
    protected $news_rep;
    protected $menu_rep;
    protected $user_rep;
    protected $comment_rep;

    protected $vars = array();
    protected $template;

    public function __construct()
    {

    }

    public function renderOutput()
    {
        $menus = view(env('THEME') . '.menu')->render();
        $this->vars = array_add($this->vars, 'menus', $menus);


        return view($this->template)->with($this->vars);
    }




}
<file_sep><?php

use Faker\Generator as Faker;

$factory->define(App\News::class, function (Faker $faker) {
    return [
        'title'   => $faker->text(120),
        'desc'    => $faker->text(1500),
        'user_id' => 1

    ];
});
<file_sep><?php
/**
 * Created by PhpStorm.
 * User: dudav
 * Date: 01.04.2018
 * Time: 20:06
 */

namespace App\Repositories;

use App\User;

class UserRepository extends Repository
{
    public function __construct(User $user)
    {
        $this->model = $user;
    }

}<file_sep><?php

namespace App\Http\Controllers;


use App\Repositories\CommentRepository;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use App\Repositories\NewsRepository;

class IndexController extends SiteController
{
    public function __construct(NewsRepository $news_rep, UserRepository $user_rep, CommentRepository $comment_rep)
    {

        $this->user_rep = $user_rep;
        $this->news_rep = $news_rep;
        $this->comment_rep = $comment_rep;
        $this->template = env('THEME') . '.index';
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
            $news = $this->news_rep->get();
            $content = view(env('THEME') . '.content')->with('news', $news)->render();
            $this->vars = array_add($this->vars, 'content', $content);

            return $this->renderOutput();
    }

    public function home()
    {
        $user_id  = \Auth::user()->id;
        $news = $this->news_rep->getUser($user_id );
        $content = view(env('THEME') . '.home')->with('news', $news)->render();
        $this->vars = array_add($this->vars, 'content', $content);

        return $this->renderOutput();

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

       //auth()->guard()->check())/
        $user_id = \Auth::user()->id;
            if($request->isMethod('post')){
                $input = $request->except('_token');
                $input = array_add($input, 'user_id', $user_id );
                $validator = $this->news_rep->validator($input);
                if($validator->fails()){

                    return redirect()->route('add')->withErrors($validator)->withInput();
                }else{
                    $this->news_rep->add($input);
                    return redirect()->route('add')->with('status', 'Новость добавлена');
                }


                //return redirect()->intended(route('oneNews', ['id' => 1]));
            }else{
                $content = view(env('THEME') . '.homeAdd')->render();
                $this->vars = array_add($this->vars, 'content', $content);

                return $this->renderOutput();

            }
            //return redirect()->intended('login');
    }

    public function addComment(Request $request)
    {
        $user_id = \Auth::user()->id;

        if($request->isMethod('post')){
            $input = $request->except('_token');
            $input = array_add($input, 'user_id', $user_id );
            $validator = $this->comment_rep->validator($input);
/*dd($input);*/
            if($validator->fails()){
                return redirect()->route('oneNews', ['id' => $request->news_id])->withErrors($validator)->withInput();
            }else{
                $this->comment_rep->add($input);
                return redirect()->route('oneNews', ['id' => $request->news_id])->with('status', 'Новость добавлена');
            }
        }else{
            return redirect()->route('oneNews', ['id' => $request->news_id]);

        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $news = $this->news_rep->getOne($id);

        $content = view(env('THEME') . '.content_one')->with(['news'=> $news])->render();
        $this->vars = array_add($this->vars, 'content', $content);

        return $this->renderOutput();
    }

    public function user()
    {

        $id = \Auth::user()->id;
        $user = $this->user_rep->getOne($id);

        $content = view(env('THEME') . '.user')->with('user', $user)->render();
        $this->vars = array_add($this->vars, 'content', $content);

        return $this->renderOutput();
    }

    public function folover()
    {
        print_r('asdasdas');
    }
    public function email(Request $request)
    {
        if($request->isMethod('post') ) {

            return redirect()->route('email')->with('status', 'Отправлено');
        }
        $content = view(env('THEME') . '.email')->render();
        $this->vars = array_add($this->vars, 'content', $content);

        return $this->renderOutput();
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $id = \Auth::user()->id;
        $news = $this->news_rep->getOne($request->news_id);
        if($request->isMethod('post') && $id  == $news->user->id) {

            $news->forceDelete();
        }

        return redirect()->route('index')->with('status', 'Удалено');
    }

    public function getNews()
    {
        $news = $this->news_rep->get();

        return $news;
    }
}
<file_sep><?php
/**
 * Created by PhpStorm.
 * User: dudav
 * Date: 31.03.2018
 * Time: 18:39
 */

namespace App\Repositories;

use App\News;
use Validator;

class NewsRepository extends Repository
{
    public function __construct(News $news)
    {
        $this->model = $news;
    }
    public function validator($input)
    {
        $validator = Validator::make($input, [
            'title'   => 'required|max:120',
            'desc'    => 'required|min:1'
        ]);

        return $validator;
    }

    public function getUser($id)
    {
        $builder = $this->model->where('user_id', $id);

        return $builder->get();

    }
}<file_sep><?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

/*Route::get('/', function () {
    return view('welcome');
});*/
Route::get('/', ['uses' => 'IndexController@index', 'as' => 'index']);
Route::post('email', ['uses' => 'IndexController@email', 'as' => 'email']);
Route::get('email', ['uses' => 'IndexController@email']);
Route::get('news/{id}', ['uses' => 'IndexController@show', 'as' => 'oneNews']);
Route::post('/news', ['uses' => 'IndexController@destroy', 'as' => 'delete'])->middleware('auth');
Route::post('comment', ['uses' => 'IndexController@addComment', 'as' => 'addComment'])->middleware('auth');





Auth::routes();

Route::group(['prefix' => 'home', 'middleware' => 'auth'], function(){
    Route::get('/', 'IndexController@home')->name('home');
    Route::get('/user', 'IndexController@user')->name('userInfo');
    Route::get('/add', 'IndexController@store')->name('add');
    Route::post('/add', 'IndexController@store')->name('postAdd');
    Route::get('/token', function () {
        return Auth::user()->createToken(\Auth::user()->id);
    })->name('token');
});
<file_sep><?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Folover extends Model
{
    //
    protected $table = 'folovers';
}
<file_sep><?php
/**
 * Created by PhpStorm.
 * User: dudav
 * Date: 01.04.2018
 * Time: 21:29
 */

namespace App\Repositories;

use App\Comment;
use Validator;

class CommentRepository extends Repository
{
    public function __construct(Comment $comment)
    {
        $this->model = $comment;
    }

    public function getNews($id)
    {
        $builder = $this->model->where('news_id', $id);

        return $builder->get();

    }
    public function validator($input)
    {
        $validator = Validator::make($input, [
            'text'   => 'required|min:1',
        ]);

        return $validator;
    }

}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: dudav
 * Date: 31.03.2018
 * Time: 18:39
 */

namespace App\Repositories;


abstract class Repository
{
    protected $model = false;

    public function get()
    {
        $builder = $this->model->select('*');

        return $builder->get();

    }

    public function getOne($id)
    {
        $result = $this->model->where('id', $id)->first();

        return $result;

    }

    public function add($input)
    {
        $model = new $this->model;
        $model->fill($input);
        $model->save();
        return $model;
    }
}