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.

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