Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
39 / 39 |
DictionaryController | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
14 | |
100.00% |
39 / 39 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
getFormats | |
100.00% |
1 / 1 |
1 | |
100.00% |
7 / 7 |
|||
getFieldType | |
100.00% |
1 / 1 |
2 | |
100.00% |
12 / 12 |
|||
index | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
update | |
100.00% |
1 / 1 |
9 | |
100.00% |
17 / 17 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\ConsumerDataDictionary\Controllers; |
4 | |
5 | use Illuminate\Http\Response; |
6 | use Illuminate\Http\Request; |
7 | |
8 | use Illuminate\Support\Facades\Log; |
9 | use Illuminate\Support\Facades\Redis; |
10 | |
11 | use Qmp\Laravel\ConsumerDataDictionary\Models\Dictionary; |
12 | use Qmp\Laravel\MicroService\Controllers\AbstractMicroServiceController; |
13 | |
14 | |
15 | class DictionaryController extends AbstractMicroServiceController |
16 | { |
17 | public function __construct(Request $request) |
18 | { |
19 | parent::__construct($request); |
20 | } |
21 | |
22 | public function getFormats(Request $request) |
23 | { |
24 | $dictionary = Dictionary::where('key', 'exists', true) |
25 | ->where('text', 'exists', true) |
26 | ->where('format', 'exists', true) |
27 | ->project(['_id' => 0, 'key' => 1, 'value' => 1, 'text' => 1, 'format' => 1]) |
28 | ->get() |
29 | ->eachAppend('value'); |
30 | |
31 | return response()->json($dictionary); |
32 | } |
33 | |
34 | public function getFieldType(Request $request) |
35 | { |
36 | $dictionary = collect(Dictionary::where('key', 'exists', true) |
37 | ->where('field_type', 'exists', true) |
38 | ->where('group', 'exists', true) |
39 | ->select(['field_type', 'group', 'format.values']) |
40 | ->get() |
41 | ->map(function($data) { |
42 | return [ |
43 | 'key' => $data->group . '|' . $data->field_type, |
44 | 'format' => !empty($data->format['values']) ? $data->format['values'] : null |
45 | ]; |
46 | }) |
47 | ->unique('key') |
48 | ->toArray()); |
49 | |
50 | |
51 | |
52 | return response()->json($dictionary); |
53 | } |
54 | |
55 | public function index(Request $request) |
56 | { |
57 | return response()->json(Dictionary::all()); |
58 | } |
59 | |
60 | public function update(Request $request) |
61 | { |
62 | $request->validate([ |
63 | 'key' => 'required', |
64 | 'format.values' => 'required' |
65 | ]); |
66 | |
67 | $formatValues = []; |
68 | if (isset($request->format['values'][1])) { |
69 | $formatValues["1"] = $request->format['values'][1]; |
70 | } |
71 | if (isset($request->format['values']["1"])) { |
72 | $formatValues["1"] = $request->format['values'][1]; |
73 | } |
74 | |
75 | foreach($request->format['values'] as $key => $value) { |
76 | if (!($key === 1 || $key === "1")) { |
77 | if ($key === 0 || $key === "0" || strlen($value) > 0) { |
78 | $formatValues[(string) $key] = $value; |
79 | } |
80 | } |
81 | } |
82 | |
83 | $item = Dictionary::where('key', $request->key)->first(); |
84 | $format = $item->format; |
85 | $format['values'] = $formatValues; |
86 | $item->format = $format; |
87 | $item->save(); |
88 | |
89 | return response()->json(['status' => 'ok']); |
90 | } |
91 | } |