From f4e9e87335ec5769b2aee51aefce3076e2e3cc52 Mon Sep 17 00:00:00 2001
From: Beth Parker <ejparker@actcur.com>
Date: Mon, 7 Feb 2022 21:50:25 -0600
Subject: [PATCH] Added Recipe index page w/ filter

---
 app/Http/Controllers/RecipeController.php     | 28 ++++++++
 app/Recipe.php                                |  5 ++
 app/User.php                                  |  4 ++
 database/factories/RecipeFactory.php          | 17 -----
 ...2022_02_08_001417_create_recipes_table.php |  4 +-
 ...12614_create_recipe_ingredients_table.php} |  6 +-
 database/seeders/RecipeSeeder.php             | 39 +++++++++++
 public/css/layout.css                         |  4 ++
 resources/views/recipes/index.blade.php       | 70 +++++++++++++++++++
 routes/web.php                                |  1 +
 10 files changed, 156 insertions(+), 22 deletions(-)
 create mode 100644 app/Http/Controllers/RecipeController.php
 delete mode 100644 database/factories/RecipeFactory.php
 rename database/migrations/{2022_02_08_003527_create_recipe_ingredient_table.php => 2022_02_08_012614_create_recipe_ingredients_table.php} (81%)
 create mode 100644 database/seeders/RecipeSeeder.php
 create mode 100644 resources/views/recipes/index.blade.php

diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php
new file mode 100644
index 0000000..56aefe1
--- /dev/null
+++ b/app/Http/Controllers/RecipeController.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use Illuminate\Http\Request;
+use App\Recipe;
+use App\RecipeIngredient;
+use App\User;
+
+class RecipeController extends Controller
+{
+  public function index()
+  {
+    //Pass all users
+    $recipes = Recipe::with('user')->get();
+    return view('recipes.index')->with('recipes', $recipes);
+  }
+  public function show($id)
+  {
+    $recipe=Recipe::with('user')->with('ingredients')->findOrFail($id);
+    return $ingredients;
+  }
+  public function edit($id){
+    $recipe=Recipe::with('user')->with('ingredients')->findOrFail($id);
+    $ingredients=RecipeIngredient::get()->sortby('name')->pluck('name')->unique();
+    return $ingredients;
+  }
+}
diff --git a/app/Recipe.php b/app/Recipe.php
index 98acda5..34c9d05 100644
--- a/app/Recipe.php
+++ b/app/Recipe.php
@@ -10,6 +10,11 @@ class Recipe extends Model
     use HasFactory;
     protected $fillable = ['name','maintainer','author','servings','date_entered','date_modified','instructions'];
 
+    public function user()
+    {
+        return $this->belongsTo('App\User');
+    }
+
     public function ingredients()
     {
         return $this->hasMany('App\RecipeIngredient');
diff --git a/app/User.php b/app/User.php
index d69fdba..8ab8151 100644
--- a/app/User.php
+++ b/app/User.php
@@ -67,4 +67,8 @@ class User extends Authenticatable
     }
     return false;
   }
+
+  public function recipes(){
+    return $this->hasMany('App\Recipe');
+  }
 }
diff --git a/database/factories/RecipeFactory.php b/database/factories/RecipeFactory.php
deleted file mode 100644
index c146cb1..0000000
--- a/database/factories/RecipeFactory.php
+++ /dev/null
@@ -1,17 +0,0 @@
-<?php
-
-namespace Database\Factories;
-
-use Illuminate\Database\Eloquent\Factories\Factory;
-
-class RecipeFactory extends Factory
-{
-    /**
-     * Define the model's default state.
-     *
-     * @return array
-     */
-    public function definition()
-    {
-    }
-}
diff --git a/database/migrations/2022_02_08_001417_create_recipes_table.php b/database/migrations/2022_02_08_001417_create_recipes_table.php
index 0289167..66b04b8 100644
--- a/database/migrations/2022_02_08_001417_create_recipes_table.php
+++ b/database/migrations/2022_02_08_001417_create_recipes_table.php
@@ -16,7 +16,7 @@ class CreateRecipesTable extends Migration
         Schema::create('recipes', function (Blueprint $table) {
             $table->id();
             $table->string('name');
-            $table->unsignedInteger('maintainer');
+            $table->unsignedInteger('user_id');
             $table->string('author');
             $table->unsignedInteger('servings');
             $table->datetime('date_entered');
@@ -24,7 +24,7 @@ class CreateRecipesTable extends Migration
             $table->longtext('instructions');
             $table->timestamps();
 
-            $table->foreign('maintainer')->references('id')->on('users');
+            $table->foreign('user_id')->references('id')->on('users');
         });
     }
 
diff --git a/database/migrations/2022_02_08_003527_create_recipe_ingredient_table.php b/database/migrations/2022_02_08_012614_create_recipe_ingredients_table.php
similarity index 81%
rename from database/migrations/2022_02_08_003527_create_recipe_ingredient_table.php
rename to database/migrations/2022_02_08_012614_create_recipe_ingredients_table.php
index 096cfd8..ff4abeb 100644
--- a/database/migrations/2022_02_08_003527_create_recipe_ingredient_table.php
+++ b/database/migrations/2022_02_08_012614_create_recipe_ingredients_table.php
@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
 use Illuminate\Database\Schema\Blueprint;
 use Illuminate\Support\Facades\Schema;
 
-class CreateRecipeIngredientTable extends Migration
+class CreateRecipeIngredientsTable extends Migration
 {
     /**
      * Run the migrations.
@@ -13,7 +13,7 @@ class CreateRecipeIngredientTable extends Migration
      */
     public function up()
     {
-        Schema::create('recipe_ingredient', function (Blueprint $table) {
+        Schema::create('recipe_ingredients', function (Blueprint $table) {
             $table->id();
             $table->unsignedInteger('recipe_id');
             $table->unsignedInteger('order');
@@ -35,6 +35,6 @@ class CreateRecipeIngredientTable extends Migration
      */
     public function down()
     {
-        Schema::dropIfExists('recipe_ingredient');
+        Schema::dropIfExists('recipe_ingredients');
     }
 }
diff --git a/database/seeders/RecipeSeeder.php b/database/seeders/RecipeSeeder.php
new file mode 100644
index 0000000..179c5ea
--- /dev/null
+++ b/database/seeders/RecipeSeeder.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Database\Seeders;
+
+use Illuminate\Database\Seeder;
+use App\User;
+use App\Recipe;
+use App\RecipeIngredient;
+
+class RecipeSeeder extends Seeder
+{
+    /**
+     * Run the database seeds.
+     *
+     * @return void
+     */
+    public function run()
+    {
+        $first_user = User::orderBy('id')->first();
+
+        $recipe=Recipe::updateOrCreate(['name'=>'Test','user_id'=>$first_user['id'],'author'=>'Jayne','servings'=>1,'date_entered'=>now(),'date_modified'=>now(),'instructions'=>'These are instructions']);
+
+        RecipeIngredient::updateOrCreate(['recipe_id'=>$recipe['id'],'order'=>0,'alternative'=>false,'name'=>'hamburger','quantity'=>1.0,'measurement'=>'lb','special_notes'=>'']);
+        RecipeIngredient::updateOrCreate(['recipe_id'=>$recipe['id'],'order'=>0,'alternative'=>true,'name'=>'sausage','quantity'=>1.0,'measurement'=>'lb','special_notes'=>'']);
+        RecipeIngredient::updateOrCreate(['recipe_id'=>$recipe['id'],'order'=>0,'alternative'=>false,'name'=>'carrot','quantity'=>1.0,'measurement'=>'lb','special_notes'=>'']);
+
+        $recipe=Recipe::updateOrCreate(['name'=>'Test2','user_id'=>$first_user['id'],'author'=>'Karen','servings'=>1,'date_entered'=>now(),'date_modified'=>now(),'instructions'=>'These are instructions']);
+
+        RecipeIngredient::updateOrCreate(['recipe_id'=>$recipe['id'],'order'=>0,'alternative'=>false,'name'=>'chicken','quantity'=>1.0,'measurement'=>'lb','special_notes'=>'']);
+        RecipeIngredient::updateOrCreate(['recipe_id'=>$recipe['id'],'order'=>0,'alternative'=>false,'name'=>'carrot','quantity'=>1.0,'measurement'=>'lb','special_notes'=>'']);
+
+        $jayne=User::updateOrCreate(['name'=>'Jayne','email'=>'jayne.passmore@actcur.com','password'=>bcrypt('temp'),'created_at'=>NOW()]);
+
+        $recipe=Recipe::updateOrCreate(['name'=>'Test3','user_id'=>$jayne['id'],'author'=>'Jayne','servings'=>1,'date_entered'=>now(),'date_modified'=>now(),'instructions'=>'These are instructions']);
+
+        RecipeIngredient::updateOrCreate(['recipe_id'=>$recipe['id'],'order'=>0,'alternative'=>false,'name'=>'chicken','quantity'=>1.0,'measurement'=>'lb','special_notes'=>'']);
+        RecipeIngredient::updateOrCreate(['recipe_id'=>$recipe['id'],'order'=>0,'alternative'=>false,'name'=>'carrot','quantity'=>1.0,'measurement'=>'lb','special_notes'=>'']);
+    }
+}
diff --git a/public/css/layout.css b/public/css/layout.css
index 5b3924b..80c376c 100644
--- a/public/css/layout.css
+++ b/public/css/layout.css
@@ -265,3 +265,7 @@ list {
 .fix-spacing{
     padding-top: 30px;
 }
+
+.operations{
+    width:132px;
+}
diff --git a/resources/views/recipes/index.blade.php b/resources/views/recipes/index.blade.php
new file mode 100644
index 0000000..5d883be
--- /dev/null
+++ b/resources/views/recipes/index.blade.php
@@ -0,0 +1,70 @@
+@extends('layouts.default')
+@extends('content_wrappers.md-10')
+
+@section('title', '| Recipes')
+@section('heading', 'Recipes')
+
+@section('content')
+  <div class="table-responsive">
+    <table class="table table-bordered table-striped">
+
+      <thead>
+        <tr>
+          <th>Name</th>
+          <th>Created By</th>
+          <th>Maintainer</th>
+          <th>Operations</th>
+        </tr>
+      </thead>
+
+      <tbody id="filteredTable">
+        <tr>
+          <td><input id="nameFilter" class="filter" type="text" placeholder="Search.."></td>
+          <td><input id="authorFilter" class="filter" type="text" placeholder="Search.."></td>
+          <td><input id="userFilter" class="filter" type="text" placeholder="Search.."></td>
+          <td class='operations'></td>
+        </tr>
+        @foreach ($recipes as $recipe)
+        <tr class="rows">
+          <td class="name">{{ $recipe->name }}</td>
+          <td class="author">{{ $recipe->author }}</td>
+          <td class="user">{{ $recipe->user ->name}}</td>
+          <td>
+          @if(in_array('EditRecipe',$allperms) || ( Auth::user() && $recipe->user_id == Auth::user()->id))
+            <a href="{{ route('recipes.edit', $recipe->id) }}" class="btn btn-info pull-left" style="margin-right: 3px;">Edit</a>
+          @endif
+
+          @if(in_array('DeleteRecipe',$allperms) || ( Auth::user() && $recipe->user_id == Auth::user()->id))
+            {!! Form::open(['method' => 'DELETE', 'route' => ['recipes.destroy', $recipe->id] ]) !!}
+            {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
+            {!! Form::close() !!}
+          @endif
+          </td>
+        </tr>
+        @endforeach
+      </tbody>
+    </table>
+  </div>
+
+  @if(in_array('Createrecipe',$allperms))
+    <a href="{{ route('recipes.create') }}" class="btn btn-success">Add recipe</a>
+  @endif
+@endsection
+
+@section('scripts')
+<script>
+  $(document).ready(function(){
+    $(".filter").on("keyup", function() {
+      var name = $("#nameFilter").val().toLowerCase();
+      var author = $("#authorFilter").val().toLowerCase();
+      var user = $("#userFilter").val().toLowerCase();
+      $("#filteredTable tr.rows").filter(function() {
+        $matchname=$(".name",this).text().toLowerCase().indexOf(name) > -1;
+        $matchauthor=$(".author",this).text().toLowerCase().indexOf(author) > -1;
+        $matchuser=$(".user",this).text().toLowerCase().indexOf(user) > -1;
+        $(this).toggle($matchname && $matchauthor && $matchuser);
+      });
+    });
+  });
+</script>
+@endsection
diff --git a/routes/web.php b/routes/web.php
index 5d24e71..6b601df 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -37,4 +37,5 @@ Route::group(['middleware' => 'permissions:SHARE'], function(){
 
     Route::resource('users','UserController');
     Route::resource('roles','RoleController');
+    Route::resource('recipes','RecipeController');
 });