|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use App\Http\Requests; |
| 7 | +use App\Post; |
| 8 | +use App\Http\Resources\Post as PostResource; |
| 9 | + |
| 10 | +class APIPostsController extends Controller |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Display a listing of the resource. |
| 14 | + * |
| 15 | + * @return \Illuminate\Http\Response |
| 16 | + */ |
| 17 | + public function index() |
| 18 | + { |
| 19 | + $posts = Post::paginate(15); |
| 20 | + return PostResource::collection($posts); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Show the form for creating a new resource. |
| 25 | + * |
| 26 | + * @return \Illuminate\Http\Response |
| 27 | + */ |
| 28 | + public function create() |
| 29 | + { |
| 30 | + // |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Store a newly created resource in storage. |
| 35 | + * |
| 36 | + * @param \Illuminate\Http\Request $request |
| 37 | + * @return \Illuminate\Http\Response |
| 38 | + */ |
| 39 | + public function store(Request $request) |
| 40 | + { |
| 41 | + $post = $request->isMethod('put') ? Post::findOrFail |
| 42 | + ($request->post_id): new Post; |
| 43 | + |
| 44 | + $post->id= $request->input('post_id'); |
| 45 | + $post->title= $request->input('title'); |
| 46 | + $post->body= $request->input('body'); |
| 47 | + |
| 48 | + if($post->save()){ |
| 49 | + return new PostResource($post); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Display the specified resource. |
| 55 | + * |
| 56 | + * @param int $id |
| 57 | + * @return \Illuminate\Http\Response |
| 58 | + */ |
| 59 | + public function show($id) |
| 60 | + { |
| 61 | + $post = Post::findOrFail($id); |
| 62 | + return new PostResource($post); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Show the form for editing the specified resource. |
| 67 | + * |
| 68 | + * @param int $id |
| 69 | + * @return \Illuminate\Http\Response |
| 70 | + */ |
| 71 | + public function edit($id) |
| 72 | + { |
| 73 | + // |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Update the specified resource in storage. |
| 78 | + * |
| 79 | + * @param \Illuminate\Http\Request $request |
| 80 | + * @param int $id |
| 81 | + * @return \Illuminate\Http\Response |
| 82 | + */ |
| 83 | + public function update(Request $request, $id) |
| 84 | + { |
| 85 | + // |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Remove the specified resource from storage. |
| 90 | + * |
| 91 | + * @param int $id |
| 92 | + * @return \Illuminate\Http\Response |
| 93 | + */ |
| 94 | + public function destroy($id) |
| 95 | + { |
| 96 | + $post = Post::findOrFail($id); |
| 97 | + |
| 98 | + if($post->delete()){ |
| 99 | + return new PostResource($post); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments