Completed
Push — master ( 1ab03c...42dba5 )
by Jianhua
03:33
created

CommentController::save()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author  Eddy <[email protected]>
4
 */
5
6
namespace App\Http\Controllers\Admin;
7
8
use App\Http\Controllers\Controller;
9
use App\Http\Requests\Admin\CommentRequest;
10
use App\Repository\Admin\CommentRepository;
11
use Illuminate\Database\QueryException;
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Str;
14
use Illuminate\View\View;
15
16
class CommentController extends Controller
17
{
18
    protected $formNames = ['content', 'content_id', 'entity_id', 'user_id'];
19
20
    public function __construct()
21
    {
22
        parent::__construct();
23
24
        $this->breadcrumb[] = ['title' => '评论列表', 'url' => route('admin::comment.index')];
25
    }
26
27
    /**
28
     * 评论管理-评论列表
29
     *
30
     */
31
    public function index()
32
    {
33
        $this->breadcrumb[] = ['title' => '评论列表', 'url' => ''];
34
        return view('admin.comment.index', ['breadcrumb' => $this->breadcrumb]);
35
    }
36
37
    /**
38
     * 评论列表数据接口
39
     *
40
     * @param Request $request
41
     * @return array
42
     */
43
    public function list(Request $request)
44
    {
45
        $perPage = (int) $request->get('limit', 50);
46
        $condition = $request->only($this->formNames);
47
48
        $data = CommentRepository::list($perPage, $condition);
49
50
        return $data;
51
    }
52
53
    /**
54
     * 评论管理-编辑评论
55
     *
56
     * @param int $id
57
     * @return View
58
     */
59
    public function edit($id)
60
    {
61
        $this->breadcrumb[] = ['title' => '编辑评论', 'url' => ''];
62
63
        $model = CommentRepository::find($id);
64
        return view('admin.comment.add', ['id' => $id, 'model' => $model, 'breadcrumb' => $this->breadcrumb]);
65
    }
66
67
    /**
68
     * 评论管理-更新评论
69
     *
70
     * @param CommentRequest $request
71
     * @param int $id
72
     * @return array
73
     */
74
    public function update(CommentRequest $request, $id)
75
    {
76
        $data = $request->only($this->formNames);
77
        try {
78
            CommentRepository::update($id, $data);
79
            return [
80
                'code' => 0,
81
                'msg' => '编辑成功',
82
                'redirect' => true
83
            ];
84
        } catch (QueryException $e) {
85
            return [
86
                'code' => 1,
87
                'msg' => '编辑失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前评论已存在' : '其它错误'),
88
                'redirect' => false
89
            ];
90
        }
91
    }
92
93
    /**
94
     * 评论管理-删除评论
95
     *
96
     * @param int $id
97
     */
98
    public function delete($id)
99
    {
100
        try {
101
            CommentRepository::delete($id);
102
            return [
103
                'code' => 0,
104
                'msg' => '删除成功',
105
                'redirect' => route('admin::comment.index')
106
            ];
107
        } catch (\RuntimeException $e) {
108
            return [
109
                'code' => 1,
110
                'msg' => '删除失败:' . $e->getMessage(),
111
                'redirect' => false
112
            ];
113
        }
114
    }
115
}
116