Skip to content
Permalink
Browse files

start to use Vue!

api: post-management
  • Loading branch information...
movsb committed Aug 15, 2018
1 parent 076db56 commit 98673f066ee0ddf7f5398a3505d7d08d38bac637
Showing with 93 additions and 55 deletions.
  1. +0 −27 admin/models/posts.php
  2. +24 −28 admin/post-manage.php
  3. +9 −0 server/main.go
  4. +60 −0 server/post-manager.go
@@ -793,31 +793,4 @@ public function increase_page_view_count(int $pid) {
$sql = "UPDATE posts SET page_view=page_view+1 WHERE id=".$pid." LIMIT 1";
$tbdb->query($sql);
}
// 获取所有文章,给文章管理用
public function get_all_posts_for_manage() {
global $tbdb;
$sql = array();
$sql['select'] = 'id,date,modified,title,page_view,source_type,comments';
$sql['from'] = 'posts';
$sql['where'][] = "type='post'";
$sql['orderby'] = 'id DESC';
$sql = $this->before_posts_query($sql);
$sql = make_query_string($sql);
$rows = $tbdb->query($sql);
if(!$rows) return [];
$posts = [];
while($r = $rows->fetch_object()) {
$posts[] = $r;
}
$posts = $this->after_posts_query($posts);
return $posts;
}
}
@@ -1,7 +1,5 @@
<?php
if($_SERVER['REQUEST_METHOD'] === 'GET') :
require_once('admin.php');
function postmanage_admin_head() {
@@ -24,6 +22,8 @@ function postmanage_admin_head() {
</style>

<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<?php }
add_hook('admin_head', 'postmanage_admin_head');
@@ -32,7 +32,7 @@ function postmanage_admin_head() {
?>

<table>
<table id="table">
<thead>
<tr>
<th>编号</th>
@@ -46,33 +46,29 @@ function postmanage_admin_head() {
</tr>
</thead>
<tbody>
<?php
$posts = $tbpost->get_all_posts_for_manage();
foreach($posts as $p) {
echo '<tr>',
'<td>',$p->id,'</td>',
'<td class="title">',htmlspecialchars($p->title),'</td>',
'<td>',$p->date,'</td>',
'<td>',$p->modified,'</td>',
'<td>',$p->page_view,'</td>',
'<td>',$p->comments,'</td>',
'<td>',htmlspecialchars($p->source_type),'</td>',
'<td>',the_edit_link($p, true, true),'</td>',
'</tr>'
;
}
?>
<tr v-for="p in posts">
<td>{{ p.id }}</td>
<td>{{ p.title }}</td>
<td>{{ p.date }}</td>
<td>{{ p.modified }}</td>
<td>{{ p.page_view }}</td>
<td>{{ p.comment_count }}</td>
<td>{{ p.source_type }}</td>
<td><a target=_blank v-bind:href="'/admin/post.php?do=edit&amp;id=' + p.id">编辑</a></td>
</tr>
</tbody>
</table>

<script>
$.get('/v1/posts!manage', function(posts) {
new Vue({
el: '#table',
data: {
posts: posts,
},
})
});
</script>

<?php
admin_footer();
die(0);
else : // POST
die(0);
endif;
@@ -399,6 +399,15 @@ func routerV1(router *gin.Engine) {
EndReq(c, nil, nil)
})

v1.GET("/posts!manage", func(c *gin.Context) {
if !auth(c, true) {
return
}

posts, err := postmgr.GetPostsForManagement(gdb)
EndReq(c, err, posts)
})

archives := v1.Group("/archives")

archives.GET("/categories/:name", func(c *gin.Context) {
@@ -17,6 +17,37 @@ type PostForArchiveQuery struct {
Title string `json:"title"`
}

// PostForManagement for post management.
type PostForManagement struct {
ID int64 `json:"id"`
Date string `json:"date"`
Modified string `json:"modified"`
Title string `json:"title"`
PageView uint `json:"page_view"`
SourceType string `json:"source_type"`
CommentCount uint `json:"comment_count"`
}

func (p *PostForManagement) Fields() string {
cols := "id,date,modified,title,page_view,source_type,comments"
return cols
}

func (p *PostForManagement) Pointers() []interface{} {
ptrs := []interface{}{
&p.ID,
&p.Date, &p.Modified,
&p.Title, &p.PageView,
&p.SourceType, &p.CommentCount,
}
return ptrs
}

func (p *PostForManagement) ToLocalTime() {
p.Date = datetime.My2Local(p.Date)
p.Modified = datetime.My2Local(p.Modified)
}

// PostManager manages posts.
type PostManager struct {
}
@@ -198,3 +229,32 @@ func (z *PostManager) GetVars(tx Querier, fields string, wheres string, outs ...

return row.Scan(outs...)
}

func (z *PostManager) GetPostsForManagement(tx Querier) ([]*PostForManagement, error) {
var dummy PostForManagement
cols := dummy.Fields()
q := make(map[string]interface{})
q["select"] = cols
q["from"] = "posts"
q["where"] = []string{"type='post'"}
q["orderby"] = "id DESC"

query := BuildQueryString(q)
rows, err := tx.Query(query)
if err != nil {
return nil, err
}

posts := make([]*PostForManagement, 0)

for rows.Next() {
var post PostForManagement
if err := rows.Scan(post.Pointers()...); err != nil {
return nil, err
}
post.ToLocalTime()
posts = append(posts, &post)
}

return posts, nil
}

0 comments on commit 98673f0

Please sign in to comment.
You can’t perform that action at this time.