|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Coderstm\Http\Controllers\Page; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Coderstm\Models\Page\Block; |
| 7 | +use Coderstm\Http\Controllers\Controller; |
| 8 | +use Illuminate\Http\Resources\Json\ResourceCollection; |
| 9 | + |
| 10 | +class BlockController extends Controller |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Create the controller instance. |
| 14 | + * |
| 15 | + * @return void |
| 16 | + */ |
| 17 | + public function __construct() |
| 18 | + { |
| 19 | + // $this->authorizeResource(Block::class); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Display a listing of the resource. |
| 24 | + */ |
| 25 | + public function index(Request $request) |
| 26 | + { |
| 27 | + $block = Block::query(); |
| 28 | + |
| 29 | + $block = $block->orderBy($request->sortBy ?? 'created_at', $request->direction ?? 'desc'); |
| 30 | + |
| 31 | + if ($request->isNotFilled('rowsPerPage')) { |
| 32 | + return $block->get(); |
| 33 | + } |
| 34 | + |
| 35 | + return new ResourceCollection($block->paginate($request->rowsPerPage ?? 15)); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Store a newly created resource in storage. |
| 40 | + */ |
| 41 | + public function store(Request $request) |
| 42 | + { |
| 43 | + // Set rules |
| 44 | + $rules = [ |
| 45 | + 'data' => 'array|required', |
| 46 | + 'data.key' => 'string|required', |
| 47 | + 'data.options' => 'array|required', |
| 48 | + ]; |
| 49 | + |
| 50 | + // Validate those rules |
| 51 | + $request->validate($rules); |
| 52 | + |
| 53 | + // create the block |
| 54 | + $block = Block::create($request->input()); |
| 55 | + |
| 56 | + return response()->json([ |
| 57 | + 'data' => $block, |
| 58 | + 'message' => trans_module('store', 'block'), |
| 59 | + ], 200); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Remove the specified resource from storage. |
| 64 | + */ |
| 65 | + public function destroy(Block $block) |
| 66 | + { |
| 67 | + $block->delete(); |
| 68 | + |
| 69 | + return response()->json([ |
| 70 | + 'message' => trans_module('destroy', 'block'), |
| 71 | + ], 200); |
| 72 | + } |
| 73 | +} |
0 commit comments