chore: add api

This commit is contained in:
deepzz0
2021-04-27 23:46:43 +08:00
parent a39e3aac3b
commit 4749b1e681
7 changed files with 469 additions and 74 deletions

View File

@@ -3,13 +3,17 @@ package admin
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"time"
"github.com/eiblog/eiblog/pkg/cache"
"github.com/eiblog/eiblog/pkg/config"
"github.com/eiblog/eiblog/pkg/core/blog"
"github.com/eiblog/eiblog/pkg/internal"
"github.com/eiblog/eiblog/pkg/model"
"github.com/eiblog/eiblog/tools"
"github.com/gin-gonic/gin"
@@ -176,29 +180,229 @@ func handleAPIPassword(c *gin.Context) {
// handleAPIPostDelete 删除文章,移入回收箱
func handleAPIPostDelete(c *gin.Context) {
// var ids []int32
// for _, v := range c.PostFormArray("cid[]") {
// i, err := strconv.Atoi(v)
// if err != nil || int32(i) < config.Conf.BlogApp.General.StartID {
// responseNotice(c, NoticeNotice, "参数错误", "")
// return
// }
// ids = append(ids, int32(i))
// }
// err := DelArticles(ids...)
// if err != nil {
// logd.Error(err)
// responseNotice(c, NOTICE_NOTICE, err.Error(), "")
// return
// }
//
// // elasticsearch
// err = ElasticDelIndex(ids)
// if err != nil {
// logrus.Error("handleAPIPostDelete.")
// }
// // TODO disqus delete
// responseNotice(c, NoticeSuccess, "删除成功", "")
var ids []int
for _, v := range c.PostFormArray("cid[]") {
id, err := strconv.Atoi(v)
if err != nil || id < config.Conf.BlogApp.General.StartID {
responseNotice(c, NoticeNotice, "参数错误", "")
return
}
ids = append(ids, id)
}
err := cache.Ei.DeleteArticles(ids)
if err != nil {
logrus.Error("handleAPIPostDelete.DeleteArticles: ", err)
responseNotice(c, NoticeNotice, err.Error(), "")
return
}
// elasticsearch
err = internal.ElasticDelIndex(ids)
if err != nil {
logrus.Error("handleAPIPostDelete.ElasticDelIndex: ", err)
}
// TODO disqus delete
responseNotice(c, NoticeSuccess, "删除成功", "")
}
// handleAPIPostCreate 创建文章
func handleAPIPostCreate(c *gin.Context) {
var (
err error
do string
cid int
)
defer func() {
switch do {
case "auto": // 自动保存
if err != nil {
c.JSON(http.StatusOK, gin.H{"fail": 1, "time": time.Now().Format("15:04:05 PM"), "cid": cid})
return
}
c.JSON(http.StatusOK, gin.H{"success": 0, "time": time.Now().Format("15:04:05 PM"), "cid": cid})
case "save", "publish": // 草稿,发布
if err != nil {
responseNotice(c, NoticeNotice, err.Error(), "")
return
}
uri := "/admin/manage-draft"
if do == "publish" {
uri = "/admin/manage-posts"
}
c.Redirect(http.StatusFound, uri)
}
}()
do = c.PostForm("do") // auto or save or publish
slug := c.PostForm("slug")
title := c.PostForm("title")
text := c.PostForm("text")
date := parseLocationDate(c.PostForm("date"))
serie := c.PostForm("serie")
tag := c.PostForm("tags")
update := c.PostForm("update")
if slug == "" || title == "" || text == "" {
err = errors.New("参数错误")
return
}
serieid, _ := strconv.Atoi(serie)
article := &model.Article{
Title: title,
Content: text,
Slug: slug,
IsDraft: do != "publish",
Author: cache.Ei.Account.Username,
SerieID: serieid,
Tags: tag,
CreatedAt: date,
}
cid, err = strconv.Atoi(c.PostForm("cid"))
// 新文章
if err != nil || cid < 1 {
err = cache.Ei.AddArticle(article)
if err != nil {
logrus.Error("handleAPIPostCreate.InsertArticle: ", err)
return
}
if !article.IsDraft {
// 异步执行,快
go func() {
// elastic
internal.ElasticAddIndex(article)
// rss
internal.PingFunc(cache.Ei.Blogger.BTitle, slug)
// disqus
internal.ThreadCreate(article, cache.Ei.Blogger.BTitle)
}()
}
return
}
// 旧文章
article.ID = cid
artc, _ := cache.Ei.FindArticleByID(article.ID)
if artc != nil {
article.IsDraft = false
article.Count = artc.Count
article.UpdatedAt = artc.UpdatedAt
}
if update == "true" || update == "1" {
artc.UpdatedAt = time.Now()
}
// 数据库更新
err = cache.Ei.UpdateArticle(context.Background(), artc.ID, map[string]interface{}{
"title": article.Title,
"content": article.Content,
"serie_id": article.SerieID,
"tags": article.Tags,
"is_draft": article.IsDraft,
"updated_at": article.UpdatedAt,
"created_at": article.CreatedAt,
})
if err != nil {
logrus.Error("handleAPIPostCreate.UpdateArticle: ", err)
return
}
if !artc.IsDraft {
cache.Ei.ReplaceArticle(artc, article)
// 异步执行,快
go func() {
// elastic
internal.ElasticAddIndex(article)
// rss
internal.PingFunc(cache.Ei.Blogger.BTitle, slug)
// disqus
if artc == nil {
internal.ThreadCreate(article, cache.Ei.Blogger.BTitle)
}
}()
}
}
// handleAPISerieDelete 只能逐一删除,专题下不能有文章
func handleAPISerieDelete(c *gin.Context) {
for _, v := range c.PostFormArray("mid[]") {
id, err := strconv.Atoi(v)
if err != nil || id < 1 {
responseNotice(c, NoticeNotice, err.Error(), "")
return
}
for i, serie := range cache.Ei.Series {
if serie.ID == id {
if len(serie.Articles) > 0 {
logrus.Error("handleAPISerieDelete.failed: ")
responseNotice(c, NoticeNotice, "请删除该专题下的所有文章", "")
return
}
err = cache.Ei.RemoveSerie(context.Background(), id)
if err != nil {
logrus.Error("handleAPISerieDelete.RemoveSerie: ")
responseNotice(c, NoticeNotice, err.Error(), "")
return
}
cache.Ei.Series[i] = nil
cache.Ei.Series = append(cache.Ei.Series[:i], cache.Ei.Series[i+1:]...)
cache.PagesCh <- cache.PageSeries
break
}
}
}
responseNotice(c, NoticeSuccess, "删除成功", "")
}
// handleAPISerieCreate 添加专题,如果专题有提交 mid 即更新专题
func handleAPISerieCreate(c *gin.Context) {
name := c.PostForm("name")
slug := c.PostForm("slug")
desc := c.PostForm("description")
if name == "" || slug == "" || desc == "" {
responseNotice(c, NoticeNotice, "参数错误", "")
return
}
mid, err := strconv.Atoi(c.PostForm("mid"))
if err == nil && mid > 0 {
var serie *model.Serie
for _, v := range cache.Ei.Series {
if v.ID == mid {
serie = v
break
}
}
if serie == nil {
responseNotice(c, NoticeNotice, "专题不存在", "")
return
}
err = cache.Ei.UpdateSerie(context.Background(), mid, map[string]interface{}{
"slug": slug,
"name": name,
"desc": desc,
})
if err != nil {
logrus.Error("handleAPISerieCreate.UpdateSerie: ", err)
responseNotice(c, NoticeNotice, err.Error(), "")
return
}
} else {
err = cache.Ei.InsertSerie(context.Background(), &model.Serie{
Slug: slug,
Name: name,
Desc: desc,
})
if err != nil {
logrus.Error("handleAPISerieCreate.InsertSerie: ", err)
responseNotice(c, NoticeNotice, err.Error(), "")
return
}
}
responseNotice(c, NoticeSuccess, "操作成功", "")
}
// parseLocationDate 解析日期
func parseLocationDate(date string) time.Time {
t, err := time.ParseInLocation("2006-01-02 15:04", date, time.Local)
if err == nil {
return t
}
return time.Now()
}
func responseNotice(c *gin.Context, typ, content, hl string) {

View File

@@ -73,7 +73,7 @@ func handleAdminPost(c *gin.Context) {
params["Domain"] = config.Conf.BlogApp.Host
params["Series"] = cache.Ei.Series
var tags []T
for tag, _ := range cache.Ei.TagArticles {
for tag := range cache.Ei.TagArticles {
tags = append(tags, T{tag, tag})
}
str, _ := json.Marshal(tags)