Passed
Push — task/all-applications-to-pdf ( cb7375...852a46 )
by Tristan
59:34 queued 50:51
created

RatingGuideQuestionController::store()   A

Complexity

Conditions 2
Paths 10

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 30
ccs 0
cts 20
cp 0
rs 9.584
c 0
b 0
f 0
cc 2
nc 10
nop 1
crap 6
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
        $this->authorize('create', RatingGuideQuestion::class);
24
        try {
25
            $job_poster_id = (int)$request->json('job_poster_id');
26
            $assessment_type_id = (int)$request->json('assessment_type_id');
27
            $question = $request->json('question');
28
29
            JobPoster::findOrFail($job_poster_id);
30
            AssessmentType::findOrFail($assessment_type_id);
31
32
            $ratingGuideQuestion = new RatingGuideQuestion([
33
                'job_poster_id' => $job_poster_id,
34
                'assessment_type_id' => $assessment_type_id,
35
                'question' => $question,
36
            ]);
37
            // Check that this user is allowed to create an Assessment for this criterion
38
            $this->authorize('update', $ratingGuideQuestion);
39
40
            $ratingGuideQuestion->save();
41
            $ratingGuideQuestion->refresh();
42
        } catch (\Exception $e) {
43
            return response()->json([
44
                'error' => $e->getMessage()
45
            ], 400);
46
        }
47
48
        return [
49
            'success' => "Successfully created rating guide question $ratingGuideQuestion->id",
50
            'rating_guide_question' => $ratingGuideQuestion->toArray(),
51
        ];
52
    }
53
54
    /**
55
     * Display the specified resource.
56
     *
57
     * @param  \App\Models\RatingGuideQuestion $ratingGuideQuestion Incoming object.
58
     * @return mixed
59
     */
60
    public function show(RatingGuideQuestion $ratingGuideQuestion)
61
    {
62
        $this->authorize('view', $ratingGuideQuestion);
63
        $ratingGuideQuestion->load([
64
            'job_poster',
65
            'assessment_type'
66
        ]);
67
        return $ratingGuideQuestion->toArray();
68
    }
69
70
    /**
71
     * Update the specified resource in storage.
72
     *
73
     * @param  \Illuminate\Http\Request        $request             Incoming request.
74
     * @param  \App\Models\RatingGuideQuestion $ratingGuideQuestion Incoming object.
75
     * @throws \InvalidArgumentException For missing $question.
76
     * @return mixed
77
     */
78
    public function update(Request $request, RatingGuideQuestion $ratingGuideQuestion)
79
    {
80
        $this->authorize('update', $ratingGuideQuestion);
81
        try {
82
            $job_poster_id = (int)$request->json('job_poster_id');
83
            $assessment_type_id = (int)$request->json('assessment_type_id');
84
            $question = $request->json('question');
85
86
            JobPoster::findOrFail($job_poster_id);
87
            AssessmentType::findOrFail($assessment_type_id);
88
89
            if (empty($question)) {
90
                throw new \InvalidArgumentException('Question is required.');
91
            }
92
93
            $ratingGuideQuestion->job_poster_id = $job_poster_id;
94
            $ratingGuideQuestion->assessment_type_id = $assessment_type_id;
95
            $ratingGuideQuestion->question = $question;
96
            $ratingGuideQuestion->save();
97
        } catch (\Exception $e) {
98
            return response()->json([
99
                'error' => $e->getMessage()
100
            ], 400);
101
        }
102
103
        return [
104
            'success' => "Successfully updated rating guide question $ratingGuideQuestion->id",
105
            'rating_guide_question' => $ratingGuideQuestion->toArray(),
106
        ];
107
    }
108
109
    /**
110
     * Remove the specified resource from storage.
111
     *
112
     * @param  \App\Models\RatingGuideQuestion $ratingGuideQuestion Incoming object.
113
     * @return mixed
114
     */
115
    public function destroy(RatingGuideQuestion $ratingGuideQuestion)
116
    {
117
        $this->authorize('delete', $ratingGuideQuestion);
118
        $ratingGuideQuestion->delete();
119
120
        return [
121
            'success' => "Successfully deleted rating guide question $ratingGuideQuestion->id"
122
        ];
123
    }
124
}
125