Browse Source

grains refactor mkI

master
jk 3 years ago
parent
commit
bc2a10ddd4
  1. 7
      app/Http/Controllers/BrewerieController.php
  2. 96
      app/Http/Controllers/GrainController.php
  3. 2
      resources/views/beers/create.blade.php
  4. 56
      resources/views/grains/create.blade.php
  5. 55
      resources/views/grains/edit.blade.php
  6. 40
      resources/views/grains/index.blade.php
  7. 7
      routes/web.php

7
app/Http/Controllers/BrewerieController.php

@ -27,7 +27,7 @@ class BrewerieController extends Controller
// create project // create project
$brewerie = new Brewerie; $brewerie = new Brewerie;
$lastID = Brewerie::orderBy('id','desc')->value('id');
$lastID = Brewerie::orderBy('id','desc')->take(1)->value('id');
$brewerie->id = number_format($lastID) + 1; $brewerie->id = number_format($lastID) + 1;
$brewerie->name = $request->input('name'); $brewerie->name = $request->input('name');
if ($request->input('place')) if ($request->input('place'))
@ -44,8 +44,8 @@ class BrewerieController extends Controller
} }
public function edit($id){ public function edit($id){
$brewery = Brewerie::where('id', (int)$id)->first();
return view('breweries.edit')->with('brewery', $brewery);
$brewery = Brewerie::where('id', (int)$id)->first();
return view('breweries.edit')->with('brewery', $brewery);
} }
public function update(Request $request, $id) public function update(Request $request, $id)
@ -77,4 +77,3 @@ class BrewerieController extends Controller
} }
} }

96
app/Http/Controllers/GrainController.php

@ -16,7 +16,7 @@ class GrainController extends Controller
public function index() public function index()
{ {
$grains = Grain::orderBy('name')->get(); $grains = Grain::orderBy('name')->get();
return view('grains')->with('grains', $grains);
return view('grains.index')->with('grains', $grains);
} }
public function store(Request $request){ public function store(Request $request){
@ -25,45 +25,101 @@ class GrainController extends Controller
'name' => 'required', 'name' => 'required',
]); ]);
// create project // create project
$grain = new Grain; $grain = new Grain;
$lastID = Grain::orderBy('id','desc')->value('id');
$grain->id = number_format($lastID) + 1;
$lastID = Grain::orderBy('id','desc')->take(1)->value('id');
$grain->id = number_format($lastID) + 1;
$grain->name = $request->input('name'); $grain->name = $request->input('name');
if ($request->input('flavour')) if ($request->input('flavour'))
{
$grain->flavour = $request->input('flavour');
}
{
$grain->flavour = $request->input('flavour');
}
if ($request->input('lintner')) if ($request->input('lintner'))
{ {
$grain->lintner = $request->input('lintner');
}
$grain->lintner = $request->input('lintner');
}
if ($request->input('winkol')) if ($request->input('winkol'))
{ {
$grain->winkol = $request->input('winkol');
}
$grain->winkol = $request->input('winkol');
}
if ($request->input('lovibond')) if ($request->input('lovibond'))
{ {
$grain->lovibond = $request->input('lovibond');
}
$grain->lovibond = $request->input('lovibond');
}
if ($request->input('colour')) if ($request->input('colour'))
{ {
$grain->colour = $request->input('colour');
}
$grain->colour = $request->input('colour');
}
if ($request->input('alternative')) if ($request->input('alternative'))
{ {
$altArr = array_map('intval',explode(',',$request->input('alternative')));
$grain->alternative = $altArr;
}
$altArr = array_map('intval',explode(',',$request->input('alternative')));
$grain->alternative = $altArr;
}
if ($request->input('ebc')) if ($request->input('ebc'))
{ {
$grain->ebc = $request->input('ebc');
$grain->ebc = $request->input('ebc');
} }
$grain->save();
$grain->save();
return redirect('/grains')->with('success', 'Grain Added'); return redirect('/grains')->with('success', 'Grain Added');
} }
public function edit($id){
$brewery = Brewerie::where('id', (int)$id)->first();
return view('breweries.edit')->with('brewery', $brewery);
}
public function update(Request $request, $id){
// validation
$this->validate($request,[
'name' => 'required',
]);
$grain = Grain::where('id', $id)->first();
$grain->name = $request->input('name');
if ($request->input('flavour'))
{
$grain->flavour = $request->input('flavour');
}
if ($request->input('lintner'))
{
$grain->lintner = $request->input('lintner');
}
if ($request->input('winkol'))
{
$grain->winkol = $request->input('winkol');
}
if ($request->input('lovibond'))
{
$grain->lovibond = $request->input('lovibond');
}
if ($request->input('colour'))
{
$grain->colour = $request->input('colour');
}
if ($request->input('alternative'))
{
$altArr = array_map('intval',explode(',',$request->input('alternative')));
$grain->alternative = $altArr;
}
if ($request->input('ebc'))
{
$grain->ebc = $request->input('ebc');
}
$grain->save();
return redirect('/grains')->with('success', 'Grain Updated!');
}
public function create()
{
return view('grains.create');
}
public function destroy($id)
{
$grain = Grain::find($id);
$grain->delete();
return redirect('/grains')->with('success', 'Grain deleted!');
}
} }

2
resources/views/beers/create.blade.php

@ -28,7 +28,7 @@
<label for="keywords">Keywords</label> <label for="keywords">Keywords</label>
<textarea type="text" class="form-control" name="keywords" ></textarea> <textarea type="text" class="form-control" name="keywords" ></textarea>
</div> </div>
<div class="Type">
<div class="form-group">
<label for="type">Style</label> <label for="type">Style</label>
<input type="text" class="form-control" name="type"> <input type="text" class="form-control" name="type">
</div> </div>

56
resources/views/grains/create.blade.php

@ -0,0 +1,56 @@
@extends('grains')
@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('grains.store') }}">
@csrf
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label for="flavour">Flavour</label>
<textarea type="text" class="form-control" name="flavour" ></textarea>
</div>
<div class="form-group">
<label for="colour">Colour</label>
<input type="text" class="form-control" name="colour">
</div>
<div class="form-group">
<label for="lintner">Lintner</label>
<input type="text" class="form-control" name="lintner">
</div>
<div class="form-group">
<label for="winkol">Windisch-Kolbach</label>
<input type="text" class="form-control" name="winkol">
</div>
<div class="form-group">
<label for="lovibond">Lovibond</label>
<input type="text" class="form-control" name="lovibond">
</div>
<div class="form-group">
<label for="ebc">EBC</label>
<input type="text" class="form-control" name="ebc">
</div>
<div class="form-group">
<label for="alternative">Alternative(s)</label>
<inpu type="text" class="form-control" name="alternative">
</div>
<button type="submit" class="btn btn-primary">Add Grain</button>
</form>
</div>
</div>
</div>
@endsection

55
resources/views/grains/edit.blade.php

@ -0,0 +1,55 @@
@extends('grains')
@section('main') <div class="row">
<div class="col-sm-8 offset-sm-2">
<h1 class="display-3">Update Grain</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('grains.update', $grain->id) }}">
@method('PATCH')
@csrf
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" value="{{$grain->name}}"/>
</div>
<div class="form-group">
<label for="flavour">Flavour</label>
<textarea type="text" class="form-control" name="flavour" />{{$grain->flavour}}</textarea>
</div>
<div class="form-group">
<label for="colour">Colour</label>
<input type="text" class="form-control" name="colour" value="{{$grain->colour}}"/>
</div>
<div class="form-group">
<label for="lintner">Lintner</label>
<input type="text" class="form-control" name="lintner" value="{{$grain->lintner}}"/>
</div>
<div class="form-group">
<label for="winkol">Windisch-Kolbach</label>
<input type="text" class="form-control" name="winkol" value="{{$grain->winkol}}"/>
</div>
<div class="form-group">
<label for="lovibond">Lovibond</label>
<input type="text" class="form-control" name="lovibond" value="{{$grain->lovibond}}"/>
</div>
<div class="form-group">
<label for="ebc">EBC</label>
<input type="text" class="form-control" name="ebc" value="{{$grain->ebc}}"/>
</div>
<div class="form-group">
<label for="alternative">Alternative(s)</label>
<inpu type="text" class="form-control" name="alternative" value="{{str_replace(' ,',',',str_replace('"',' ',str_replace(',',', ',str_replace('}', '', str_replace('{','',$grain->alternative)))))}}"/>
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
</div>
@endsection

40
resources/views/grains/index.blade.php

@ -0,0 +1,40 @@
@extends('grains')
@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 List') }}<span class="backlink"><a href="{{url()->previous()}}">Go Back</a></span></div>
<div class="card-body">
@if (Auth::user()->isAdmin())
<a href="{{ route('grains.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')
<tbody>
@foreach ($grains as $grain)
<tr>
<td><a href="/grain/{{$grain->id}}">{{$grain->name}}</a> </td>
@if (Auth::user()->isAdmin())
<td><a href="{{ route('grains.edit',$grain->id)}}" class="btn btn-primary">Edit</a></td>
<td><form action="{{ route('grains.destroy', $grain->grain_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>

7
routes/web.php

@ -24,12 +24,12 @@ Route::any('/register', function() {
Route::resource('breweries', 'App\Http\Controllers\BrewerieController'); Route::resource('breweries', 'App\Http\Controllers\BrewerieController');
Route::resource('beers', 'App\Http\Controllers\SummarieController'); Route::resource('beers', 'App\Http\Controllers\SummarieController');
Route::resource('grains', 'App\Http\Controllers\GrainController');
Route::get('/home', [App\Http\Controllers\SummarieController::class, 'index'])->name('home'); Route::get('/home', [App\Http\Controllers\SummarieController::class, 'index'])->name('home');
Route::get('/adjuncts', [App\Http\Controllers\AdjunctController::class, 'index'])->name('adjuncts'); Route::get('/adjuncts', [App\Http\Controllers\AdjunctController::class, 'index'])->name('adjuncts');
Route::get('/grains', [App\Http\Controllers\GrainController::class, 'index'])->name('grains');
//Route::get('/grains', [App\Http\Controllers\GrainController::class, 'index'])->name('grains');
Route::get('/hops', [App\Http\Controllers\HopController::class, 'index'])->name('hops'); Route::get('/hops', [App\Http\Controllers\HopController::class, 'index'])->name('hops');
Route::get('/yeasts', [App\Http\Controllers\YeastController::class, 'index'])->name('yeasts'); Route::get('/yeasts', [App\Http\Controllers\YeastController::class, 'index'])->name('yeasts');
//Route::get('/beers', [App\Http\Controllers\SummarieController::class, 'index'])->name('beers');
Route::get('/fermentations', [App\Http\Controllers\FermentationController::class, 'index'])->name('fermentations'); Route::get('/fermentations', [App\Http\Controllers\FermentationController::class, 'index'])->name('fermentations');
Route::get('/grainbills', [App\Http\Controllers\GrainbillController::class, 'index'])->name('grainbills'); Route::get('/grainbills', [App\Http\Controllers\GrainbillController::class, 'index'])->name('grainbills');
Route::get('/mashes', [App\Http\Controllers\MasheController::class, 'index'])->name('mashes'); Route::get('/mashes', [App\Http\Controllers\MasheController::class, 'index'])->name('mashes');
@ -41,11 +41,10 @@ Route::get('/grain/{grainID}', [App\Http\Controllers\GraincardController::class,
Route::get('/yeast/{yeastID}', [App\Http\Controllers\YeastcardController::class, 'index'])->name('yeast'); 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('/hop/{hopID}', [App\Http\Controllers\HopcardController::class, 'index'])->name('hop');
Route::get('/adjunct/{adjunctID}', [App\Http\Controllers\AdjunctcardController::class, 'index'])->name('adjunct'); Route::get('/adjunct/{adjunctID}', [App\Http\Controllers\AdjunctcardController::class, 'index'])->name('adjunct');
Route::post('/grains', [App\Http\Controllers\GrainController::class, 'store'])->name('grains.store');
//Route::post('/grains', [App\Http\Controllers\GrainController::class, 'store'])->name('grains.store');
Route::post('/hops', [App\Http\Controllers\HopController::class, 'store'])->name('hops.store'); Route::post('/hops', [App\Http\Controllers\HopController::class, 'store'])->name('hops.store');
Route::post('/yeasts', [App\Http\Controllers\YeastController::class, 'store'])->name('yeasts.store'); Route::post('/yeasts', [App\Http\Controllers\YeastController::class, 'store'])->name('yeasts.store');
Route::post('/adjuncts', [App\Http\Controllers\AdjunctController::class, 'store'])->name('adjuncts.store'); Route::post('/adjuncts', [App\Http\Controllers\AdjunctController::class, 'store'])->name('adjuncts.store');
//Route::post('/beers', [App\Http\Controllers\SummarieController::class, 'store'])->name('beers.store');
Route::post('/grainbills', [App\Http\Controllers\GrainbillController::class, 'store'])->name('grainbills.store'); 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('/mashes', [App\Http\Controllers\MasheController::class, 'store'])->name('mash.store');
Route::post('/hopadditions', [App\Http\Controllers\HopAdditionController::class, 'store'])->name('hopadditions.store'); Route::post('/hopadditions', [App\Http\Controllers\HopAdditionController::class, 'store'])->name('hopadditions.store');

Loading…
Cancel
Save