Passed
Push — feature/screening-plan-redux ( 8ed68a...044f96 )
by Chris
08:45
created

RatingGuideQuestionController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
eloc 46
dl 0
loc 104
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 28 3
A destroy() 0 6 1
A show() 0 7 1
A update() 0 26 3
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\RatingGuideQuestion;
6
use App\Models\JobPoster;
7
use App\Models\Lookup\AssessmentType;
8
9
use Illuminate\Http\Request;
10
use Illuminate\Http\Response;
11
12
class RatingGuideQuestionController extends Controller
13
{
14
    /**
15
     * Store a newly created resource in storage.
16
     *
17
     * @param  \Illuminate\Http\Request $request Incoming request.
18
     * @throws \InvalidArgumentException For missing $question.
19
     * @return mixed
20
     */
21
    public function store(Request $request)
22
    {
23
        try {
24
            $job_poster_id = (int)$request->json('job_poster_id');
25
            $assessment_type_id = (int)$request->json('assessment_type_id');
26
            $question = $request->json('question');
27
28
            JobPoster::findOrFail($job_poster_id);
29
            AssessmentType::findOrFail($assessment_type_id);
30
31
            if (empty($question)) {
32
                throw new \InvalidArgumentException('Question is required.');
33
            }
34
            $ratingGuideQuestion = new RatingGuideQuestion([
35
                'job_poster_id' => $job_poster_id,
36
                'assessment_type_id' => $assessment_type_id,
37
                'question' => $question,
38
            ]);
39
            $ratingGuideQuestion->save();
40
            $ratingGuideQuestion->refresh();
41
        } catch (\Exception $e) {
42
            return response()->json([
43
                'error' => $e->getMessage()
44
            ], 400);
45
        }
46
47
        return [
48
            'success' => "Successfully created rating guide question $ratingGuideQuestion->id"
49
        ];
50
    }
51
52
    /**
53
     * Display the specified resource.
54
     *
55
     * @param  \App\Models\RatingGuideQuestion $ratingGuideQuestion Incoming object.
56
     * @return mixed
57
     */
58
    public function show(RatingGuideQuestion $ratingGuideQuestion)
59
    {
60
        $ratingGuideQuestion->load([
61
            'job_poster',
62
            'assessment_type'
63
        ]);
64
        return $ratingGuideQuestion->toArray();
65
    }
66
67
    /**
68
     * Update the specified resource in storage.
69
     *
70
     * @param  \Illuminate\Http\Request        $request             Incoming request.
71
     * @param  \App\Models\RatingGuideQuestion $ratingGuideQuestion Incoming object.
72
     * @throws \InvalidArgumentException For missing $question.
73
     * @return mixed
74
     */
75
    public function update(Request $request, RatingGuideQuestion $ratingGuideQuestion)
76
    {
77
        try {
78
            $job_poster_id = (int)$request->json('job_poster_id');
79
            $assessment_type_id = (int)$request->json('assessment_type_id');
80
            $question = $request->json('question');
81
82
            JobPoster::findOrFail($job_poster_id);
83
            AssessmentType::findOrFail($assessment_type_id);
84
85
            if (empty($question)) {
86
                throw new \InvalidArgumentException('Question is required.');
87
            }
88
89
            $ratingGuideQuestion->job_poster_id = $job_poster_id;
90
            $ratingGuideQuestion->assessment_type_id = $assessment_type_id;
91
            $ratingGuideQuestion->question = $question;
92
            $ratingGuideQuestion->save();
93
        } catch (\Exception $e) {
94
            return response()->json([
95
                'error' => $e->getMessage()
96
            ], 400);
97
        }
98
99
        return [
100
            'success' => "Successfully updated rating guide question $ratingGuideQuestion->id"
101
        ];
102
    }
103
104
    /**
105
     * Remove the specified resource from storage.
106
     *
107
     * @param  \App\Models\RatingGuideQuestion $ratingGuideQuestion Incoming object.
108
     * @return mixed
109
     */
110
    public function destroy(RatingGuideQuestion $ratingGuideQuestion)
111
    {
112
        $ratingGuideQuestion->delete();
113
114
        return [
115
            'success' => "Successfully deleted rating guide question $ratingGuideQuestion->id"
116
        ];
117
    }
118
}
119