add and delete news from admin dashboard #52

This commit is contained in:
Nabeel Shahzad 2018-01-08 16:22:26 -06:00
parent 5a12493739
commit 6b265ed67b
10 changed files with 149 additions and 21 deletions

View File

@ -28,6 +28,7 @@ if [ "$TRAVIS" = "true" ]; then
rm -rf env.php rm -rf env.php
find ./vendor -type d -name ".git" -print0 | xargs rm -rf find ./vendor -type d -name ".git" -print0 | xargs rm -rf
find . -type d -name "sass-cache" -print0 | xargs rm -rf
# clear any app specific stuff that might have been loaded in # clear any app specific stuff that might have been loaded in
find storage/app/public -mindepth 1 -not -name '.gitignore' -print0 -exec rm -rf {} + find storage/app/public -mindepth 1 -not -name '.gitignore' -print0 -exec rm -rf {} +

View File

@ -2,19 +2,23 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use Auth;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Repositories\NewsRepository;
use App\Repositories\PirepRepository; use App\Repositories\PirepRepository;
use App\Repositories\UserRepository; use App\Repositories\UserRepository;
class DashboardController extends BaseController class DashboardController extends BaseController
{ {
private $pirepRepo, $userRepo; private $newsRepo, $pirepRepo, $userRepo;
public function __construct( public function __construct(
NewsRepository $newsRepo,
PirepRepository $pirepRepo, PirepRepository $pirepRepo,
UserRepository $userRepo UserRepository $userRepo
) { ) {
$this->newsRepo = $newsRepo;
$this->pirepRepo = $pirepRepo; $this->pirepRepo = $pirepRepo;
$this->userRepo = $userRepo; $this->userRepo = $userRepo;
} }
@ -24,15 +28,32 @@ class DashboardController extends BaseController
*/ */
public function index(Request $request) public function index(Request $request)
{ {
/*Feed::$cacheDir = storage_path('app');
Feed::$cacheExpire = '5 hours';
$feed = Feed::loadRss(config('phpvms.news_feed_url'));*/
$feed = [];
return view('admin.dashboard.index', [ return view('admin.dashboard.index', [
'feed' => $feed, 'news' => $this->newsRepo->getLatest(),
'pending_pireps' => $this->pirepRepo->getPendingCount(), 'pending_pireps' => $this->pirepRepo->getPendingCount(),
'pending_users' => $this->userRepo->getPendingCount(), 'pending_users' => $this->userRepo->getPendingCount(),
]); ]);
} }
/**
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
public function news(Request $request)
{
if($request->isMethod('post')) {
$attrs = $request->post();
$attrs['user_id'] = Auth::user()->id;
$this->newsRepo->create($request->post());
} elseif ($request->isMethod('delete')) {
$news_id = $request->input('news_id');
$this->newsRepo->delete($news_id);
}
return view('admin.dashboard.news', [
'news' => $this->newsRepo->getLatest(),
]);
}
} }

View File

@ -15,7 +15,6 @@ class Kernel extends HttpKernel
*/ */
protected $middleware = [ protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Spatie\Pjax\Middleware\FilterIfPjax::class,
]; ];
/** /**
@ -32,6 +31,7 @@ class Kernel extends HttpKernel
\Illuminate\View\Middleware\ShareErrorsFromSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class,
#\App\Http\Middleware\VerifyCsrfToken::class, #\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class, \Illuminate\Routing\Middleware\SubstituteBindings::class,
\Spatie\Pjax\Middleware\FilterIfPjax::class,
], ],
'api' => [ 'api' => [

View File

@ -0,0 +1,29 @@
<?php
namespace App\Repositories;
use App\Models\News;
use App\Repositories\Traits\CacheableRepository;
use Prettus\Repository\Contracts\CacheableInterface;
class NewsRepository extends BaseRepository implements CacheableInterface
{
use CacheableRepository;
public function model()
{
return News::class;
}
/**
* Latest news items
* @param int $count
* @return mixed
*/
public function getLatest($count=5)
{
return $this->orderBy('created_at', 'desc')
->with(['user'])
->paginate($count);
}
}

View File

@ -54,7 +54,9 @@ class PirepRepository extends BaseRepository
$where['user_id'] = $user->id; $where['user_id'] = $user->id;
} }
$pireps = $this->orderBy('created_at', 'desc')->findWhere($where)->count(); $pireps = $this->orderBy('created_at', 'desc')
->findWhere($where, ['id'])
->count();
return $pireps; return $pireps;
} }
} }

View File

@ -32,7 +32,10 @@ class UserRepository extends BaseRepository
'state' => UserState::PENDING, 'state' => UserState::PENDING,
]; ];
$users = $this->orderBy('created_at', 'desc')->findWhere($where)->count(); $users = $this->orderBy('created_at', 'desc')
->findWhere($where, ['id'])
->count();
return $users; return $users;
} }

View File

@ -50,5 +50,9 @@ Route::group([
# defaults # defaults
Route::get('', ['uses' => 'DashboardController@index']); Route::get('', ['uses' => 'DashboardController@index']);
Route::get('/', ['uses' => 'DashboardController@index']); Route::get('/', ['uses' => 'DashboardController@index']);
Route::get('/dashboard', ['uses' => 'DashboardController@index', 'name' => 'dashboard']);
Route::get('dashboard', ['uses' => 'DashboardController@index', 'name' => 'dashboard']);
Route::match(['get', 'post', 'delete'],
'dashboard/news', ['uses' => 'DashboardController@news'])
->name('dashboard.news');
}); });

View File

@ -1,9 +0,0 @@
<div class="card border-blue-bottom">
<div class="header">
<h4 class="title">Announcements</h4>
<p class="category">just updated</p>
</div>
<div class="content">
blah blah blah
</div>
</div>

View File

@ -5,7 +5,7 @@
<div class="row"> <div class="row">
<div class="col-md-7"> <div class="col-md-7">
@include('admin.dashboard.announcements') @include('admin.dashboard.news')
</div> </div>
<div class="col-md-5"> <div class="col-md-5">
@component('admin.components.infobox') @component('admin.components.infobox')
@ -43,4 +43,17 @@
</div> </div>
</div> </div>
@endsection @endsection
@section('scripts')
<script>
$(document).ready(function() {
$(document).on('submit', 'form.pjax_news_form', function (event) {
event.preventDefault();
$.pjax.submit(event, '#pjax_news_wrapper', {push: false});
});
/*$(document).on('pjax:complete', function () {
$(".select2").select2();
});*/
});
</script>
@endsection

View File

@ -0,0 +1,64 @@
<div id="pjax_news_wrapper">
<div class="card border-blue-bottom">
@if($news->count() === 0)
<div class="text-center text-muted" style="padding: 30px;">
no news items
</div>
@endif
@foreach($news as $item)
<div class="content">
<div class="header">
<h4 class="title">{!! $item->subject !!}</h4>
<p class="category">{!! $item->user->name !!} - {!! show_datetime($item->created_at) !!}</p>
</div>
{!! $item->body !!}
<div class="text-right">
{!! Form::open(['route' => 'admin.dashboard.news',
'method' => 'delete',
'class' => 'pjax_news_form',
]) !!}
{!! Form::hidden('news_id', $item->id) !!}
{!!
Form::button('delete',
['type' => 'submit',
'class' => ' btn btn-danger btn-xs text-small'])
!!}
{!! Form::close() !!}
</div>
</div>
<hr />
@endforeach
<div class="content">
<div class="header">
<h4 class="title">Add News</h4>
</div>
{!! Form::open(['route' => 'admin.dashboard.news',
'method' => 'post',
'class' => 'pjax_news_form',
]) !!}
<table class="table">
<tr>
<td>{!! Form::label('subject', 'Subject:') !!}</td>
<td>{!! Form::text('subject', '', ['class' => 'form-control']) !!}</td>
</tr>
<tr>
<td>{!! Form::label('body', 'Body:') !!}</td>
<td>{!! Form::textarea('body', '', ['class' => 'form-control']) !!}</td>
</tr>
</table>
<div class="text-right">
{!!
Form::button('<i class="glyphicon glyphicon-plus"></i> add',
['type' => 'submit',
'class' => 'btn btn-success btn-s'])
!!}
{!! Form::close() !!}
</div>
</div>
</div>
</div>