Ekapp/skolehjem/app/Http/Controllers/NewsController.php

157 lines
3.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Helpers;
use App\News;
use App\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class NewsController extends Controller
{
public function __construct()
{
$this->middleware([ "auth" ]);
$this->middleware([ "check.auth:news.list" ])->only("index");
$this->middleware([ "check.auth:news.show" ])->only("show");
$this->middleware([ "check.auth:news.create" ])->only("create", "store");
$this->middleware([ "check.auth:news.edit" ])->only("edit", "update");
$this->middleware([ "check.auth:news.delete" ])->only("delete");
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$news = News::query()->paginate($request->input("limit", 20));
return Response::detect("news.index", [ "news" => $news ]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return Response::detect("news.create");
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return RedirectResponse
*/
public function store(Request $request)
{
$data = $request->validate([
"subname" => "required",
"content" => "required"
]);
$news = new News($data);
$news->name = "";
$news->type_id = '1';
$news->save();
Helpers::sendNewsNotification($news, User::all());
return redirect()->route("news.index");
}
/**
* Store a newly created resource in storage.
*
* @return News
*/
public static function storeAndGet(News $news)
{
$news->save();
Helpers::sendNewsNotification($news, User::all());
return $news;
}
/**
* Display the specified resource.
*
* @param \App\News $news
* @return \Illuminate\Http\Response
*/
public function show(News $news)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\News $news
* @return \Illuminate\Http\Response
*/
public function edit(News $news)
{
return Response::detect("news.edit", [ "news" => $news ]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\News $news
* @return RedirectResponse
*/
public function update(Request $request, News $news)
{
$data = $request->validate([
"subname" => "required",
"content" => "required"
]);
$news->update($data);
Helpers::sendNewsNotification($news, User::all());
return redirect()->route("news.index");
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\News $news
* @return RedirectResponse
*/
public static function updateAndGet(News $news)
{
$news->update();
Helpers::sendNewsNotification($news, User::all());
return $news;
}
/**
* Remove the specified resource from storage.
*
* @param \App\News $news
* @return RedirectResponse
* @throws \Exception
*/
public function destroy(News $news)
{
$news->delete();
return redirect()->route("news.index");
}
}