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.

177 lines
5.1 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('ibu'))
  64. {
  65. $beer->ibu = $request->input('ibu');
  66. }
  67. if ($request->input('ebc'))
  68. {
  69. $beer->ebc = $request->input('ebc');
  70. }
  71. if ($request->input('srm'))
  72. {
  73. $beer->srm = $request->input('srm');
  74. }
  75. if ($request->input('ph'))
  76. {
  77. $beer->ph = $request->input('ph');
  78. }
  79. $beer->save();
  80. return redirect('/beers')->with('success', 'Beer Added');
  81. }
  82. public function edit($id){
  83. $beer = Summarie::where('beer_id', (int)$id)->first();
  84. $breweries = Brewerie::orderBy('name')->get();
  85. $selected_brewery = $beer->brewery_id;
  86. $data = array('beer'=>$beer, 'breweries'=>$breweries, 'selected_brewery'=>$selected_brewery);
  87. return view('beers.edit')->with($data);
  88. }
  89. public function update(Request $request, $id)
  90. {
  91. $request->validate([
  92. 'name' => 'required',
  93. 'type' => 'required',
  94. 'abv' => 'required',
  95. 'fg' => 'required',
  96. 'keywords' => 'required',
  97. 'og' => 'required',
  98. 'source' => 'required',
  99. 'batch' => 'required',
  100. 'brewery_id' => 'required'
  101. ]);
  102. $beer = Summarie::where('beer_id', (int)$id)->first();
  103. $beer->name = $request->input('name');
  104. $beer->type = $request->input('type');
  105. $beer->abv = $request->input('abv');
  106. $beer->fg = $request->input('fg');
  107. $beer->keywords = '{'.$request->input('keywords').'}';
  108. if ($request->mine)
  109. {
  110. $beer->mine = true;
  111. } else {
  112. $beer->mine = false;
  113. }
  114. $beer->og = $request->input('og');
  115. $beer->source = $request->input('source');
  116. $beer->batch = $request->input('batch');
  117. $beer->brewery_id = $request->input('brewery_id');
  118. if ($request->input('notes'))
  119. {
  120. $beer->notes = $request->input('notes');
  121. }
  122. if ($request->input('boil_volume'))
  123. {
  124. $beer->boil_volume = $request->input('boil_volume');
  125. }
  126. if ($request->input('ibu'))
  127. {
  128. $beer->ibu = $request->input('ibu');
  129. }
  130. if ($request->input('ebc'))
  131. {
  132. $beer->ebc = $request->input('ebc');
  133. }
  134. if ($request->input('srm'))
  135. {
  136. $beer->srm = $request->input('srm');
  137. }
  138. if ($request->input('ph'))
  139. {
  140. $beer->ph = $request->input('ph');
  141. }
  142. $beer->save();
  143. return redirect('/beers')->with('success', 'Beer updated!');
  144. }
  145. public function create()
  146. {
  147. $lastID = Summarie::orderBy('beer_id','desc')->take(1)->value('beer_id');
  148. $beer_id = number_format($lastID) + 1;
  149. $breweries = Brewerie::orderBy('name')->get();
  150. $data = array('beer_id'=>$beer_id, 'breweries'=>$breweries);
  151. return view('beers.create')->with($data);
  152. }
  153. public function destroy($id)
  154. {
  155. $beer = Summarie::find($id);
  156. $beer->delete();
  157. return redirect('/beers')->with('success', 'Beer deleted!');
  158. }
  159. }