Completed
Push — master ( e38c72...0dd5d2 )
by Jianhua
03:17
created

EntityRepository::list()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 2
dl 0
loc 23
rs 9.6333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author  Eddy <[email protected]>
4
 */
5
6
namespace App\Repository\Admin;
7
8
use App\Model\Admin\Entity;
9
use App\Repository\Searchable;
10
use Illuminate\Support\Facades\DB;
11
12
class EntityRepository
13
{
14
    use Searchable;
15
16
    public static function list($perPage, $condition = [])
17
    {
18
        $data = Entity::query()
19
            ->where(function ($query) use ($condition) {
20
                Searchable::buildQuery($query, $condition);
21
            })
22
            ->orderBy('id', 'desc')
23
            ->paginate($perPage);
24
        $data->transform(function ($item) {
25
            xssFilter($item);
26
            $item->editUrl = route('admin::entity.edit', ['id' => $item->id]);
27
            $item->deleteUrl = route('admin::entity.delete', ['id' => $item->id]);
28
            $item->fieldUrl = route('admin::entityField.index') . '?entity_id=' . $item->id;
29
            $item->contentUrl = route('admin::content.index', ['entity' => $item->id]);
30
            $item->commentListUrl = route('admin::comment.index', ['entity_id' => $item->id]);
31
            return $item;
32
        });
33
34
        return [
35
            'code' => 0,
36
            'msg' => '',
37
            'count' => $data->total(),
38
            'data' => $data->items(),
39
        ];
40
    }
41
42
    public static function add($data)
43
    {
44
        Entity::query()->create($data);
45
        $sql = <<<"EOF"
46
CREATE TABLE `{$data['table_name']}` (
47
`id`  int(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
48
`created_at`  timestamp NULL DEFAULT NULL ,
49
`updated_at`  timestamp NULL DEFAULT NULL ,
50
PRIMARY KEY (`id`)
51
)
52
ENGINE=InnoDB
53
DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci
54
;
55
EOF;
56
        DB::statement($sql);
57
    }
58
59
    public static function update($id, $data)
60
    {
61
        return Entity::query()->where('id', $id)->update($data);
62
    }
63
64
    public static function find($id)
65
    {
66
        return Entity::query()->find($id);
67
    }
68
69
    public static function external($id)
70
    {
71
        return Entity::query()->External()->find($id);
72
    }
73
74
    public static function all()
75
    {
76
        return Entity::query()->get();
77
    }
78
79
    public static function systemMenu()
80
    {
81
        $entities = Entity::query()->pluck('name', 'id')->all();
82
        $autoMenu = [];
83
        foreach ($entities as $k => $v) {
84
            $autoMenu[] = [
85
                'url' => route('admin::content.index', ['entity' => $k]),
86
                'name' => $v,
87
            ];
88
        }
89
90
        return $autoMenu;
91
    }
92
}
93