Passed
Branch 0.2 (92feb3)
by Lars
03:29
created

CommentController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\BlogArticle;
6
use App\Comments;
7
use App\Helper\FormatHelper;
8
use Illuminate\Http\Request;
9
10
/**
11
 * Class CommentController
12
 *
13
 * @package App\Http\Controllers
14
 */
15
class CommentController extends Controller
16
{
17
    public function index($articleHash)
18
    {
19
        $article = new BlogArticle();
20
        $comment = new Comments();
21
        $articleResult = $article->where("hash", $articleHash)->first();
22
23
        if ($articleResult != null) {
24
            $articleResult["comments"] = $comment->where("articleHash", $articleHash)->orderBy("created_at")->get();
25
26
            return FormatHelper::formatData($articleResult);
27
        } else {
28
            return FormatHelper::formatData(array("errorCode" => "not-found"), false, 404);
29
        }
30
    }
31
32
    public function getComment($commentHash)
33
    {
34
        $comment = new Comments();
35
36
        $commentResult = $comment->where("hash", $commentHash)->first();
37
38
        if ($commentResult != null) {
39
            return FormatHelper::formatData($commentResult);
40
        } else {
41
            return FormatHelper::formatData(array("errorCode" => "not-found"), false, 404);
42
        }
43
    }
44
45
    public function addComment(Request $request)
46
    {
47
        $article = new BlogArticle();
48
        $comment = new Comments();
49
50
        $blogHash = $request->input("blogHash");
51
        $articleHash = $request->input("articleHash");
52
53
        $articleTitle = $request->input("articleTitle");
54
        $articleAuthor = $request->input("articleAuthor");
55
        $articleUrl = $request->input("articleUrl");
56
57
        $authorName = $request->input("authorName");
58
        $authorMail = $request->input("authorMail");
59
        $title = $request->input("title");
60
        $content = $request->input("content");
61
62
        $articleResult = $article->where("hash", $articleHash)->where("blogHash", $blogHash)->first();
63
64
        if ($articleResult != null) {
65
            $commentHash = md5(time());
66
            $dataArray = array(
67
                "hash" => $commentHash,
68
                "articleHash" => $articleHash,
69
                "authorName" => $authorName,
70
                "authorMail" => $authorMail,
71
                "title" => $title,
72
                "content" => $content
73
            );
74
            $comment->create($dataArray);
75
76
            return $this->getComment($commentHash);
77
        } else {
78
            $articleHash = md5(time());
79
            $dataArray = array(
80
                "hash" => $articleHash,
81
                "blogHash" => $blogHash,
82
                "title" => $articleTitle,
83
                "author" => $articleAuthor,
84
                "url" => $articleUrl
85
            );
86
            $article->create($dataArray);
87
88
            $commentHash = md5(time());
89
            $dataArray = array(
90
                "hash" => $commentHash,
91
                "articleHash" => $articleHash,
92
                "authorName" => $authorName,
93
                "authorMail" => $authorMail,
94
                "title" => $title,
95
                "content" => $content
96
            );
97
            $comment->create($dataArray);
98
99
            return $this->getComment($commentHash);
100
        }
101
    }
102
103
    public function editComment(Request $request, $commentHash)
104
    {
105
        $comment = new Comments();
106
107
        $authorName = $request->input("authorName");
108
        $authorMail = $request->input("authorMail");
109
        $title = $request->input("title");
110
        $content = $request->input("content");
111
112
        $dataArray = array();
113
114
        if ($authorName != null) {
115
            $dataArray["authorName"] = $authorName;
116
        }
117
118
        if ($authorMail != null) {
119
            $dataArray["authorMail"] = $authorMail;
120
        }
121
122
        if ($title != null) {
123
            $dataArray["title"] = $title;
124
        }
125
126
        if ($content != null) {
127
            $dataArray["content"] = $content;
128
        }
129
130
        $comment->where("hash", $commentHash)->update($dataArray);
131
132
        return $this->getComment($commentHash);
133
    }
134
135
    public function deleteComment($commentHash)
136
    {
137
        $comment = new Comments();
138
        $commentResult = $comment->where("hash", $commentHash)->first();
139
        if ($commentResult != null) {
140
            $comment->where("hash", $commentHash)->delete();
141
            return FormatHelper::formatData(array("errorCode" => "comment-deleted"), true);
142
        } else {
143
            return FormatHelper::formatData(array("errorCode" => "not-found"), false, 404);
144
        }
145
    }
146
}