Browse Source

grainbills refactor mkI

master
jk 3 years ago
parent
commit
1532035724
  1. 47
      app/Http/Controllers/GrainbillController.php
  2. 60
      resources/views/grainbills.blade.php
  3. 40
      resources/views/grainbills/create.blade.php
  4. 39
      resources/views/grainbills/edit.blade.php
  5. 50
      resources/views/grainbills/index.blade.php
  6. 4
      resources/views/hops/create.blade.php
  7. 3
      routes/web.php

47
app/Http/Controllers/GrainbillController.php

@ -4,6 +4,8 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Grainbill;
use App\Models\Summarie;
use App\Models\Grain;
class GrainbillController extends Controller
{
@ -14,8 +16,15 @@ class GrainbillController extends Controller
public function index()
{
$project = Grainbill::get();
return view('grainbills')->with('grainbills', $project);
$grainbills = Grainbill::get();
foreach ($grainbills as $grainbill)
{
$beername = Summarie::where('beer_id', $grainbill->beer_id)->value('name');
$grainname = Grain::where('id', $grainbill->grain_id)->value('name');
$grainbill['beer'] = $beername;
$grainbill['name'] = $grainname;
}
return view('grainbills.index')->with('grainbills', $grainbills);
}
@ -28,10 +37,9 @@ class GrainbillController extends Controller
'grain_bill' => 'required',
]);
// create project
$grain = new Grainbill;
$lastID = Grainbill::orderBy('id','desc')->value('id');
$lastID = Grainbill::orderBy('id','desc')->take(1)->value('id');
$grain->id = number_format($lastID) + 1;
$grain->beer_id = $request->input('beer_id');
$grain->grain_id = $request->input('grain_id');
@ -41,6 +49,37 @@ class GrainbillController extends Controller
return redirect('/grainbills')->with('success', 'Grain Bill Added');
}
public function edit($id){
$grainbill = Grainbill::where('id', (int)$id)->first();
return view('grainbills.edit')->with('grainbill', $grainbill);
}
public function update(Request $request, $id){
// validation
$this->validate($request,[
'name' => 'required',
]);
$grain = Grainbill::where('id', (int)$id)->first();
$grain->name = $request->input('name');
$grain->beer_id = $request->input('beer_id');
$grain->grain_id = $request->input('grain_id');
$grain->amount = $request->input('amount');
$grain->grain_bill = $request->input('grain_bill');
$grain->save();
return redirect('/grainbills')->with('success', 'Grain Updated!');
}
public function create()
{
return view('grainbills.create');
}
public function destroy($id)
{
$grainbill = Grainbill::find($id);
$grainbill->delete();
return redirect('/grainbills')->with('success', 'Grain deleted!');
}
}

60
resources/views/grainbills.blade.php

@ -1,64 +1,8 @@
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
@include('inc.navbar')
<div class="card-header"><a href="/home">Home</a> -> {{ __('Grain Bills') }}<span class="backlink"><a href="{{url()->previous()}}">Go Back</a></span></div>
<div class="card-body">
@if (Auth::user()->isAdmin())
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#addModal" type="button" name="button" >
Add Adjunct
</button>
<hr />
@endif
<!-- This piece of code is for error messages display purposes -->
@include('inc.messages')
</div>
</div>
</div>
</div>
</div>
<!-- this code if for the modal -->
<div class="modal fade bg-primary text-dark" tabindex="-1" role="dialog" id="addModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add Grain Bill</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close" >
<span aria-hidden="true">x</span>
</button>
</div>
<div class="modal-body">
<form class="" action="{{route('grainbills.store')}}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="">Beer ID</label>
<input type="text" class="form-control" name="beer_id">
</div>
<div class="form-group">
<label for="">Grain ID</label>
<input type="text" class="form-control" name="grain_id" >
</div>
<div class="form-group">
<label for="">Amount</label>
<input type="text" class="form-control" name="amount">
</div>
<div class="form-group">
<label for="">% of Total Grain Bill</label>
<input type="text" class="form-control" name="grain_bill">
</div>
<input type="submit" name="submit" value="Submit" class="btn btn-success">
</form>
</div>
</div>
</div>
<div class="container">
@yield('main')
</div>
@endsection

40
resources/views/grainbills/create.blade.php

@ -0,0 +1,40 @@
@extends('grainbills')
@section('main')
<div class="row">
<div class="col-sm-8 offset-sm-2">
<h1 class="display-3">Add a Grain</h1>
<div>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div><br />
@endif
<form method="post" action="{{ route('grainbills.store') }}">
@csrf
<div class="form-group">
<label for="beer_id">Beer ID</label>
<input type="text" class="form-control" name="beer_id">
</div>
<div class="form-group">
<label for="grain_id">Grain ID</label>
<input type="text" class="form-control" name="grain_id" >
</div>
<div class="form-group">
<label for="amount">Amount</label>
<input type="text" class="form-control" name="amount">
</div>
<div class="form-group">
<label for="grain_bill">% of Total Grain Bill</label>
<input type="text" class="form-control" name="grain_bill">
</div>
<button type="submit" class="btn btn-primary">Add Grain</button>
</form>
</div>
</div>
</div>
@endsection

39
resources/views/grainbills/edit.blade.php

@ -0,0 +1,39 @@
@extends('grainbills')
@section('main') <div class="row">
<div class="col-sm-8 offset-sm-2">
<h1 class="display-3">Update Grain Bill</h1>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
<br />
@endif
<form method="post" action="{{ route('grainbills.update', $grainbill->id) }}">
@method('PATCH')
@csrf
<div class="form-group">
<label for="beer_id">Beer ID</label>
<input type="text" class="form-control" name="beer_id" value="{{$grainbill->beer_id}}" />
</div>
<div class="form-group">
<label for="grain_id">Grain ID</label>
<input type="text" class="form-control" name="grain_id" value="{{$grainbill->grain_id}}" />
</div>
<div class="form-group">
<label for="amount">Amount</label>
<input type="text" class="form-control" name="amount" value="{{$grainbill->amount}}" />
</div>
<div class="form-group">
<label for="grain_bill">% of Total Grain Bill</label>
<input type="text" class="form-control" name="grain_bill" value="{{$grainbill->grain_bill}}" />
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
</div>
@endsection

50
resources/views/grainbills/index.blade.php

@ -0,0 +1,50 @@
@extends('grainbills')
@section('main)
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
@include('inc.navbar')
<div class="card-header"><a href="/home">Home</a> -> {{ __('Grain Bills') }}<span class="backlink"><a href="{{url()->previous()}}">Go Back</a></span></div>
<div class="card-body">
@if (Auth::user()->isAdmin())
<a href="{{ route('grainbills.create') }}" class="btn btn-primary">Add Grain</a>
<hr />
@endif
<table class="table table-striped table-dark" id="table-programs">
<!-- This piece of code is for error messages display purposes -->
@include('inc.messages')
<thead>
<tr>
<th scope="col">Grain</th>
<th scope="col">Beer</th>
<th scope="col">Amount</th>
<th scope="col">% of Grain Bill</th>
@if (Auth::user()->isAdmin())<th colspan="2" scope="col">Admin</th>@endif
<tbody>
@foreach ($grainbills as $grainbill)
<tr>
<td><a href="/grain/{{$grainbill->grain_id}}">{{$grainbill->name}}</a> </td>
<td><a href="/beer/{{$grainbill->beer_id}}">{{$grainbill->beer}}</a> </td>
<td>{{$grainbill->amount}} </td>
<td>{{$grainbill->grain_bill}} </td>
@if (Auth::user()->isAdmin())
<td><a href="{{ route('grainbills.edit',$grainbill->id)}}" class="btn btn-primary">Edit</a></td>
<td><form action="{{ route('grainbills.destroy', $grainbill->id)}}" method="post">
@csrf
@method('DELETE')
<button class="btn btn-danger" type="submit">Delete</button>
</form></td>
@endif
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
@endsection

4
resources/views/hops/create.blade.php

@ -3,7 +3,7 @@
@section('main')
<div class="row">
<div class="col-sm-8 offset-sm-2">
<h1 class="display-3">Add a Grain</h1>
<h1 class="display-3">Add a Hop</h1>
<div>
@if ($errors->any())
<div class="alert alert-danger">
@ -64,7 +64,7 @@
<label for="">Caryophyllene</label>
<input type="text" class="form-control" name="caryophyllene">
</div>
<button type="submit" class="btn btn-primary">Add Grain</button>
<button type="submit" class="btn btn-primary">Add Hop</button>
</form>
</div>
</div>

3
routes/web.php

@ -28,9 +28,9 @@ Route::resource('grains', 'App\Http\Controllers\GrainController');
Route::resource('hops', 'App\Http\Controllers\HopController');
Route::resource('yeasts', 'App\Http\Controllers\YeastController');
Route::resource('adjuncts', 'App\Http\Controllers\AdjunctController');
Route::resource('grainbills', 'App\Http\Controllers\GrainbillController');
Route::get('/home', [App\Http\Controllers\SummarieController::class, 'index'])->name('home');
Route::get('/fermentations', [App\Http\Controllers\FermentationController::class, 'index'])->name('fermentations');
Route::get('/grainbills', [App\Http\Controllers\GrainbillController::class, 'index'])->name('grainbills');
Route::get('/mashes', [App\Http\Controllers\MasheController::class, 'index'])->name('mashes');
Route::get('/hopadditions', [App\Http\Controllers\HopAdditionController::class, 'index'])->name('hopadditions');
Route::get('/adjunctadditions', [App\Http\Controllers\AdjunctAdditionController::class, 'index'])->name('adjunctadditions');
@ -40,7 +40,6 @@ Route::get('/grain/{grainID}', [App\Http\Controllers\GraincardController::class,
Route::get('/yeast/{yeastID}', [App\Http\Controllers\YeastcardController::class, 'index'])->name('yeast');
Route::get('/hop/{hopID}', [App\Http\Controllers\HopcardController::class, 'index'])->name('hop');
Route::get('/adjunct/{adjunctID}', [App\Http\Controllers\AdjunctcardController::class, 'index'])->name('adjunct');
Route::post('/grainbills', [App\Http\Controllers\GrainbillController::class, 'store'])->name('grainbills.store');
Route::post('/mashes', [App\Http\Controllers\MasheController::class, 'store'])->name('mash.store');
Route::post('/hopadditions', [App\Http\Controllers\HopAdditionController::class, 'store'])->name('hopadditions.store');
Route::post('/adjunctadditions', [App\Http\Controllers\AdjunctAdditionController::class, 'store'])->name('adjunctadditions.store');

Loading…
Cancel
Save