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

RatingGuideAnswerController::update()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 27
ccs 0
cts 19
cp 0
rs 9.6
c 0
b 0
f 0
cc 3
nc 8
nop 2
crap 12
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\RatingGuideAnswer;
6
use App\Models\RatingGuideQuestion;
7
use App\Models\Criteria;
8
9
use Illuminate\Http\Request;
10
use Illuminate\Http\Response;
11
12
class RatingGuideAnswerController 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 $expected_answer.
19
     * @return mixed
20
     */
21
    public function store(Request $request)
22
    {
23
        $this->authorize('create', RatingGuideAnswer::class);
24
        try {
25
            $rating_guide_question_id = (int)$request->json('rating_guide_question_id');
26
            $criterion_id = (int)$request->json('criterion_id');
27
            $expected_answer = $request->json('expected_answer');
28
29
            RatingGuideQuestion::findOrFail($rating_guide_question_id);
30
            Criteria::findOrFail($criterion_id);
31
32
            $ratingGuideAnswer = new RatingGuideAnswer([
33
                'rating_guide_question_id' => $rating_guide_question_id,
34
                'criterion_id' => $criterion_id,
35
                'expected_answer' => $expected_answer,
36
            ]);
37
             // Check that this user is allowed to create an Assessment for this question
38
            $this->authorize('update', $ratingGuideAnswer);
39
            $ratingGuideAnswer->save();
40
            $ratingGuideAnswer->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 answer $ratingGuideAnswer->id",
49
            'rating_guide_answer' => $ratingGuideAnswer->toArray(),
50
        ];
51
    }
52
53
    /**
54
     * Display the specified resource.
55
     *
56
     * @param  \App\Models\RatingGuideAnswer $ratingGuideAnswer Incoming object.
57
     * @return mixed
58
     */
59
    public function show(RatingGuideAnswer $ratingGuideAnswer)
60
    {
61
        $this->authorize('view', $ratingGuideAnswer);
62
        return $ratingGuideAnswer->toArray();
63
    }
64
65
    /**
66
     * Update the specified resource in storage.
67
     *
68
     * @param  \Illuminate\Http\Request      $request           Incoming request.
69
     * @param  \App\Models\RatingGuideAnswer $ratingGuideAnswer Incoming object.
70
     * @throws \InvalidArgumentException For empty $expected_answer.
71
     * @return mixed
72
     */
73
    public function update(Request $request, RatingGuideAnswer $ratingGuideAnswer)
74
    {
75
        $this->authorize('update', $ratingGuideAnswer);
76
        try {
77
            $rating_guide_question_id = (int)$request->json('rating_guide_question_id');
78
            $criterion_id = (int)$request->json('criterion_id');
79
            $expected_answer = $request->json('expected_answer');
80
81
            RatingGuideQuestion::findOrFail($rating_guide_question_id);
82
            Criteria::findOrFail($criterion_id);
83
84
            if (empty($expected_answer)) {
85
                throw new \InvalidArgumentException('Expected answer is required.');
86
            }
87
            $ratingGuideAnswer->rating_guide_question_id = $rating_guide_question_id;
88
            $ratingGuideAnswer->criterion_id = $criterion_id;
89
            $ratingGuideAnswer->expected_answer = $expected_answer;
90
            $ratingGuideAnswer->save();
91
        } catch (\Exception $e) {
92
            return response()->json([
93
                'error' => $e->getMessage()
94
            ], 400);
95
        }
96
97
        return [
98
            'success' => "Successfully updated rating guide answer $ratingGuideAnswer->id",
99
            'rating_guide_answer' => $ratingGuideAnswer->toArray(),
100
        ];
101
    }
102
103
    /**
104
     * Remove the specified resource from storage.
105
     *
106
     * @param  \App\Models\RatingGuideAnswer $ratingGuideAnswer Incoming object.
107
     * @return mixed
108
     */
109
    public function destroy(RatingGuideAnswer $ratingGuideAnswer)
110
    {
111
        $this->authorize('delete', $ratingGuideAnswer);
112
        $ratingGuideAnswer->delete();
113
114
        return [
115
            'success' => "Successfully deleted rating guide answer $ratingGuideAnswer->id"
116
        ];
117
    }
118
}
119