My beer compendium
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Summarie;
  5. use App\Models\Brewerie;
  6. use Illuminate\Support\Facades\DB;
  7. class SummarieController extends Controller
  8. {
  9. public function __construct()
  10. {
  11. $this->middleware('auth');
  12. }
  13. public function index()
  14. {
  15. $beers = Summarie::orderBy('name')->get();
  16. foreach ($beers as $beer)
  17. {
  18. $brewery = DB::table('breweries')->where('id', $beer->brewery_id)->value('name');
  19. $beer['brewery'] = $brewery;
  20. }
  21. return view('beers.index')->with('beers', $beers);
  22. }
  23. public function store(Request $request){
  24. // validation
  25. $this->validate($request,[
  26. 'name' => 'required',
  27. 'type' => 'required',
  28. 'abv' => 'required',
  29. 'fg' => 'required',
  30. 'keywords' => 'required',
  31. 'og' => 'required',
  32. 'source' => 'required',
  33. 'batch' => 'required',
  34. 'beer_id' => 'required',
  35. 'brewery_id' => 'required'
  36. ]);
  37. // create project
  38. $beer = new Summarie;
  39. $beer->beer_id = $request->input('beer_id');
  40. $beer->name = $request->input('name');
  41. $beer->type = $request->input('type');
  42. $beer->abv = $request->input('abv');
  43. $beer->fg = $request->input('fg');
  44. $beer->keywords = '{'.$request->input('keywords').'}';
  45. if ($request->mine)
  46. {
  47. $beer->mine = true;
  48. } else {
  49. $beer->mine = false;
  50. }
  51. $beer->og = $request->input('og');
  52. $beer->source = $request->input('source');
  53. $beer->batch = $request->input('batch');
  54. $beer->brewery_id = $request->input('brewery_id');
  55. if ($request->input('notes'))
  56. {
  57. $beer->notes = $request->input('notes');
  58. }
  59. if ($request->input('boil_volume'))
  60. {
  61. $beer->boil_volume = $request->input('boil_volume');
  62. }
  63. if ($request->input('ebc'))
  64. {
  65. $beer->ebc = $request->input('ebc');
  66. }
  67. if ($request->input('srm'))
  68. {
  69. $beer->srm = $request->input('srm');
  70. }
  71. if ($request->input('ph'))
  72. {
  73. $beer->ph = $request->input('ph');
  74. }
  75. $beer->save();
  76. return redirect('/beers')->with('success', 'Beer Added');
  77. }
  78. public function edit($id){
  79. $beer = Summarie::where('beer_id', (int)$id)->first();
  80. $breweries = Brewerie::orderBy('name')->get();
  81. $selected_brewery = $beer->brewery_id;
  82. $data = array('beer'=>$beer, 'breweries'=>$breweries, 'selected_brewery'=>$selected_brewery);
  83. return view('beers.edit')->with($data);
  84. }
  85. public function update(Request $request, $id)
  86. {
  87. $request->validate([
  88. 'name' => 'required',
  89. 'type' => 'required',
  90. 'abv' => 'required',
  91. 'fg' => 'required',
  92. 'keywords' => 'required',
  93. 'og' => 'required',
  94. 'source' => 'required',
  95. 'batch' => 'required',
  96. 'brewery_id' => 'required'
  97. ]);
  98. $beer = Summarie::where('beer_id', (int)$id)->first();
  99. $beer->name = $request->input('name');
  100. $beer->type = $request->input('type');
  101. $beer->abv = $request->input('abv');
  102. $beer->fg = $request->input('fg');
  103. $beer->keywords = '{'.$request->input('keywords').'}';
  104. if ($request->mine)
  105. {
  106. $beer->mine = true;
  107. } else {
  108. $beer->mine = false;
  109. }
  110. $beer->og = $request->input('og');
  111. $beer->source = $request->input('source');
  112. $beer->batch = $request->input('batch');
  113. $beer->brewery_id = $request->input('brewery_id');
  114. if ($request->input('notes'))
  115. {
  116. $beer->notes = $request->input('notes');
  117. }
  118. if ($request->input('boil_volume'))
  119. {
  120. $beer->boil_volume = $request->input('boil_volume');
  121. }
  122. if ($request->input('ebc'))
  123. {
  124. $beer->ebc = $request->input('ebc');
  125. }
  126. if ($request->input('srm'))
  127. {
  128. $beer->srm = $request->input('srm');
  129. }
  130. if ($request->input('ph'))
  131. {
  132. $beer->ph = $request->input('ph');
  133. }
  134. $beer->save();
  135. return redirect('/beers')->with('success', 'Beer updated!');
  136. }
  137. public function create()
  138. {
  139. $lastID = Summarie::orderBy('beer_id','desc')->take(1)->value('beer_id');
  140. $beer_id = number_format($lastID) + 1;
  141. $breweries = Brewerie::orderBy('name')->get();
  142. $data = array('beer_id'=>$beer_id, 'breweries'=>$breweries);
  143. return view('beers.create')->with($data);
  144. }
  145. public function destroy($id)
  146. {
  147. $beer = Summarie::find($id);
  148. $beer->delete();
  149. return redirect('/beers')->with('success', 'Beer deleted!');
  150. }
  151. }