Passed
Pull Request — master (#46)
by
unknown
04:36
created

ChampionshipSettingsController::destroy()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 3
nop 1
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\FightersGroup;
6
use DaveJamesMiller\Breadcrumbs\Exception;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\App;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\Response;
11
use Illuminate\Support\Facades\View;
12
use Xoco70\LaravelTournaments\Models\ChampionshipSettings;
13
14
class ChampionshipSettingsController extends Controller
15
{
16
17
    protected $defaultSettings;
18
19
    /**
20
     * Store a newly created resource in storage.
21
     *
22
     * @param  \Illuminate\Http\Request $request
23
     * @param $championshipId
24
     * @return \Illuminate\Http\JsonResponse
25
     */
26 1
    public function store(Request $request, $championshipId)
27
    {
28
29
        try {
30 1
            if (Auth::check()) {
31 1
                App::setLocale(Auth::user()->locale);
32
            }
33 1
            $request->request->add(['championship_id' => $championshipId]);
34 1
            $setting = ChampionshipSettings::create($request->all());
35 1
            return Response::json(['setting' => $setting, 'msg' => trans('msg.category_create_successful'), 'status' => 'success']);
36
        } catch (Exception $e) {
37
            return Response::json(['msg' => trans('msg.category_create_error'), 'status' => 'error']);
38
        }
39
    }
40
41
42
    /**
43
     * Update the specified resource in storage.
44
     *
45
     * @param  \Illuminate\Http\Request $request
46
     * @param $championshipId
47
     * @param $championshipSettingsId
48
     * @return \Illuminate\Http\JsonResponse
49
     */
50 1
    public function update(Request $request, $championshipId, $championshipSettingsId)
51
    {
52
        try {
53
            //TODO As it is a WebService, Locale is resetted, as User info
54 1
            if (Auth::check()) {
55 1
                App::setLocale(Auth::user()->locale);
56
            }
57 1
            $setting = ChampionshipSettings::findOrFail($championshipSettingsId)->fill($request->all());
58
59
            // If we changed one of those data, remove tree
60 1
            if ($setting->isDirty('hasPreliminary') || $setting->isDirty('hasPreliminary') || $setting->isDirty('treeType')) {
61 1
                FightersGroup::where('championship_id', $championshipId)->delete();
62
            }
63 1
            $setting->save();
64 1
            return Response::json(['setting' => $setting, 'msg' => trans('msg.category_update_successful'), 'status' => 'success']);
65
        } catch (Exception $e) {
66
            return Response::json(['msg' => trans('msg.category_update_error'), 'status' => 'error']);
67
        }
68
    }
69
70
    /**
71
     * Remove the specified resource from storage.
72
     *
73
     * @param ChampionshipSettings $cs
74
     * @return \Illuminate\Http\JsonResponse
75
     */
76
    public function destroy(ChampionshipSettings $cs)
77
    {
78
        try {
79
            $cs->delete();
80
            return Response::json(['msg' => trans('msg.category_delete_succesful'), 'status' => 'success']);
81
        } catch (Exception $e) {
82
            return Response::json(['msg' => trans('msg.category_delete_error'), 'status' => 'error']);
83
        }
84
    }
85
86
}
87