1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Blog; |
6
|
|
|
use App\BlogArticle; |
7
|
|
|
use App\Helper\FormatHelper; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class BlogController |
12
|
|
|
* @package App\Http\Controllers |
13
|
|
|
*/ |
14
|
|
|
class BlogController extends Controller |
15
|
|
|
{ |
16
|
|
|
public function index() |
17
|
|
|
{ |
18
|
|
|
return FormatHelper::formatData(Blog::all()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function getBlogWithArticles($blogHash) |
22
|
|
|
{ |
23
|
|
|
$blog = new Blog(); |
24
|
|
|
$blogResult = $blog->where("hash", $blogHash)->first(); |
25
|
|
|
|
26
|
|
|
if ($blogResult != null) { |
27
|
|
|
$blogArticle = new BlogArticle(); |
28
|
|
|
$blogResult["articles"] = $blogArticle->where("blogHash", $blogHash)->orderBy("created_at")->get(); |
29
|
|
|
|
30
|
|
|
return FormatHelper::formatData($blogResult); |
31
|
|
|
} else { |
32
|
|
|
return FormatHelper::formatData(array("errorCode" => "blog-not-found"), false, 404); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getArticle($articleHash) |
37
|
|
|
{ |
38
|
|
|
$article = new BlogArticle(); |
39
|
|
|
$articleResult = $article->where("hash", $articleHash)->first(); |
40
|
|
|
|
41
|
|
|
if ($articleResult != null) { |
42
|
|
|
return FormatHelper::formatData($articleResult); |
43
|
|
|
} else { |
44
|
|
|
return FormatHelper::formatData(array("errorCode" => "article-not-found"), false, 404); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function addBlog(Request $request) |
49
|
|
|
{ |
50
|
|
|
$blog = new Blog(); |
51
|
|
|
|
52
|
|
|
$name = $request->input("name"); |
53
|
|
|
$description = $request->input("description"); |
54
|
|
|
$url = $request->input("url"); |
55
|
|
|
|
56
|
|
|
$blogHash = md5(time()); |
57
|
|
|
$dataArray = array( |
58
|
|
|
"hash" => $blogHash, |
59
|
|
|
"name" => $name, |
60
|
|
|
"description" => $description, |
61
|
|
|
"url" => $url |
62
|
|
|
); |
63
|
|
|
$blog->create($dataArray); |
64
|
|
|
return $dataArray; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function editBlog(Request $request) |
68
|
|
|
{ |
69
|
|
|
$blog = new Blog(); |
70
|
|
|
|
71
|
|
|
$blogHash = $request->input("hash"); |
72
|
|
|
$name = $request->input("name"); |
73
|
|
|
$description = $request->input("description"); |
74
|
|
|
$url = $request->input("url"); |
75
|
|
|
|
76
|
|
|
$dataArray = array(); |
77
|
|
|
|
78
|
|
|
if ($name != null) { |
79
|
|
|
$dataArray["name"] = $name; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($description != null) { |
83
|
|
|
$dataArray["description"] = $description; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($url != null) { |
87
|
|
|
$dataArray["url"] = $url; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$blog->where("hash", $blogHash)->update($dataArray); |
91
|
|
|
|
92
|
|
|
return $dataArray; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function deleteBlog($blogHash) |
96
|
|
|
{ |
97
|
|
|
$blog = new Blog(); |
98
|
|
|
$blogResult = $blog->where("hash", $blogHash)->first(); |
99
|
|
|
if ($blogResult != null) { |
100
|
|
|
$blogResult->delete(); |
101
|
|
|
return FormatHelper::formatData(array(), true); |
102
|
|
|
} else { |
103
|
|
|
return FormatHelper::formatData(array("errorCode" => "blog-not-found"), false, 404); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |