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

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 Illuminate\Support\Facades\DB;
  6. class SummarieController extends Controller
  7. {
  8. public function __construct()
  9. {
  10. $this->middleware('auth');
  11. }
  12. public function index()
  13. {
  14. $beers = Summarie::orderBy('name')->get();
  15. foreach ($beers as $beer)
  16. {
  17. $brewery = DB::table('breweries')->where('id', $beer->beer_id)->value('name');
  18. $beer['brewery'] = $brewery;
  19. }
  20. return view('beers.index')->with('beers', $beers);
  21. }
  22. public function store(Request $request){
  23. // validation
  24. $this->validate($request,[
  25. 'name' => 'required',
  26. 'type' => 'required',
  27. 'abv' => 'required',
  28. 'fg' => 'required',
  29. 'keywords' => 'required',
  30. 'og' => 'required',
  31. 'source' => 'required',
  32. 'batch' => 'required',
  33. 'beer_id' => 'required',
  34. 'brewery_id' => 'required'
  35. ]);
  36. // create project
  37. $beer = new Summarie;
  38. $beer->beer_id = $request->input('beer_id');
  39. $beer->name = $request->input('name');
  40. $beer->type = $request->input('type');
  41. $beer->abv = $request->input('abv');
  42. $beer->fg = $request->input('fg');
  43. $beer->keywords = '{'.$request->input('keywords').'}';
  44. if ($request->mine)
  45. {
  46. $beer->mine = true;
  47. } else {
  48. $beer->mine = false;
  49. }
  50. $beer->og = $request->input('og');
  51. $beer->source = $request->input('source');
  52. $beer->batch = $request->input('batch');
  53. $beer->beer_id = $request->input('beer_id');
  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', $id)->first();
  80. $beer->beer_id=$id;
  81. $beer->name = $request->input('name');
  82. $beer->type = $request->input('type');
  83. $beer->abv = $request->input('abv');
  84. $beer->fg = $request->input('fg');
  85. $beer->keywords = '{'.$request->input('keywords').'}';
  86. if ($request->mine)
  87. {
  88. $beer->mine = true;
  89. } else {
  90. $beer->mine = false;
  91. }
  92. $beer->og = $request->input('og');
  93. $beer->source = $request->input('source');
  94. $beer->batch = $request->input('batch');
  95. $beer->beer_id = $request->input('beer_id');
  96. $beer->brewery_id = $request->input('beer_id');
  97. if ($request->input('notes'))
  98. {
  99. $beer->notes = $request->input('notes');
  100. }
  101. $beer->save();
  102. return redirect('/beers')->with('success', 'Beer updated!');
  103. }
  104. public function create()
  105. {
  106. $lastID = Summarie::orderBy('beer_id','desc')->take(1)->value('beer_id');
  107. $beer_id = number_format($lastID) + 1;
  108. return view('beers.create')->with('beer_id', $beer_id);
  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. }