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.

134 lines
3.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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. $beer->save();
  60. return redirect('/beers')->with('success', 'Beer Added');
  61. }
  62. public function edit($id){
  63. $beer = Summarie::where('beer_id', (int)$id)->first();
  64. return view('beers.edit')->with('beer', $beer);
  65. }
  66. public function update(Request $request, $id)
  67. {
  68. $request->validate([
  69. 'name' => 'required',
  70. 'type' => 'required',
  71. 'abv' => 'required',
  72. 'fg' => 'required',
  73. 'keywords' => 'required',
  74. 'og' => 'required',
  75. 'source' => 'required',
  76. 'batch' => 'required',
  77. 'brewery_id' => 'required'
  78. ]);
  79. $beer = Summarie::where('beer_id', (int)$id)->first();
  80. $beer->name = $request->input('name');
  81. $beer->type = $request->input('type');
  82. $beer->abv = $request->input('abv');
  83. $beer->fg = $request->input('fg');
  84. $beer->keywords = '{'.$request->input('keywords').'}';
  85. if ($request->mine)
  86. {
  87. $beer->mine = true;
  88. } else {
  89. $beer->mine = false;
  90. }
  91. $beer->og = $request->input('og');
  92. $beer->source = $request->input('source');
  93. $beer->batch = $request->input('batch');
  94. $beer->brewery_id = $request->input('brewery_id');
  95. if ($request->input('notes'))
  96. {
  97. $beer->notes = $request->input('notes');
  98. }
  99. $beer->save();
  100. return redirect('/beers')->with('success', 'Beer updated!');
  101. }
  102. public function create()
  103. {
  104. $lastID = Summarie::orderBy('beer_id','desc')->take(1)->value('beer_id');
  105. $beer_id = number_format($lastID) + 1;
  106. $breweries = Brewerie::orderBy('name')->get();
  107. $data = array('beer_id'=>$beer_id, 'breweries'=>$breweries);
  108. return view('beers.create')->with($data);
  109. }
  110. public function destroy($id)
  111. {
  112. $beer = Summarie::find($id);
  113. $beer->delete();
  114. return redirect('/beers')->with('success', 'Beer deleted!');
  115. }
  116. }