1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
use Illuminate\Http\Request; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class BuildersController. |
9
|
|
|
*/ |
10
|
|
|
class BuildersController extends Controller |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Create account |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
public function createAccount(Request $request) |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$data = [ |
19
|
|
|
"name" => $request->name, |
20
|
|
|
"cp" => $request->cp, |
21
|
|
|
"phone" => $request->phone, |
22
|
|
|
"sexe" => $request->sexe, |
23
|
|
|
"news" => $request->news, |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017'); |
27
|
|
|
$collection = new \MongoDB\Collection($manager, 'builders', 'account'); |
28
|
|
|
$stat = [ |
29
|
|
|
'email' => $request->email, |
30
|
|
|
'data' => $data, |
31
|
|
|
'created' => new \DateTime("now"), |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
try{ |
35
|
|
|
$collection->insertOne($stat); |
36
|
|
|
}catch (\Exception $e){ |
37
|
|
|
return response()->json(['state' => false]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$data["email"] = $request->email; |
|
|
|
|
41
|
|
|
return response()->json(['data' => $data, 'state' => true]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* List account |
46
|
|
|
*/ |
47
|
|
|
public function listAccount() |
48
|
|
|
{ |
49
|
|
|
$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017'); |
50
|
|
|
$collection = new \MongoDB\Collection($manager, 'builders', 'account'); |
51
|
|
|
|
52
|
|
|
$result = $collection->find()->toArray(); |
53
|
|
|
|
54
|
|
|
$tab = []; |
55
|
|
|
foreach($result as $one){ |
56
|
|
|
$tab[] = $one->bsonSerialize(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return response()->json($tab); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Update account |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function updateAccount(Request $request) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$data = [ |
68
|
|
|
"name" => $request->name, |
69
|
|
|
"cp" => $request->cp, |
70
|
|
|
"phone" => $request->phone, |
71
|
|
|
"sexe" => $request->sexe, |
72
|
|
|
"news" => $request->news, |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017'); |
76
|
|
|
$collection = new \MongoDB\Collection($manager, 'builders', 'account'); |
77
|
|
|
$stat = [ |
78
|
|
|
'email' => $request->email, |
79
|
|
|
'data' => $data, |
80
|
|
|
'created' => new \DateTime("now"), |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
try{ |
84
|
|
|
$collection->updateOne(["email" => $request->email], $stat); |
85
|
|
|
}catch (\Exception $e){ |
86
|
|
|
return response()->json(['state' => false]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$data["email"] = $request->email; |
|
|
|
|
90
|
|
|
return response()->json(['data' => $data, 'state' => true]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
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.