|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace API\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use API\Repositories\Contracts\WorkoutPlanRepositoryInterface; |
|
6
|
|
|
use Illuminate\Support\Facades\Validator; |
|
7
|
|
|
|
|
8
|
|
|
final class WorkoutPlanRepository extends BaseRepository implements WorkoutPlanRepositoryInterface |
|
9
|
|
|
{ |
|
10
|
|
View Code Duplication |
public function workoutPlanByUser($id) |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
$workoutPlansByUser = $this->workoutPlan |
|
|
|
|
|
|
13
|
|
|
->where('user_id', $id) |
|
14
|
|
|
->with('workoutType') |
|
15
|
|
|
->get(); |
|
16
|
|
|
|
|
17
|
|
|
if ($workoutPlansByUser) { |
|
18
|
|
|
return response()->json(['status' => 'success', 'data' => ['workoutPlanByUser' => $workoutPlansByUser]], 200); |
|
19
|
|
|
} |
|
20
|
|
|
return response()->json(['status' => 'error', 'message' => 'no data'], 404); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
View Code Duplication |
public function store($request) |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
$data = $request->all(); |
|
26
|
|
|
|
|
27
|
|
|
$validator = Validator::make($data, [ |
|
28
|
|
|
'user_id' => 'required', |
|
29
|
|
|
]); |
|
30
|
|
|
|
|
31
|
|
|
if ($validator->fails()) { |
|
32
|
|
|
return response()->json([ |
|
33
|
|
|
'status' => 'fail', |
|
34
|
|
|
'data' => [ |
|
35
|
|
|
'user_id' => 'required', |
|
36
|
|
|
]], 422); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$createWorkoutPlan = $this->workoutPlan->create($data); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
if ($createWorkoutPlan) { |
|
42
|
|
|
return response()->json(['status' => 'success'], 201); |
|
43
|
|
|
} |
|
44
|
|
|
return response()->json(['status' => 'error'], 500); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
View Code Duplication |
public function update($request, $id) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$workoutPlan = $this->workoutPlan->find($id); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
if ($workoutPlan) { |
|
52
|
|
|
|
|
53
|
|
|
$data = $request->all(); |
|
54
|
|
|
|
|
55
|
|
|
$validator = Validator::make($data, [ |
|
56
|
|
|
'user_id' => 'sometimes|required', |
|
57
|
|
|
]); |
|
58
|
|
|
|
|
59
|
|
|
if ($validator->fails()) { |
|
60
|
|
|
return response()->json([ |
|
61
|
|
|
'status' => 'fail', |
|
62
|
|
|
'data' => [ |
|
63
|
|
|
'user_id' => 'required', |
|
64
|
|
|
]], 422); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$workoutPlan->fill($data)->save(); |
|
68
|
|
|
|
|
69
|
|
|
return response()->json(['status' => 'success'], 200); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return response()->json(['status' => 'error'], 500); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
View Code Duplication |
public function delete($id) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$workoutPlan = $this->workoutPlan->find($id); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
if ($workoutPlan) { |
|
80
|
|
|
$workoutPlan->delete(); |
|
81
|
|
|
return response()->json(['status' => 'success', 'data' => null], 200); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return response()->json(['status' => 'error'], 500); |
|
85
|
|
|
} |
|
86
|
|
|
} |
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.