From 3342dd10874457af55054e1f69119eb81cfadb36 Mon Sep 17 00:00:00 2001 From: Celine Date: Wed, 3 Apr 2024 21:59:50 +0200 Subject: [PATCH] V 0.1.3 Neu:Reviews --- CHANGELOG.md | 3 + app/Http/Controllers/ReviewController.php | 210 ++++++++++++++++++ app/Models/Review.php | 22 ++ ...2024_03_09_194857_create_reviews_table.php | 31 +++ resources/views/review/create.blade.php | 16 ++ resources/views/review/edit.blade.php | 14 ++ resources/views/review/index.blade.php | 40 ++++ resources/views/review/show.blade.php | 17 ++ routes/web.php | 16 +- 9 files changed, 362 insertions(+), 7 deletions(-) create mode 100644 app/Http/Controllers/ReviewController.php create mode 100644 app/Models/Review.php create mode 100644 database/migrations/2024_03_09_194857_create_reviews_table.php create mode 100644 resources/views/review/create.blade.php create mode 100644 resources/views/review/edit.blade.php create mode 100644 resources/views/review/index.blade.php create mode 100644 resources/views/review/show.blade.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 191a869..b442109 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# V 0.1.3 Neu:Reviews + + # V 0.1.2 Grammatik ## Changed diff --git a/app/Http/Controllers/ReviewController.php b/app/Http/Controllers/ReviewController.php new file mode 100644 index 0000000..7f044b2 --- /dev/null +++ b/app/Http/Controllers/ReviewController.php @@ -0,0 +1,210 @@ +btnSubmit === 'btnCancel') { + $rc = redirect('/review-index'); + } else { + $fields = $request->all(); + if (count($fields) === 0) { + $fields = [ + 'nickname' => '', + 'stars' => '', + 'title' => '', + 'comment' => '', + 'email' => '' + ]; + } + $context = new ContextLaraKnife($request, $fields); + $rc = view('review.create', [ + 'context' => $context, + ]); + } + return $rc; + } + /** + * Show the form for editing the specified resource. + */ + public function edit(Review $review, Request $request) + { + if ($request->btnSubmit === 'btnCancel') { + $rc = redirect('/review-index'); + } else { + $fields = $request->all(); + if (count($fields) === 0) { + $fields = [ + 'nickname' => '', + 'stars' => '', + 'title' => '', + 'comment' => '', + 'email' => '' + ]; + } + $context = new ContextLaraKnife($request, null, $review); + $rc = view('review.edit', [ + 'context' => $context, + ]); + } + return $rc; + } + /** + * Remove the specified resource from storage. + */ + public function destroy(Review $review, Request $request) + { + if ($request->btnSubmit === 'btnDelete') { + $review->delete(); + } + return redirect('/review-index'); + } + /** + * Display the database records of the resource. + */ + public function index(Request $request) + { + if ($request->btnSubmit === 'btnNew') { + return redirect('/review-create'); + } else { + $sql = 'SELECT t0.*' + . ' FROM reviews t0' + ; + $parameters = []; + $fields = $request->all(); + if (count($fields) == 0) { + $fields = [ + 'nickname' => '', + 'stars' => '', + 'title' => '', + 'comment' => '', + 'email' => '', + '_sortParams' => 'id:asc' + . ';nickname:desc' + ]; + } else { + $conditions = []; + ViewHelper::addConditionPattern($conditions, $parameters, 'nickname'); + ViewHelper::addConditionPattern($conditions, $parameters, 'title'); + ViewHelper::addConditionPattern($conditions, $parameters, 'comment'); + ViewHelper::addConditionPattern($conditions, $parameters, 'email'); + ViewHelper::addConditionComparism($conditions, $parameters, 'stars', null, '='); + $sql = DbHelper::addConditions($sql, $conditions); + } + $sql = DbHelper::addOrderBy($sql, $fields['_sortParams']); + $pagination = new Pagination($sql, $parameters, $fields); + $records = $pagination->records; + $context = new ContextLaraKnife($request, $fields); + return view('review.index', [ + 'context' => $context, + 'records' => $records, + 'pagination' => $pagination + ]); + } + } + /** + * Returns the validation rules. + * @return array The validation rules. + */ + private function rules(bool $isCreate=false): array + { + $rc = [ + 'nickname' => 'required', + 'stars' => 'required|integer|min:1|max:5', + 'title' => 'required', + 'comment' => '', + 'email' => 'required|email' + ]; + return $rc; + } + public static function routes() + { + Route::get('/review-index', [ReviewController::class, 'index'])->middleware('auth'); + Route::post('/review-index', [ReviewController::class, 'index'])->middleware('auth'); + Route::get('/review-create', [ReviewController::class, 'create'])->middleware('auth'); + Route::put('/review-store', [ReviewController::class, 'store'])->middleware('auth'); + Route::post('/review-edit/{review}', [ReviewController::class, 'edit'])->middleware('auth'); + Route::get('/review-edit/{review}', [ReviewController::class, 'edit'])->middleware('auth'); + Route::post('/review-update/{review}', [ReviewController::class, 'update'])->middleware('auth'); + Route::get('/review-show/{review}/delete', [ReviewController::class, 'show'])->middleware('auth'); + Route::delete('/review-show/{review}/delete', [ReviewController::class, 'destroy'])->middleware('auth'); + } + /** + * Display the specified resource. + */ + public function show(Review $review, Request $request) + { + if ($request->btnSubmit === 'btnCancel') { + $rc = redirect('/review-index')->middleware('auth'); + } else { + $context = new ContextLaraKnife($request, null, $review); + $rc = view('review.show', [ + 'context' => $context, + 'mode' => 'delete' + ]); + } + return $rc; + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + $rc = null; + if ($request->btnSubmit === 'btnStore') { + $fields = $request->all(); + $validator = Validator::make($fields, $this->rules(true)); + if ($validator->fails()) { + $rc = back()->withErrors($validator)->withInput(); + } else { + $validated = $validator->validated(); + $validated['comment'] = strip_tags($validated['comment']); + Review::create($validated); + } + } + if ($rc == null){ + $rc = redirect('/review-index'); + } + return $rc; + } + /** + * Update the specified resource in storage. + */ + public function update(Review $review, Request $request) + { + $rc = null; + if ($request->btnSubmit === 'btnStore') { + $fields = $request->all(); + $validator = Validator::make($fields, $this->rules(false)); + if ($validator->fails()) { + $rc = back()->withErrors($validator)->withInput(); + } else { + $validated = $validator->validated(); + $validated['comment'] = strip_tags($validated['comment']); + $review->update($validated); + } + } + if ($rc == null){ + $rc = redirect('/review-index'); + } + return $rc; + } +} diff --git a/app/Models/Review.php b/app/Models/Review.php new file mode 100644 index 0000000..32c14ee --- /dev/null +++ b/app/Models/Review.php @@ -0,0 +1,22 @@ +id(); + $table->timestamps(); + $table->string('nickname'); + $table->integer('stars'); + $table->string('title'); + $table->text('comment'); + $table->string('email')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('reviews'); + } +}; diff --git a/resources/views/review/create.blade.php b/resources/views/review/create.blade.php new file mode 100644 index 0000000..5471bd0 --- /dev/null +++ b/resources/views/review/create.blade.php @@ -0,0 +1,16 @@ +@extends('layouts.backend') + +@section('content') +
+ @csrf + @method('PUT') + + + + + + + + +
+@endsection diff --git a/resources/views/review/edit.blade.php b/resources/views/review/edit.blade.php new file mode 100644 index 0000000..d6f4ac0 --- /dev/null +++ b/resources/views/review/edit.blade.php @@ -0,0 +1,14 @@ +@extends('layouts.backend') + +@section('content') +
+ @csrf + + + + + + + +
+@endsection diff --git a/resources/views/review/index.blade.php b/resources/views/review/index.blade.php new file mode 100644 index 0000000..3bd3c6e --- /dev/null +++ b/resources/views/review/index.blade.php @@ -0,0 +1,40 @@ +@extends('layouts.backend') + +@section('content') +
+ @csrf + + + + + + + + + + + {{__('Nickname')}} + {{__('Stars')}} + {{__('Title')}} + {{__('Comment')}} + {{__('Email')}} + + + + +@foreach ($records as $review) + + + {{$review->nickname}} + {{$review->stars}} + {{$review->title}} + {{$review->comment}} + {{$review->email}} + + +@endforeach + + + +
+@endsection diff --git a/resources/views/review/show.blade.php b/resources/views/review/show.blade.php new file mode 100644 index 0000000..89f4bc2 --- /dev/null +++ b/resources/views/review/show.blade.php @@ -0,0 +1,17 @@ +@extends('layouts.backend') + +@section('content') +
+ @csrf + @if($mode === 'delete') + @method('DELETE') + @endif + + + + + + + +
+@endsection diff --git a/routes/web.php b/routes/web.php index 04b4f16..1d9b99a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,17 +1,18 @@