blue-twilight/app/Http/Controllers/Admin/AlbumController.php

140 lines
3.0 KiB
PHP

<?php
namespace app\Http\Controllers\Admin;
use App\Album;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
class AlbumController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$this->authorize('admin-access');
$albums = Album::all()->sortBy('name');
return view('admin.list_albums', [
'albums' => $albums
]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$this->authorize('admin-access');
return view('admin.create_album');
}
public function delete($id)
{
$this->authorize('admin-access');
$album = $this->loadAlbum($id);
return view('admin.delete_album', ['album' => $album]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Requests\StoreAlbumRequest $request)
{
$this->authorize('admin-access');
$album = new Album();
$album->fromRequest($request)->save();
return redirect(route('albums.index'));
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$this->authorize('admin-access');
$album = $this->loadAlbum($id);
return view('admin.show_album', ['album' => $album]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$this->authorize('admin-access');
$album = $this->loadAlbum($id);
return view('admin.edit_album', ['album' => $album]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Requests\StoreAlbumRequest $request, $id)
{
$this->authorize('admin-access');
$album = $this->loadAlbum($id);
$album->fromRequest($request)->save();
return view('admin.show_album', ['album' => $album]);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$this->authorize('admin-access');
$album = $this->loadAlbum($id);
$album->delete();
return redirect(route('albums.index'));
}
private function loadAlbum($id)
{
$album = Album::all()->where('id', intval($id))->first();
if (is_null($album))
{
App::abort(404);
return null;
}
return $album;
}
}