1 | <?php namespace Bantenprov\PdrbHargaDasar\Http\Controllers; |
||
22 | class PdrbHargaDasarController extends Controller |
||
23 | { |
||
24 | |||
25 | protected $province; |
||
26 | |||
27 | protected $regency; |
||
28 | |||
29 | protected $pdrb; |
||
30 | |||
31 | public function __construct(Regency $regency, Province $province, PdrbModel $pdrb) |
||
32 | { |
||
33 | $this->regency = $regency; |
||
34 | $this->province = $province; |
||
35 | $this->pdrb = $pdrb; |
||
36 | } |
||
37 | |||
38 | public function index(Request $request) |
||
39 | { |
||
40 | /* todo : return json */ |
||
41 | |||
42 | return 'index'; |
||
43 | |||
44 | } |
||
45 | |||
46 | public function create() |
||
47 | { |
||
48 | |||
49 | return response()->json([ |
||
50 | 'provinces' => $this->province->all(), |
||
51 | 'regencies' => $this->regency->all() |
||
52 | ]); |
||
53 | } |
||
54 | |||
55 | public function show($id) |
||
56 | { |
||
57 | |||
58 | $pdrb = $this->pdrb->find($id); |
||
59 | |||
60 | return response()->json([ |
||
61 | 'negara' => $pdrb->negara, |
||
62 | 'province' => $pdrb->getProvince->name, |
||
63 | 'regencies' => $pdrb->getRegency->name, |
||
64 | 'tahun' => $pdrb->tahun, |
||
65 | 'data' => $pdrb->data |
||
66 | ]); |
||
67 | } |
||
68 | |||
69 | public function store(Request $request) |
||
70 | { |
||
71 | |||
72 | $validator = Validator::make($request->all(),[ |
||
73 | 'negara' => 'required', |
||
74 | 'province_id' => 'required', |
||
75 | 'regency_id' => 'required', |
||
76 | 'kab_kota' => 'required', |
||
77 | 'tahun' => 'required|integer', |
||
78 | 'data' => 'required|integer', |
||
79 | ]); |
||
80 | |||
81 | if($validator->fails()) |
||
82 | { |
||
83 | return response()->json([ |
||
84 | 'title' => 'error', |
||
85 | 'message' => 'add failed', |
||
86 | 'type' => 'failed', |
||
87 | 'errors' => $validator->errors() |
||
88 | ]); |
||
89 | } |
||
90 | |||
91 | $check = $this->pdrb->where('regency_id',$request->regency_id)->where('tahun',$request->tahun)->count(); |
||
92 | |||
93 | if($check > 0) |
||
94 | { |
||
95 | return response()->json([ |
||
96 | 'title' => 'error', |
||
97 | 'message' => 'Data allready exist', |
||
98 | 'type' => 'failed', |
||
99 | ]); |
||
100 | |||
101 | }else{ |
||
102 | $data = $this->pdrb->create($request->all()); |
||
103 | |||
104 | return response()->json([ |
||
105 | 'type' => 'success', |
||
106 | 'title' => 'success', |
||
107 | 'id' => $data->id, |
||
108 | 'message' => 'PDRB '. $this->regency->find($request->regency_id)->name .' tahun '. $request->tahun .' successfully created', |
||
109 | ]); |
||
110 | } |
||
111 | |||
112 | } |
||
113 | |||
114 | public function update(Request $request, $id) |
||
120 | |||
121 | public function destroy($id) |
||
127 | } |
||
128 | |||
129 |