PdrbHargaDasarController::index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 7
rs 9.4285
1
<?php namespace Bantenprov\PdrbHargaDasar\Http\Controllers;
2
3
/* require */
4
use App\Http\Controllers\Controller;
5
use Illuminate\Http\Request;
6
use Bantenprov\PdrbHargaDasar\Facades\PdrbHargaDasar;
7
8
/* Models */
9
use Bantenprov\PdrbHargaDasar\Models\Bantenprov\PdrbHargaDasar\PdrbHargaDasar as PdrbModel;
10
use Bantenprov\PdrbHargaDasar\Models\Bantenprov\PdrbHargaDasar\Province;
11
use Bantenprov\PdrbHargaDasar\Models\Bantenprov\PdrbHargaDasar\Regency;
12
13
/* etc */
14
use Validator;
15
16
/**
17
 * The PdrbHargaDasarController class.
18
 *
19
 * @package Bantenprov\PdrbHargaDasar
20
 * @author  bantenprov <[email protected]>
21
 */
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)
115
    {
116
        /* todo : return json */
117
        return '';
118
119
    }
120
121
    public function destroy($id)
122
    {
123
        /* todo : return json */
124
        return '';
125
126
    }
127
}
128
129