3
3
namespace Vcian \LaravelDBAuditor \Controllers ;
4
4
5
5
use Illuminate \Http \JsonResponse ;
6
+ use Illuminate \Http \Request ;
6
7
use Vcian \LaravelDBAuditor \Constants \Constant ;
7
8
use Vcian \LaravelDBAuditor \Traits \Audit ;
8
9
use Vcian \LaravelDBAuditor \Traits \Rules ;
@@ -23,9 +24,9 @@ public function index()
23
24
24
25
/**
25
26
* Get audit table list
26
- * @return JsonResponse
27
+ * @return JsonResponse
27
28
*/
28
- public function getAudit () : JsonResponse
29
+ public function getAudit (): JsonResponse
29
30
{
30
31
$ columnName = 'status ' ;
31
32
$ noConstraint = '<img src= ' . asset ("auditor/icon/close.svg " ) . ' alt="key" class="m-auto" /> ' ;
@@ -37,6 +38,7 @@ public function getAudit() : JsonResponse
37
38
} else {
38
39
$ value [$ columnName ] = $ constraint ;
39
40
}
41
+ $ value ["size " ] = $ value ['size ' ]." MB " ;
40
42
return $ value ;
41
43
}, $ this ->tablesRule ());
42
44
@@ -50,9 +52,9 @@ public function getAudit() : JsonResponse
50
52
/**
51
53
* Get table data
52
54
* @param string $tableName
53
- * @return JsonResponse
55
+ * @return JsonResponse
54
56
*/
55
- public function getTableData (string $ tableName ) : JsonResponse
57
+ public function getTableData (string $ tableName ): JsonResponse
56
58
{
57
59
return response ()->json (array (
58
60
"data " => $ this ->tableRules ($ tableName )
@@ -62,11 +64,14 @@ public function getTableData(string $tableName) : JsonResponse
62
64
/**
63
65
* Get Constraint list
64
66
* @param string $tableName
65
- * @return JsonResponse
67
+ * @return JsonResponse
66
68
*/
67
- public function getTableConstraint (string $ tableName ) : JsonResponse
69
+ public function getTableConstraint (string $ tableName ): JsonResponse
68
70
{
69
71
72
+ $ noConstraintFields = $ this ->getNoConstraintFields ($ tableName );
73
+ $ constraintList = $ this ->getConstraintList ($ tableName , $ noConstraintFields );
74
+
70
75
$ data = [
71
76
"fields " => $ this ->getFieldsDetails ($ tableName ),
72
77
'constrain ' => [
@@ -76,26 +81,30 @@ public function getTableConstraint(string $tableName) : JsonResponse
76
81
'index ' => $ this ->getConstraintField ($ tableName , Constant::CONSTRAINT_INDEX_KEY )
77
82
]
78
83
];
79
-
80
84
$ response = [];
81
85
$ greenKey = '<img src= ' . asset ("auditor/icon/green-key.svg " ) . ' alt="key" class="m-auto" /> ' ;
82
86
$ grayKey = '<img src= ' . asset ("auditor/icon/gray-key.svg " ) . ' alt="key" class="m-auto" /> ' ;
83
87
84
88
foreach ($ data ['fields ' ] as $ table ) {
85
89
86
- $ primaryKey = $ indexing = $ uniqueKey = $ foreignKey = "- " ;
90
+ $ primaryKey = $ indexKey = $ uniqueKey = $ foreignKey = "- " ;
91
+
87
92
88
93
if (in_array ($ table ->COLUMN_NAME , $ data ['constrain ' ]['primary ' ])) {
89
94
$ primaryKey = $ greenKey ;
90
- } else if (in_array ($ table ->COLUMN_NAME , $ data ['constrain ' ]['unique ' ])) {
95
+ }
96
+
97
+ if (in_array ($ table ->COLUMN_NAME , $ data ['constrain ' ]['unique ' ])) {
91
98
$ uniqueKey = $ grayKey ;
92
- } else if (in_array ($ table ->COLUMN_NAME , $ data ['constrain ' ]['index ' ])) {
93
- $ indexing = $ grayKey ;
99
+ }
100
+
101
+ if (in_array ($ table ->COLUMN_NAME , $ data ['constrain ' ]['index ' ])) {
102
+ $ indexKey = $ grayKey ;
94
103
}
95
104
96
105
foreach ($ data ['constrain ' ]['foreign ' ] as $ foreign ) {
97
- if ($ table ->COLUMN_NAME === $ foreign[ ' column_name ' ] ) {
98
- $ foreignKeyToottip = '<div class="inline-flex">
106
+ if ($ table ->COLUMN_NAME === $ foreign ) {
107
+ $ foreignKeyTooltip = '<div class="inline-flex">
99
108
<img src= ' . asset ("auditor/icon/gray-key.svg " ) . ' alt="key" class="mr-2">
100
109
<div class="relative flex flex-col items-center group">
101
110
<svg class="w-5 h-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
@@ -107,15 +116,101 @@ public function getTableConstraint(string $tableName) : JsonResponse
107
116
</div>
108
117
</div>
109
118
</div> ' ;
110
- $ foreignKey = $ foreignKeyToottip ;
119
+ $ foreignKey = $ foreignKeyTooltip ;
120
+ }
121
+ }
122
+
123
+ foreach ($ constraintList as $ constraint ) {
124
+ switch ($ constraint ) {
125
+ case Constant::CONSTRAINT_PRIMARY_KEY :
126
+ if (in_array ($ table ->COLUMN_NAME , $ noConstraintFields ['integer ' ])) {
127
+ $ primaryKey = '<img src= ' . asset ("auditor/icon/add.svg " ) . ' alt="key" class="m-auto add-constraint- ' . $ table ->COLUMN_NAME . '- ' . Constant::CONSTRAINT_PRIMARY_KEY . '" style="height:20px;cursor: pointer;" onclick="add(` ' . $ table ->COLUMN_NAME . '`, ` ' . Constant::CONSTRAINT_PRIMARY_KEY . '`)"/> ' ;
128
+ }
129
+ break ;
130
+ case Constant::CONSTRAINT_INDEX_KEY :
131
+ if (in_array ($ table ->COLUMN_NAME , $ noConstraintFields ['mix ' ])) {
132
+ $ indexKey = '<img src= ' . asset ("auditor/icon/add.svg " ) . ' alt="key" class="m-auto add-constraint- ' . $ table ->COLUMN_NAME . '- ' . Constant::CONSTRAINT_INDEX_KEY . '" style="height:20px;cursor: pointer;" onclick="add(` ' . $ table ->COLUMN_NAME . '`, ` ' . Constant::CONSTRAINT_INDEX_KEY . '`)"/> ' ;
133
+ }
134
+ break ;
135
+ case Constant::CONSTRAINT_UNIQUE_KEY :
136
+ if (in_array ($ table ->COLUMN_NAME , $ noConstraintFields ['mix ' ])) {
137
+ $ fields = $ this ->getUniqueFields ($ tableName , $ noConstraintFields ['mix ' ]);
138
+ if (in_array ($ table ->COLUMN_NAME , $ fields )) {
139
+ $ uniqueKey = '<img src= ' . asset ("auditor/icon/add.svg " ) . ' alt="key" class="m-auto add-constraint- ' . $ table ->COLUMN_NAME . '- ' . Constant::CONSTRAINT_UNIQUE_KEY . '" style="height:20px;cursor: pointer;" onclick="add(` ' . $ table ->COLUMN_NAME . '`, ` ' . Constant::CONSTRAINT_UNIQUE_KEY . '`)"/> ' ;
140
+ }
141
+ }
142
+ break ;
143
+ case Constant::CONSTRAINT_FOREIGN_KEY :
144
+ if (in_array ($ table ->COLUMN_NAME , $ noConstraintFields ['integer ' ])) {
145
+ if (!$ this ->tableHasValue ($ tableName )) {
146
+ $ foreignKey = '<img src= ' . asset ("auditor/icon/add.svg " ) . ' alt="key" class="m-auto add-constraint- ' .$ table ->COLUMN_NAME .'- ' .Constant::CONSTRAINT_FOREIGN_KEY .'" style="height:20px;cursor: pointer;" onclick="add(` ' .$ table ->COLUMN_NAME .'`, ` ' .Constant::CONSTRAINT_FOREIGN_KEY .'`,` ' .$ tableName .'`)"/> ' ;
147
+ }
148
+ }
149
+ break ;
150
+ default :
151
+ break ;
111
152
}
112
153
}
113
154
114
- $ response [] = ["column " => $ table ->COLUMN_NAME , "primaryKey " => $ primaryKey , "indexing " => $ indexing , "uniqueKey " => $ uniqueKey , "foreignKey " => $ foreignKey ];
155
+ $ response [] = ["column " => $ table ->COLUMN_NAME , "primaryKey " => $ primaryKey , "indexing " => $ indexKey , "uniqueKey " => $ uniqueKey , "foreignKey " => $ foreignKey ];
115
156
}
116
157
117
158
return response ()->json (array (
118
159
"data " => $ response
119
160
));
120
161
}
162
+
163
+ /**
164
+ * Update the field Constraint
165
+ * @param Request
166
+ * @return
167
+ */
168
+ public function changeConstraint (Request $ request ): bool
169
+ {
170
+ $ data = $ request ->all ();
171
+ $ this ->addConstraint ($ data ['table_name ' ], $ data ['colum_name ' ], $ data ['constraint ' ]);
172
+ return Constant::STATUS_TRUE ;
173
+ }
174
+
175
+ /**
176
+ * Get Foreign Key Details
177
+ * @return array
178
+ */
179
+ public function getForeignKeyTableList (): array
180
+ {
181
+ return $ this ->getTableList ();
182
+ }
183
+
184
+ /**
185
+ * Get Foreign Key Field List
186
+ * @param string
187
+ * @return array
188
+ */
189
+ public function getForeignKeyFieldList (string $ tableName ): array
190
+ {
191
+ return $ this ->getFieldsDetails ($ tableName );
192
+ }
193
+
194
+ /**
195
+ * Add Foreign Key Constraint
196
+ * @param Request
197
+ * @return mixed
198
+ */
199
+ public function addForeignKeyConstraint (Request $ request ): mixed
200
+ {
201
+ $ data = $ request ->all ();
202
+
203
+ if ($ data ['reference_table ' ] === $ data ['table_name ' ]) {
204
+ return __ ('Lang::messages.constraint.error_message.foreign_selected_table_match ' , ['foreign ' => $ data ['reference_table ' ], 'selected ' => $ data ['table_name ' ]]);
205
+ }
206
+
207
+ $ referenceFieldType = $ this ->getFieldDataType ($ data ['reference_table ' ], $ data ['reference_field ' ]);
208
+ $ selectedFieldType = $ this ->getFieldDataType ($ data ['table_name ' ], $ data ['select_field ' ]);
209
+ if ($ referenceFieldType ['data_type ' ] !== $ selectedFieldType ['data_type ' ]) {
210
+ return __ ('Lang::messages.constraint.error_message.foreign_not_apply ' );
211
+ }
212
+
213
+ $ this ->addConstraint ($ data ['table_name ' ], $ data ['select_field ' ], Constant::CONSTRAINT_FOREIGN_KEY , $ data ['reference_table ' ], $ data ['reference_field ' ]);
214
+ return Constant::STATUS_TRUE ;
215
+ }
121
216
}
0 commit comments