Example #1
0
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $color = Color::find($id);
     $color->name = $request->name;
     $color->desc = $request->desc;
     $color->color_code = $request->color_code;
     $color->save();
     return $color;
 }
 /**
  * Get beers by their color values.  Can pass either start SRM value, start and end SRM values, or the color ID.
  *
  * @param Request $request
  * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  */
 public function beers(Request $request)
 {
     $start = $request->get('start');
     $end = $request->get('end');
     $colorId = $request->get('color_id');
     if (!empty($start) && !empty($end)) {
         $beers = Beer::where('srm', '>=', $start)->where('srm', '<=', $end)->get();
         return response(['status' => 'ok', 'message' => 'Beers that fall in the SRM range of ' . $start . ' and ' . $end, 'beers' => $beers]);
     } else {
         if (!empty($start)) {
             $beers = Beer::where('srm', '=', $start)->get();
             return response(['status' => 'ok', 'message' => 'Beers that have the SRM value of ' . $start, 'beers' => $beers]);
         } else {
             if (!empty($colorId)) {
                 $color = Color::find($colorId);
                 if (!!$color) {
                     $beers = Beer::where('srm', '>=', $color->start)->where('srm', '<=', $color->end)->get();
                     return response(['status' => 'ok', 'message' => 'Beers that are of the color ' . $color->name . ' (id: ' . $colorId . ')', 'beers' => $beers]);
                 }
             }
         }
     }
     return response(['status' => 'failed', 'message' => 'Invalid data received.  You must provide the range of SRM values with the name start and end,' . 'or a color ID must be provided.  To get a list of colors including their ID, name, start and end range ' . 'perform a GET request on the color index route.', 'errors' => 'Bad values received'], 400);
 }