Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.94 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use Symfony\Component\HttpKernel\Exception\HttpException;
  7. use App\Movie;
  8.  
  9. class MovieController extends Controller
  10. {
  11.     public function index()
  12.     {
  13.         $movies = Movie::paginate(10);
  14.  
  15.         if (!$movies) {
  16.             throw new HttpException(400, "Invalid data");
  17.         }
  18.  
  19.         return response()->json(
  20.             $movies,
  21.             200
  22.         );
  23.     }
  24.  
  25.     public function show($id)
  26.     {
  27.         if (!$id) {
  28.             throw new HttpException(400, "Invalid id");
  29.         }
  30.  
  31.         $movie = Movie::find($id);
  32.  
  33.         return response()->json([
  34.             $movie,
  35.         ], 200);
  36.  
  37.     }
  38.  
  39.     public function store(Request $request)
  40.     {
  41.         $movie = new Movie();
  42.         $movie->title = $request->input('title');
  43.         $movie->genre = $request->input('genre');
  44.         $movie->date = $request->input('date');
  45.         $movie->director = $request->input('director');
  46.  
  47.         if ($movie->save()) {
  48.             return $movie;
  49.         }
  50.  
  51.         throw new HttpException(400, "Invalid data");
  52.     }
  53.  
  54.     public function update(Request $request, $id)
  55.     {
  56.         if (!$id) {
  57.             throw new HttpException(400, "Invalid id");
  58.         }
  59.  
  60.         $movie = Movie::find($id);
  61.         $movie->title = $request->input('title');
  62.         $movie->genre = $request->input('genre');
  63.         $movie->date = $request->input('date');
  64.         $movie->director = $request->input('director');
  65.  
  66.         if ($movie->save()) {
  67.             return $movie;
  68.         }
  69.  
  70.         throw new HttpException(400, "Invalid data");
  71.     }
  72.  
  73.     public function destroy($id)
  74.     {
  75.         if (!$id) {
  76.             throw new HttpException(400, "Invalid id");
  77.         }
  78.  
  79.         $movie = Movie::find($id);
  80.         $movie->delete();
  81.  
  82.         return response()->json([
  83.             'message' => 'movie deleted',
  84.         ], 200);
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement