From 6b265ed67b7d486812a28b3621c53bdc098d35ea Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Mon, 8 Jan 2018 16:22:26 -0600 Subject: [PATCH] add and delete news from admin dashboard #52 --- .travis/deploy_script.sh | 1 + .../Controllers/Admin/DashboardController.php | 35 ++++++++-- app/Http/Kernel.php | 2 +- app/Repositories/NewsRepository.php | 29 +++++++++ app/Repositories/PirepRepository.php | 4 +- app/Repositories/UserRepository.php | 5 +- app/Routes/admin.php | 6 +- .../admin/dashboard/announcements.blade.php | 9 --- .../views/admin/dashboard/index.blade.php | 15 ++++- .../views/admin/dashboard/news.blade.php | 64 +++++++++++++++++++ 10 files changed, 149 insertions(+), 21 deletions(-) create mode 100644 app/Repositories/NewsRepository.php delete mode 100644 resources/views/admin/dashboard/announcements.blade.php create mode 100644 resources/views/admin/dashboard/news.blade.php diff --git a/.travis/deploy_script.sh b/.travis/deploy_script.sh index 1399efaf..d29f1bfb 100755 --- a/.travis/deploy_script.sh +++ b/.travis/deploy_script.sh @@ -28,6 +28,7 @@ if [ "$TRAVIS" = "true" ]; then rm -rf env.php 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 find storage/app/public -mindepth 1 -not -name '.gitignore' -print0 -exec rm -rf {} + diff --git a/app/Http/Controllers/Admin/DashboardController.php b/app/Http/Controllers/Admin/DashboardController.php index e6a874dc..918709d4 100644 --- a/app/Http/Controllers/Admin/DashboardController.php +++ b/app/Http/Controllers/Admin/DashboardController.php @@ -2,19 +2,23 @@ namespace App\Http\Controllers\Admin; +use Auth; use Illuminate\Http\Request; +use App\Repositories\NewsRepository; use App\Repositories\PirepRepository; use App\Repositories\UserRepository; class DashboardController extends BaseController { - private $pirepRepo, $userRepo; + private $newsRepo, $pirepRepo, $userRepo; public function __construct( + NewsRepository $newsRepo, PirepRepository $pirepRepo, UserRepository $userRepo ) { + $this->newsRepo = $newsRepo; $this->pirepRepo = $pirepRepo; $this->userRepo = $userRepo; } @@ -24,15 +28,32 @@ class DashboardController extends BaseController */ 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', [ - 'feed' => $feed, + 'news' => $this->newsRepo->getLatest(), 'pending_pireps' => $this->pirepRepo->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(), + ]); + } } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index a7fbe51c..af709e8a 100755 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -15,7 +15,6 @@ class Kernel extends HttpKernel */ protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, - \Spatie\Pjax\Middleware\FilterIfPjax::class, ]; /** @@ -32,6 +31,7 @@ class Kernel extends HttpKernel \Illuminate\View\Middleware\ShareErrorsFromSession::class, #\App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, + \Spatie\Pjax\Middleware\FilterIfPjax::class, ], 'api' => [ diff --git a/app/Repositories/NewsRepository.php b/app/Repositories/NewsRepository.php new file mode 100644 index 00000000..be29579a --- /dev/null +++ b/app/Repositories/NewsRepository.php @@ -0,0 +1,29 @@ +orderBy('created_at', 'desc') + ->with(['user']) + ->paginate($count); + } +} diff --git a/app/Repositories/PirepRepository.php b/app/Repositories/PirepRepository.php index 8d745570..9c080655 100644 --- a/app/Repositories/PirepRepository.php +++ b/app/Repositories/PirepRepository.php @@ -54,7 +54,9 @@ class PirepRepository extends BaseRepository $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; } } diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php index 5d6d3df4..abbd9b8f 100644 --- a/app/Repositories/UserRepository.php +++ b/app/Repositories/UserRepository.php @@ -32,7 +32,10 @@ class UserRepository extends BaseRepository 'state' => UserState::PENDING, ]; - $users = $this->orderBy('created_at', 'desc')->findWhere($where)->count(); + $users = $this->orderBy('created_at', 'desc') + ->findWhere($where, ['id']) + ->count(); + return $users; } diff --git a/app/Routes/admin.php b/app/Routes/admin.php index 848cbf0a..4be061e0 100644 --- a/app/Routes/admin.php +++ b/app/Routes/admin.php @@ -50,5 +50,9 @@ Route::group([ # defaults 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'); }); diff --git a/resources/views/admin/dashboard/announcements.blade.php b/resources/views/admin/dashboard/announcements.blade.php deleted file mode 100644 index a678d955..00000000 --- a/resources/views/admin/dashboard/announcements.blade.php +++ /dev/null @@ -1,9 +0,0 @@ -
-
-

Announcements

-

just updated

-
-
- blah blah blah -
-
diff --git a/resources/views/admin/dashboard/index.blade.php b/resources/views/admin/dashboard/index.blade.php index 9eac8441..a7880c19 100644 --- a/resources/views/admin/dashboard/index.blade.php +++ b/resources/views/admin/dashboard/index.blade.php @@ -5,7 +5,7 @@
- @include('admin.dashboard.announcements') + @include('admin.dashboard.news')
@component('admin.components.infobox') @@ -43,4 +43,17 @@
@endsection +@section('scripts') + +@endsection diff --git a/resources/views/admin/dashboard/news.blade.php b/resources/views/admin/dashboard/news.blade.php new file mode 100644 index 00000000..717413c0 --- /dev/null +++ b/resources/views/admin/dashboard/news.blade.php @@ -0,0 +1,64 @@ +
+
+ @if($news->count() === 0) +
+ no news items +
+ @endif + @foreach($news as $item) +
+
+

{!! $item->subject !!}

+

{!! $item->user->name !!} - {!! show_datetime($item->created_at) !!}

+
+ + {!! $item->body !!} + +
+ {!! 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() !!} +
+
+
+ @endforeach +
+
+

Add News

+
+ {!! Form::open(['route' => 'admin.dashboard.news', + 'method' => 'post', + 'class' => 'pjax_news_form', + ]) !!} + + + + + + + + + + +
{!! Form::label('subject', 'Subject:') !!}{!! Form::text('subject', '', ['class' => 'form-control']) !!}
{!! Form::label('body', 'Body:') !!}{!! Form::textarea('body', '', ['class' => 'form-control']) !!}
+
+ {!! + Form::button(' add', + ['type' => 'submit', + 'class' => 'btn btn-success btn-s']) + + !!} + {!! Form::close() !!} +
+
+
+