Albums can now be edited. De-duplicated code in the AlbumController

This commit is contained in:
Andy Heathershaw 2016-09-01 17:31:16 +01:00
parent b4f35649c2
commit 932f7017dc
7 changed files with 86 additions and 24 deletions

View File

@ -27,12 +27,11 @@ class Album extends Model
protected $hidden = [ protected $hidden = [
]; ];
public static function fromRequest(Request $request) public function fromRequest(Request $request)
{ {
$album = new Album(); $this->name = $request->get('name');
$album->name = $request->get('name'); $this->description = $request->get('description');
$album->description = $request->get('description');
return $album; return $this;
} }
} }

View File

@ -42,11 +42,7 @@ class AlbumController extends Controller
{ {
$this->authorize('admin-access'); $this->authorize('admin-access');
$album = Album::all()->where('id', intval($id))->first(); $album = $this->loadAlbum($id);
if (is_null($album))
{
App::abort(404);
}
return view('admin.delete_album', ['album' => $album]); return view('admin.delete_album', ['album' => $album]);
} }
@ -61,8 +57,8 @@ class AlbumController extends Controller
{ {
$this->authorize('admin-access'); $this->authorize('admin-access');
$album = Album::fromRequest($request); $album = new Album();
$album->save(); $album->fromRequest($request)->save();
return redirect(route('albums.index')); return redirect(route('albums.index'));
} }
@ -77,11 +73,7 @@ class AlbumController extends Controller
{ {
$this->authorize('admin-access'); $this->authorize('admin-access');
$album = Album::all()->where('id', intval($id))->first(); $album = $this->loadAlbum($id);
if (is_null($album))
{
App::abort(404);
}
return view('admin.show_album', ['album' => $album]); return view('admin.show_album', ['album' => $album]);
} }
@ -95,6 +87,10 @@ class AlbumController extends Controller
public function edit($id) public function edit($id)
{ {
$this->authorize('admin-access'); $this->authorize('admin-access');
$album = $this->loadAlbum($id);
return view('admin.edit_album', ['album' => $album]);
} }
/** /**
@ -104,9 +100,14 @@ class AlbumController extends Controller
* @param int $id * @param int $id
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function update(Request $request, $id) public function update(Requests\StoreAlbumRequest $request, $id)
{ {
$this->authorize('admin-access'); $this->authorize('admin-access');
$album = $this->loadAlbum($id);
$album->fromRequest($request)->save();
return view('admin.show_album', ['album' => $album]);
} }
/** /**
@ -119,13 +120,21 @@ class AlbumController extends Controller
{ {
$this->authorize('admin-access'); $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(); $album = Album::all()->where('id', intval($id))->first();
if (is_null($album)) if (is_null($album))
{ {
App::abort(404); App::abort(404);
return null;
} }
$album->delete(); return $album;
return redirect(route('albums.index'));
} }
} }

View File

@ -5,5 +5,6 @@ return [
'delete_action' => 'Delete', 'delete_action' => 'Delete',
'description_label' => 'Description:', 'description_label' => 'Description:',
'edit_action' => 'Edit', 'edit_action' => 'Edit',
'name_label' => 'Name:' 'name_label' => 'Name:',
'save_action' => 'Save Changes'
]; ];

View File

@ -11,7 +11,7 @@
@lang('admin.delete_album_warning') @lang('admin.delete_album_warning')
</div> </div>
<div class="form-actions"> <div class="form-actions">
{!! Form::open(['url' => route('albums.destroy', ['id' => $album->id]), 'method' => 'DELETE']) !!} {!! Form::open(['route' => ['albums.destroy', $album->id], 'method' => 'DELETE']) !!}
<a href="{{ route('albums.show', ['id' => $album->id]) }}" class="btn btn-default">@lang('forms.cancel_action')</a> <a href="{{ route('albums.show', ['id' => $album->id]) }}" class="btn btn-default">@lang('forms.cancel_action')</a>
{!! Form::submit(trans('forms.delete_action'), ['class' => 'btn btn-danger']) !!} {!! Form::submit(trans('forms.delete_action'), ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!} {!! Form::close() !!}

View File

@ -0,0 +1,42 @@
@extends('layouts.app')
@section('title', 'Gallery Admin')
@section('content')
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1>@lang('admin.create_album')</h1>
<p>@lang('admin.create_album_intro')</p>
<p>@lang('admin.create_album_intro2')</p>
<hr/>
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
{!! Form::model($album, ['route' => ['albums.update', $album->id], 'method' => 'PUT']) !!}
<div class="form-group">
{!! Form::label('name', trans('forms.name_label'), ['class' => 'control-label']) !!}
{!! Form::text('name', old('name'), ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('description', trans('forms.description_label'), ['class' => 'control-label']) !!}
{!! Form::textarea('description', old('description'), ['class' => 'form-control']) !!}
</div>
<div class="form-actions">
{!! Form::submit(trans('forms.save_action'), ['class' => 'btn btn-success']) !!}
<a href="{{ route('albums.show', ['id' => $album->id]) }}" class="btn btn-default">@lang('forms.cancel_action')</a>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
@endsection

View File

@ -8,7 +8,11 @@
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading">@lang('admin.stats_panel')</div> <div class="panel-heading">@lang('admin.stats_panel')</div>
<div class="panel-body"> <div class="panel-body">
<p><b>{{ $album_count }}</b> {{ trans_choice('admin.stats_albums', $album_count) }}</p> <p>
<a href="{{ route('albums.index') }}">
<b>{{ $album_count }}</b> {{ trans_choice('admin.stats_albums', $album_count) }}
</a>
</p>
</div> </div>
</div> </div>
</div> </div>

View File

@ -24,7 +24,10 @@
<tbody> <tbody>
@foreach ($albums as $album) @foreach ($albums as $album)
<tr> <tr>
<td><a href="{{ route('albums.show', ['id' => $album->id]) }}">{{ $album->name }}</a></td> <td>
<span style="font-size: 1.3em;"><a href="{{ route('albums.show', ['id' => $album->id]) }}">{{ $album->name }}</a></span><br/>
<p>{{ $album->description }}</p>
</td>
<td class="text-right"> <td class="text-right">
<a href="{{ route('albums.edit', ['id' => $album->id]) }}" class="btn btn-default">@lang('forms.edit_action')</a> <a href="{{ route('albums.edit', ['id' => $album->id]) }}" class="btn btn-default">@lang('forms.edit_action')</a>
<a href="{{ route('albums.delete', ['id' => $album->id]) }}" class="btn btn-danger">@lang('forms.delete_action')</a> <a href="{{ route('albums.delete', ['id' => $album->id]) }}" class="btn btn-danger">@lang('forms.delete_action')</a>
@ -33,6 +36,10 @@
@endforeach @endforeach
</tbody> </tbody>
</table> </table>
<div style="margin-top: 10px;">
<a href="{{ route('albums.create') }}" class="btn btn-success"><i class="fa fa-fw fa-plus"></i> @lang('admin.create_album_link')</a>
</div>
@endif @endif
</div> </div>
</div> </div>