Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use App\Movie;
- class MovieController extends Controller
- {
- public function index()
- {
- $movies = Movie::paginate(10);
- if (!$movies) {
- throw new HttpException(400, "Invalid data");
- }
- return response()->json(
- $movies,
- 200
- );
- }
- public function show($id)
- {
- if (!$id) {
- throw new HttpException(400, "Invalid id");
- }
- $movie = Movie::find($id);
- return response()->json([
- $movie,
- ], 200);
- }
- public function store(Request $request)
- {
- $movie = new Movie();
- $movie->title = $request->input('title');
- $movie->genre = $request->input('genre');
- $movie->date = $request->input('date');
- $movie->director = $request->input('director');
- if ($movie->save()) {
- return $movie;
- }
- throw new HttpException(400, "Invalid data");
- }
- public function update(Request $request, $id)
- {
- if (!$id) {
- throw new HttpException(400, "Invalid id");
- }
- $movie = Movie::find($id);
- $movie->title = $request->input('title');
- $movie->genre = $request->input('genre');
- $movie->date = $request->input('date');
- $movie->director = $request->input('director');
- if ($movie->save()) {
- return $movie;
- }
- throw new HttpException(400, "Invalid data");
- }
- public function destroy($id)
- {
- if (!$id) {
- throw new HttpException(400, "Invalid id");
- }
- $movie = Movie::find($id);
- $movie->delete();
- return response()->json([
- 'message' => 'movie deleted',
- ], 200);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement