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.

166 lines
4.7 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. return view('beers.edit')->with('beer', $beer);
  81. }
  82. public function update(Request $request, $id)
  83. {
  84. $request->validate([
  85. 'name' => 'required',
  86. 'type' => 'required',
  87. 'abv' => 'required',
  88. 'fg' => 'required',
  89. 'keywords' => 'required',
  90. 'og' => 'required',
  91. 'source' => 'required',
  92. 'batch' => 'required',
  93. 'brewery_id' => 'required'
  94. ]);
  95. $beer = Summarie::where('beer_id', (int)$id)->first();
  96. $beer->name = $request->input('name');
  97. $beer->type = $request->input('type');
  98. $beer->abv = $request->input('abv');
  99. $beer->fg = $request->input('fg');
  100. $beer->keywords = '{'.$request->input('keywords').'}';
  101. if ($request->mine)
  102. {
  103. $beer->mine = true;
  104. } else {
  105. $beer->mine = false;
  106. }
  107. $beer->og = $request->input('og');
  108. $beer->source = $request->input('source');
  109. $beer->batch = $request->input('batch');
  110. $beer->brewery_id = $request->input('brewery_id');
  111. if ($request->input('notes'))
  112. {
  113. $beer->notes = $request->input('notes');
  114. }
  115. if ($request->input('boil_volume'))
  116. {
  117. $beer->boil_volume = $request->input('boil_volume');
  118. }
  119. if ($request->input('ebc'))
  120. {
  121. $beer->ebc = $request->input('ebc');
  122. }
  123. if ($request->input('srm'))
  124. {
  125. $beer->srm = $request->input('srm');
  126. }
  127. if ($request->input('ph'))
  128. {
  129. $beer->ph = $request->input('ph');
  130. }
  131. $beer->save();
  132. return redirect('/beers')->with('success', 'Beer updated!');
  133. }
  134. public function create()
  135. {
  136. $lastID = Summarie::orderBy('beer_id','desc')->take(1)->value('beer_id');
  137. $beer_id = number_format($lastID) + 1;
  138. $brewery_list = Brewerie::orderBy('name')->pluck('name', 'id');
  139. $data = array('beer_id'=>$beer_id, 'brewery_list'=>$brewery_list);
  140. return view('beers.create')->with($data);
  141. }
  142. public function destroy($id)
  143. {
  144. $beer = Summarie::find($id);
  145. $beer->delete();
  146. return redirect('/beers')->with('success', 'Beer deleted!');
  147. }
  148. }