|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace API\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use API\Repositories\Contracts\WorkoutPlanRepositoryInterface; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
use Validator; |
|
8
|
|
|
|
|
9
|
|
|
final class WorkoutPlanRepository extends BaseRepository implements WorkoutPlanRepositoryInterface |
|
10
|
|
|
{ |
|
11
|
|
|
public function workoutPlanByUser($id) |
|
12
|
|
|
{ |
|
13
|
|
|
$workoutPlanByUser = DB::table('workout_plan AS wkP') |
|
14
|
|
|
->join('workout_type AS wkT', 'wkT.id_workout_type', '=', 'wkP.fk_workout_type') |
|
15
|
|
|
->join('user AS u', 'wkP.fk_user', '=', 'u.id_user') |
|
16
|
|
|
->where('wkP.fk_user', $id) |
|
17
|
|
|
->select('u.id_user', 'u.first_name as user_name', 'u.email AS user_email', |
|
18
|
|
|
'wkT.name AS workout_type', 'wkT.description AS workout_type_description', |
|
19
|
|
|
'wkP.id_workout_plan AS workout_plan_id', |
|
20
|
|
|
'wkP.date AS workout_plan_date') |
|
21
|
|
|
->get(); |
|
22
|
|
|
|
|
23
|
|
|
if (count($workoutPlanByUser) > 0) { |
|
24
|
|
|
return response()->json(['status' => 'success', 'data' => ['workoutPlanByUser' => $workoutPlanByUser]], 200); |
|
25
|
|
|
} |
|
26
|
|
|
return response()->json(['status' => 'error', 'message' => 'no data'], 404); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
View Code Duplication |
public function store($request) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
$data = $request->all(); |
|
32
|
|
|
|
|
33
|
|
|
$validator = Validator::make($data, [ |
|
34
|
|
|
'fk_workout_type' => 'required', |
|
35
|
|
|
'fk_user' => 'required', |
|
36
|
|
|
'date' => 'required', |
|
37
|
|
|
]); |
|
38
|
|
|
|
|
39
|
|
|
if ($validator->fails()) { |
|
40
|
|
|
return response()->json([ |
|
41
|
|
|
'status' => 'fail', |
|
42
|
|
|
'data' => [ |
|
43
|
|
|
'fk_workout_type' => 'required', |
|
44
|
|
|
'fk_user' => 'required', |
|
45
|
|
|
'date' => 'required', |
|
46
|
|
|
]], 400); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$workoutPlan = $this->workoutPlan->create($data); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
if (count($workoutPlan) > 0) { |
|
52
|
|
|
return response()->json(['status' => 'success'], 201); |
|
53
|
|
|
} |
|
54
|
|
|
return response()->json(['status' => 'error'], 500); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
View Code Duplication |
public function update($request, $id) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$workoutPlan = $this->workoutPlan->find($id); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
if (count($workoutPlan) > 0) { |
|
62
|
|
|
|
|
63
|
|
|
$data = $request->all(); |
|
64
|
|
|
|
|
65
|
|
|
$validator = Validator::make($data, [ |
|
66
|
|
|
'fk_workout_type' => 'sometimes|required', |
|
67
|
|
|
'fk_user' => 'sometimes|required', |
|
68
|
|
|
'date' => 'sometimes|required', |
|
69
|
|
|
]); |
|
70
|
|
|
|
|
71
|
|
|
if ($validator->fails()) { |
|
72
|
|
|
return response()->json([ |
|
73
|
|
|
'status' => 'fail', |
|
74
|
|
|
'data' => [ |
|
75
|
|
|
'fk_workout_type' => 'required', |
|
76
|
|
|
'fk_user' => 'required', |
|
77
|
|
|
'date' => 'required', |
|
78
|
|
|
]], 400); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$workoutPlan->fill($data)->save(); |
|
82
|
|
|
|
|
83
|
|
|
return response()->json(['status' => 'success'], 200); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return response()->json(['status' => 'error'], 500); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
View Code Duplication |
public function delete($id) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
$workoutPlan = $this->workoutPlan->find($id); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
if (count($workoutPlan) > 0) { |
|
94
|
|
|
$workoutPlan->delete(); |
|
95
|
|
|
return response()->json(['status' => 'success', 'data' => null], 200); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return response()->json(['status' => 'error'], 500); |
|
99
|
|
|
} |
|
100
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.