55use App \Blog ;
66use App \BlogArticle ;
77use App \Helper \FormatHelper ;
8+ use Illuminate \Http \Request ;
89
910/**
1011 * Class BlogController
@@ -17,25 +18,86 @@ public function index()
1718 return FormatHelper::formatData (Blog::all ());
1819 }
1920
20- public function getBlog ($ blogHash )
21+ public function getBlogByHash ($ blogHash )
2122 {
2223 $ blog = new Blog ();
23- $ return = $ blog ->where ("hash " , $ blogHash )->first ();
24- return $ return != null ? FormatHelper::formatData ($ return ) : FormatHelper::formatData (array (), FALSE );
24+ $ blogResult = $ blog ->where ("hash " , $ blogHash )->first ();
25+
26+ if ($ blogResult != null ) {
27+ return FormatHelper::formatData ($ blogResult );
28+ } else {
29+ return FormatHelper::formatData (array ("errorCode " => "blog-not-found " ), false , 404 );
30+ }
2531 }
2632
27- public function getBlogWithArticles ($ blogHash )
33+ public function getArticle ($ articleHash )
34+ {
35+ $ article = new BlogArticle ();
36+ $ articleResult = $ article ->where ("hash " , $ articleHash )->first ();
37+
38+ if ($ articleResult != null ) {
39+ return FormatHelper::formatData ($ articleResult );
40+ } else {
41+ return FormatHelper::formatData (array ("errorCode " => "article-not-found " ), false , 404 );
42+ }
43+ }
44+
45+ public function addBlog (Request $ request )
2846 {
2947 $ blog = new Blog ();
30- $ blogResult = $ blog ->where ("hash " , $ blogHash )->first ();
3148
32- if ( $ blogResult != null ) {
33- $ blogArticle = new BlogArticle ( );
34- $ blogResult [ " articles " ] = $ blogArticle -> where ( " blogId " , $ blogResult [ " id " ])-> get ( );
49+ $ name = $ request -> input ( " name " );
50+ $ description = $ request -> input ( " description " );
51+ $ url = $ request -> input ( " url " );
3552
36- return $ blogResult != null ? FormatHelper::formatData ($ blogResult ) : FormatHelper::formatData (array (), FALSE );
53+ $ blogHash = md5 (time ());
54+ $ dataArray = array (
55+ "hash " => $ blogHash ,
56+ "name " => $ name ,
57+ "description " => $ description ,
58+ "url " => $ url
59+ );
60+ $ blog ->create ($ dataArray );
61+ return $ dataArray ;
62+ }
63+
64+ public function editBlog (Request $ request )
65+ {
66+ $ blog = new Blog ();
67+
68+ $ blogHash = $ request ->input ("hash " );
69+ $ name = $ request ->input ("name " );
70+ $ description = $ request ->input ("description " );
71+ $ url = $ request ->input ("url " );
72+
73+ $ dataArray = array ();
74+
75+ if ($ name != null ) {
76+ $ dataArray ["name " ] = $ name ;
77+ }
78+
79+ if ($ description != null ) {
80+ $ dataArray ["description " ] = $ description ;
81+ }
82+
83+ if ($ url != null ) {
84+ $ dataArray ["url " ] = $ url ;
85+ }
86+
87+ $ blog ->where ("hash " , $ blogHash )->update ($ dataArray );
88+
89+ return $ dataArray ;
90+ }
91+
92+ public function deleteBlog ($ blogHash )
93+ {
94+ $ blog = new Blog ();
95+ $ blogResult = $ blog ->where ("hash " , $ blogHash )->first ();
96+ if ($ blogResult != null ) {
97+ $ blogResult ->delete ();
98+ return FormatHelper::formatData (array (), true );
3799 } else {
38- return FormatHelper::formatData (array (), false );
100+ return FormatHelper::formatData (array (" errorCode " => " blog-not-found " ), false , 404 );
39101 }
40102 }
41103}
0 commit comments