Passed
Push — master ( e20bb5...b3a0d1 )
by Lars
04:05
created

CommentController::getAllComments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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)
104
    {
105
        $comment = new Comments();
106
107
        $commentHash = $request->input("hash");
108
        $authorName = $request->input("authorName");
109
        $authorMail = $request->input("authorMail");
110
        $title = $request->input("title");
111
        $content = $request->input("content");
112
113
        $dataArray = array();
114
115
        if ($authorName != null) {
116
            $dataArray["authorName"] = $authorName;
117
        }
118
119
        if ($authorMail != null) {
120
            $dataArray["authorMail"] = $authorMail;
121
        }
122
123
        if ($title != null) {
124
            $dataArray["title"] = $title;
125
        }
126
127
        if ($content != null) {
128
            $dataArray["content"] = $content;
129
        }
130
131
        $comment->where("hash", $commentHash)->update($dataArray);
132
133
        return $this->getComment($commentHash);
134
    }
135
136
    public function deleteComment($commentHash)
137
    {
138
        $comment = new Comments();
139
        $commentResult = $comment->where("hash", $commentHash)->first();
140
        if ($commentResult != null) {
141
            $comment->where("hash", $commentHash)->delete();
142
            return FormatHelper::formatData(array("errorCode" => "comment-deleted"), true);
143
        } else {
144
            return FormatHelper::formatData(array("errorCode" => "not-found"), false, 404);
145
        }
146
    }
147
148
    public function getAllComments()
149
    {
150
        return FormatHelper::formatData(Comments::all());
151
    }
152
}