background/controller/blog.go

1139 lines
21 KiB
Go
Raw Normal View History

2019-05-16 10:05:20 +00:00
package controller
import (
2019-05-17 09:45:50 +00:00
"background/db"
2019-05-16 10:05:20 +00:00
"background/logs"
"background/model"
2019-05-17 09:45:50 +00:00
"fmt"
2020-08-23 18:08:26 +00:00
"strconv"
2022-04-13 16:38:16 +00:00
"sync"
2020-08-23 18:08:26 +00:00
2019-05-16 10:05:20 +00:00
"github.com/gin-gonic/gin"
2021-08-01 15:33:55 +00:00
"gopkg.in/olivere/elastic.v7"
2019-06-30 14:59:00 +00:00
"qiniupkg.com/x/log.v7"
2019-05-16 10:05:20 +00:00
)
2022-04-13 16:38:16 +00:00
type DocTree struct{
ID int64 `json:"id"`
Label string `json:"label"`
Children []*DocTree `json:"children"`
}
var gDocMapCache map[int]*DocTree = nil
var gLock sync.Mutex
func init(){
if nil == gDocMapCache{
gDocMapCache = make(map[int]*DocTree)
}
}
func InitDocCache(){
gLock.Lock()
for k := range gDocMapCache {
delete(gDocMapCache, k)
}
articles := []model.Doc{}
e := db.GetMysqlClient().Query2("select id,title,father,level from doc_copy1", &articles)
if nil != e {
logs.Error(e.Error())
return
}
for _,v := range articles{
tmp := new (DocTree)
tmp.ID = v.ID
tmp.Label = v.Title
tmp.Children = make([]*DocTree,0)
gDocMapCache[int(v.ID)] = tmp
}
for _,v := range articles{
tmp := new (DocTree)
tmp.ID = v.ID
tmp.Label = v.Title
tmp.Children = make([]*DocTree,0)
gDocMapCache[int(v.ID)] = tmp
}
for _,v := range articles{
if v.Father != 0{
gDocMapCache[int(v.Father)].Children = append(gDocMapCache[int(v.Father)].Children, gDocMapCache[int(v.ID)])
}
}
gLock.Unlock()
log.Print(gDocMapCache)
}
2020-08-23 18:08:26 +00:00
func GetPageParaFromQuery(c *gin.Context) (int, int) {
2019-05-17 09:45:50 +00:00
limit := c.Query("limit")
offset := c.Query("offset")
2020-08-23 18:08:26 +00:00
iLmit, e := strconv.Atoi(limit)
if nil != e {
return 0, 0
2019-05-17 09:45:50 +00:00
}
2020-08-23 18:08:26 +00:00
iOffset, e := strconv.Atoi(offset)
if nil != e {
return 0, 0
2019-05-17 09:45:50 +00:00
}
2020-08-23 18:08:26 +00:00
return iLmit, iOffset
2019-05-17 09:45:50 +00:00
}
2020-04-28 04:29:17 +00:00
2022-04-13 16:38:16 +00:00
func GetArticlesTree(c *gin.Context) {
rsp := RespBase{"ERR", -1, nil}
defer func() {
c.JSON(200, rsp)
}()
articles := []model.Doc{}
e := db.GetMysqlClient().Query2("select id,title,father,level from doc_copy1", &articles)
if nil != e {
logs.Error(e.Error())
return
}
InitDocCache()
ret := []*DocTree{}
for _, v := range articles {
if v.Level == 0{
ret = append(ret, gDocMapCache[int(v.ID)])
}
}
rsp.Data = ret
rsp.Msg = "OK"
rsp.Status = 0
}
2020-08-23 18:08:26 +00:00
func GetArticles(c *gin.Context) {
2019-05-17 09:45:50 +00:00
type ReqArticles struct {
Name string `json:"name"`
2020-03-01 14:33:00 +00:00
Type int32 `json:"types"`
2019-05-17 09:45:50 +00:00
}
var req ReqArticles
2020-08-23 18:08:26 +00:00
rsp := RespBase{"ERR", -1, nil}
2019-05-17 09:45:50 +00:00
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, rsp)
2019-05-17 09:45:50 +00:00
}()
e := c.BindJSON(&req)
2020-08-23 18:08:26 +00:00
if nil != e {
2019-05-17 09:45:50 +00:00
logs.Error(e.Error())
return
}
article := []model.Doc{}
2020-08-23 18:08:26 +00:00
limit, offset := GetPageParaFromQuery(c)
2020-04-28 04:29:17 +00:00
public := c.Query("is_public")
2019-05-17 09:45:50 +00:00
var sql string
2020-08-23 18:08:26 +00:00
if req.Name != "" {
if req.Type == 110 {
2020-04-28 04:29:17 +00:00
is_public := 0
2020-08-23 18:08:26 +00:00
if public == "true" {
2020-04-28 04:29:17 +00:00
is_public = 1
2021-02-02 17:47:01 +00:00
} else {
is_public = 0
2020-04-28 04:29:17 +00:00
}
sql = "select * from doc where doc.title like '%%%s%%' and is_public = %d limit %d offset %d "
2020-08-23 18:08:26 +00:00
sql = fmt.Sprintf(sql, req.Name, is_public, limit, offset*limit)
} else {
2020-04-28 04:29:17 +00:00
is_public := 0
2020-06-01 11:40:31 +00:00
if public == "true" {
2020-04-28 04:29:17 +00:00
is_public = 1
2021-02-02 17:47:01 +00:00
} else {
is_public = 0
2020-04-28 04:29:17 +00:00
}
2022-04-12 17:24:39 +00:00
sql = fmt.Sprintf("select * from doc where doc.title like '%%%s%%' and doc.type = %d and is_public = %d limit %d offset %d ",
2020-08-23 18:08:26 +00:00
req.Name, req.Type, is_public, limit, offset*limit)
2020-03-01 14:33:00 +00:00
}
2020-08-23 18:08:26 +00:00
} else {
if req.Type == 110 {
2020-04-28 04:29:17 +00:00
is_public := 0
2020-06-01 11:40:31 +00:00
if public == "true" {
2020-04-28 04:29:17 +00:00
is_public = 1
2020-08-23 18:08:26 +00:00
} else {
2020-04-28 04:29:17 +00:00
is_public = 0
}
2022-04-12 17:24:39 +00:00
sql = fmt.Sprintf("SELECT doc.id,doc.author,LEFT(doc.content,50) as content,doc.title,doc.type from doc where is_public = %d "+
2020-08-23 18:08:26 +00:00
"order by id desc limit %d offset %d", is_public, limit, offset)
} else {
2020-04-28 04:29:17 +00:00
is_public := 0
2020-06-01 11:40:31 +00:00
if public == "true" {
2020-04-28 04:29:17 +00:00
is_public = 1
2020-08-23 18:08:26 +00:00
} else {
is_public = 0
2020-04-28 04:29:17 +00:00
}
2020-08-23 18:08:26 +00:00
sql = fmt.Sprintf("SELECT doc.id,doc.author,LEFT(doc.content,50) as content,doc.title,doc.type from doc where doc.type = %d and is_public = %d "+
" order by id desc limit %d offset %d", req.Type, is_public, limit, offset)
2022-04-12 17:24:39 +00:00
2020-03-01 14:33:00 +00:00
}
2019-05-17 09:45:50 +00:00
}
2020-05-04 08:12:17 +00:00
log.Print(sql)
2020-08-23 18:08:26 +00:00
e = db.GetMysqlClient().Query2(sql, &article)
if nil != e {
logs.Error(e.Error(), sql)
2019-05-17 09:45:50 +00:00
return
}
2020-08-23 18:08:26 +00:00
for k, _ := range article {
article[k].Content += "..."
}
2019-05-17 09:45:50 +00:00
rsp.Data = article
2019-05-17 09:45:50 +00:00
rsp.Status = 0
rsp.Msg = "OK"
}
2019-05-16 10:05:20 +00:00
2020-08-23 18:08:26 +00:00
func GetArticleCount(c *gin.Context) {
resp := RespBase{Msg: "FAIL", Status: 211}
2019-09-04 16:32:39 +00:00
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, resp)
2019-09-04 16:32:39 +00:00
}()
query := fmt.Sprintf("select count(*) as cnt from doc")
type Cnt struct {
Cnt int64 `sql:"cnt" json:"cnt"`
}
2020-08-23 18:08:26 +00:00
cnt := []Cnt{}
e := db.GetMysqlClient().Query2(query, &cnt)
if nil != e {
2019-09-04 16:32:39 +00:00
log.Print(e.Error())
return
}
resp.Data = cnt[0]
resp.Status = 0
resp.Msg = "OK"
}
2020-08-23 18:08:26 +00:00
func GetArticle(c *gin.Context) {
2021-08-25 16:53:20 +00:00
2020-08-23 18:08:26 +00:00
resp := RespBase{Msg: "FAIL", Status: 211}
2019-06-30 14:59:00 +00:00
sid := c.Param("id")
var id int
var err error
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, resp)
2019-06-30 14:59:00 +00:00
}()
2020-08-23 18:08:26 +00:00
if sid == "" {
2019-06-30 14:59:00 +00:00
return
2020-08-23 18:08:26 +00:00
} else {
id, err = strconv.Atoi(sid)
if nil != err {
2019-06-30 14:59:00 +00:00
return
}
}
query := fmt.Sprintf("select * from doc_copy1 where doc_copy1.id = '%d'", id)
2019-06-30 14:59:00 +00:00
docs := []model.Doc{}
2020-08-23 18:08:26 +00:00
e := db.GetMysqlClient().Query2(query, &docs)
if nil != e {
2019-06-30 14:59:00 +00:00
log.Print(e.Error())
return
}
2020-08-23 18:08:26 +00:00
if len(docs) > 0 {
2019-06-30 14:59:00 +00:00
resp.Data = docs[0]
resp.Status = 0
resp.Msg = "OK"
}
}
2020-08-23 18:08:26 +00:00
func UpdateArtilce(c *gin.Context) {
rsp := RespBase{Msg: "FAIL", Status: 210}
2019-08-01 14:52:57 +00:00
type ReqUpdateArticle struct {
2021-03-04 13:26:09 +00:00
ID int64 `json:"id"`
2020-08-23 18:08:26 +00:00
Title string `json:"title"`
2019-08-01 14:52:57 +00:00
Content string `json:"content"`
2021-08-01 15:33:55 +00:00
Author string `json:"author"`
2020-08-23 18:08:26 +00:00
Type int64 `json:"type"`
IsPublic int `json:"is_public"`
2019-08-01 14:52:57 +00:00
}
var req ReqUpdateArticle
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, rsp)
2019-08-01 14:52:57 +00:00
}()
2021-03-04 13:21:12 +00:00
2021-03-04 13:46:04 +00:00
er := c.BindJSON(&req)
if nil != er {
logs.Error(er.Error())
return
}
if req.Title == "" {
rsp.Msg = "title required"
return
}
2021-03-04 13:26:09 +00:00
query := fmt.Sprintf("select * from doc where doc.id = '%d'", req.ID)
2021-03-04 13:21:12 +00:00
docs := []model.Doc{}
e := db.GetMysqlClient().Query2(query, &docs)
if nil != e {
log.Print(e.Error())
return
}
if len(docs) == 0 {
rsp.Msg = "不存在该"
return
}
e = model.UpdateDoc(
2019-08-01 14:52:57 +00:00
model.Doc{
2021-03-04 13:21:12 +00:00
Type: int32(req.Type),
2020-08-23 18:08:26 +00:00
Title: req.Title,
Content: req.Content,
2021-08-01 15:33:55 +00:00
Author: req.Author,
2021-03-04 13:26:09 +00:00
ID: req.ID,
2021-03-04 13:21:12 +00:00
IsPublic: int32(req.IsPublic),
Version: (docs[0].Version + 1),
2019-08-01 14:52:57 +00:00
},
)
2020-08-23 18:08:26 +00:00
if nil != e {
2019-08-01 14:52:57 +00:00
logs.Error(e.Error())
return
}
rsp.Msg = "OK"
rsp.Status = 0
2020-08-23 18:08:26 +00:00
2019-08-01 14:52:57 +00:00
}
2021-08-01 11:50:48 +00:00
func UpdateArtilceTree(c *gin.Context) {
rsp := RespBase{Msg: "FAIL", Status: 210}
type ReqUpdateArticle struct {
ID int64 `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Author string `json:"author"`
Type int64 `json:"type"`
IsPublic int `json:"is_public"`
Father int `json:"father"`
Level int `json:"level"`
}
var req ReqUpdateArticle
defer func() {
c.JSON(200, rsp)
}()
er := c.BindJSON(&req)
if nil != er {
logs.Error(er.Error())
return
}
if req.Title == "" {
rsp.Msg = "title required"
return
}
2022-04-17 15:17:46 +00:00
query := fmt.Sprintf("select * from doc_copy1 where doc.id = '%d'", req.ID)
docs := []model.Doc{}
e := db.GetMysqlClient().Query2(query, &docs)
if nil != e {
log.Print(e.Error())
return
}
if len(docs) == 0 {
rsp.Msg = "不存在该"
return
}
e = model.UpdateDocTree(
model.DocTree{
Type: int32(req.Type),
Title: req.Title,
Content: req.Content,
Author: req.Author,
ID: req.ID,
IsPublic: int32(req.IsPublic),
Version: (docs[0].Version + 1),
Father: int32(req.Father),
Level: int32(req.Level),
},
)
if nil != e {
logs.Error(e.Error())
return
}
rsp.Msg = "OK"
rsp.Status = 0
}
2021-08-01 11:50:48 +00:00
func SearchArticleESHightLight(c *gin.Context) {
2021-08-01 15:33:55 +00:00
type RetHighlight struct{
ID int64 `json:"id" gorm:"column:id" sql:"id"`
Title string `json:"title" gorm:"column:title" sql:"title"`
Type int32 `json:"type" gorm:"column:type" sql:"type"`
Highlight []string `json:"highlight" gorm:"column:highlight" sql:"highlight"`
}
2021-08-01 11:50:48 +00:00
rsp := RespBase{Msg: "FAIL", Status: 210}
defer func() {
c.JSON(200, rsp)
}()
type Req struct{
Term string `json:"term"`
}
var req Req
e := c.BindJSON(&req)
if nil != e{
log.Print(e.Error())
return
}
query := elastic.NewMatchQuery("content", req.Term)
2021-08-01 15:33:55 +00:00
highlight := elastic.NewHighlight()
highlight = highlight.Fields(elastic.NewHighlighterField("content"))
highlight = highlight.PreTags("<span>").PostTags("</span>")
x := []RetHighlight{}
2021-11-03 16:46:33 +00:00
_, e = db.GetElastic().
QueryHighlight("doc", query, &x,highlight, 10, 0)
2021-08-01 11:50:48 +00:00
if nil != e {
log.Print(e.Error())
return
}
rsp.Data = x
rsp.Msg = "OK"
rsp.Status = 0
}
2021-05-22 17:27:55 +00:00
func SearchArticleES(c *gin.Context) {
2020-08-23 18:08:26 +00:00
rsp := RespBase{Msg: "FAIL", Status: 210}
2020-06-15 16:34:59 +00:00
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, rsp)
2020-06-15 16:34:59 +00:00
}()
2021-05-22 17:27:55 +00:00
type Req struct{
Term string `json:"term"`
}
var req Req
e := c.BindJSON(&req)
if nil != e{
log.Print(e.Error())
return
}
query := elastic.NewTermQuery("content", req.Term)
x := []model.Doc{}
_, e = db.GetElastic().Query("doc", query, &x, 10, 0)
if nil != e {
log.Print(e.Error())
return
}
rsp.Data = x
rsp.Msg = "OK"
rsp.Status = 0
2020-06-15 16:34:59 +00:00
}
2021-05-22 17:27:55 +00:00
2020-08-23 18:08:26 +00:00
func AddArticle(c *gin.Context) {
rsp := RespBase{Msg: "FAIL", Status: 210}
2019-05-16 10:05:20 +00:00
type ReqAddArticle struct {
ID int64 `json:"id"`
2020-08-23 18:08:26 +00:00
Title string `json:"title"`
2019-05-16 10:05:20 +00:00
Content string `json:"content"`
Author string `json:"author"`
2020-08-23 18:08:26 +00:00
Type int64 `json:"type"`
Ispublic int `json:"is_public"`
2019-05-16 10:05:20 +00:00
}
var req ReqAddArticle
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, rsp)
2019-05-16 10:05:20 +00:00
}()
er := c.BindJSON(&req)
2020-08-23 18:08:26 +00:00
if nil != er {
2019-05-16 10:05:20 +00:00
logs.Error(er.Error())
return
}
2020-08-23 18:08:26 +00:00
if req.Title == "" {
2019-05-17 09:45:50 +00:00
rsp.Msg = "title required"
return
}
2019-05-16 10:05:20 +00:00
e := model.CreateDoc(
model.Doc{
2021-03-04 13:26:09 +00:00
Type: int32(req.Type),
2020-08-23 18:08:26 +00:00
Title: req.Title,
Content: req.Content,
Author: req.Author,
2021-03-04 13:26:09 +00:00
IsPublic: int32(req.Ispublic),
2019-05-16 10:05:20 +00:00
},
2020-08-23 18:08:26 +00:00
)
if nil != e {
2019-05-16 10:05:20 +00:00
logs.Error(e.Error())
return
}
rsp.Msg = "OK"
rsp.Status = 0
2019-05-17 09:45:50 +00:00
}
func AddArticleTree(c *gin.Context) {
rsp := RespBase{Msg: "FAIL", Status: 210}
type ReqAddArticle struct {
Title string `json:"title"`
Content string `json:"content"`
Author string `json:"author"`
Type int64 `json:"type"`
Ispublic int `json:"is_public"`
Father int `json:"father"`
Level int `json:"level"`
}
var req ReqAddArticle
defer func() {
c.JSON(200, rsp)
}()
er := c.BindJSON(&req)
if nil != er {
logs.Error(er.Error())
return
}
if req.Title == "" {
rsp.Msg = "title required"
return
}
e := model.CreateDocTree(
model.DocTree{
Type: int32(req.Type),
Title: req.Title,
Content: req.Content,
Author: req.Author,
IsPublic: int32(req.Ispublic),
Father: int32(req.Father),
Level: int32(req.Level),
},
)
if nil != e {
logs.Error(e.Error())
return
}
rsp.Msg = "OK"
rsp.Status = 0
}
2020-08-23 18:08:26 +00:00
func ArticlesType(c *gin.Context) {
rsp := RespBase{Msg: "FAIL", Status: 210}
2019-05-17 09:45:50 +00:00
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, rsp)
2019-05-17 09:45:50 +00:00
}()
rsp.Data = model.GetArticlesType()
rsp.Msg = "OK"
rsp.Status = 0
}
2020-08-23 18:08:26 +00:00
func DeleteArticleType(c *gin.Context) {
rsp := RespBase{Msg: "Fail", Status: 210}
2020-01-24 16:07:37 +00:00
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, rsp)
2020-01-24 16:07:37 +00:00
}()
2020-01-25 17:39:31 +00:00
2020-08-23 18:08:26 +00:00
sid := c.Query("id")
if sid == "" {
2020-01-25 17:39:31 +00:00
return
2020-01-24 16:07:37 +00:00
}
2020-08-23 18:08:26 +00:00
id, e := strconv.Atoi(sid)
if nil != e {
2020-01-25 17:39:31 +00:00
log.Print(e.Error())
return
}
rsp.Data = model.DeleteArticleType(int32(id))
rsp.Msg = "OK"
rsp.Status = 0
}
2021-05-22 17:27:55 +00:00
2020-08-23 18:08:26 +00:00
func AddArticleType(c *gin.Context) {
rsp := RespBase{Msg: "Fail", Status: 210}
2020-01-25 17:39:31 +00:00
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, rsp)
2020-01-25 17:39:31 +00:00
}()
2020-08-23 18:08:26 +00:00
typeName := c.Query("name")
2020-07-26 07:03:42 +00:00
group := c.Query("group")
2020-08-23 18:08:26 +00:00
groupid, e := strconv.Atoi(group)
if nil != e {
2020-07-26 07:03:42 +00:00
return
}
2020-01-26 11:11:48 +00:00
id := c.Query("id")
2020-08-23 18:08:26 +00:00
if id != "" {
if typeName == "" {
2020-01-26 11:11:48 +00:00
return
}
articleType := model.ArticleType{
2020-08-23 18:08:26 +00:00
Id: 0,
Name: typeName,
Group: int32(groupid),
2020-01-26 11:11:48 +00:00
}
rsp.Data = model.UpdateArticleType(articleType)
rsp.Msg = "OK"
rsp.Status = 0
2020-08-23 18:08:26 +00:00
} else {
if typeName == "" {
2020-01-26 11:11:48 +00:00
return
}
articleType := model.ArticleType{
2020-08-23 18:08:26 +00:00
Id: 0,
Name: typeName,
Group: int32(groupid),
2020-01-26 11:11:48 +00:00
}
rsp.Data = model.AddArticleType(articleType)
rsp.Msg = "OK"
rsp.Status = 0
2020-01-24 16:07:37 +00:00
}
}
2020-08-23 18:08:26 +00:00
func DeleteArticle(c *gin.Context) {
rsp := RespBase{Msg: "FAIL", Status: 210}
2019-09-06 07:53:04 +00:00
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, rsp)
2019-09-06 07:53:04 +00:00
}()
sid := c.Param("id")
2020-08-23 18:08:26 +00:00
id, err := strconv.Atoi(sid)
if nil != err {
2019-09-06 07:53:04 +00:00
rsp.Status = -234
2020-08-23 18:08:26 +00:00
c.JSON(200, rsp)
2019-09-06 07:53:04 +00:00
}
err = model.DeleteDoc(int64(id))
2020-08-23 18:08:26 +00:00
if nil != err {
2019-09-06 07:53:04 +00:00
rsp.Status = -234
2020-08-23 18:08:26 +00:00
c.JSON(200, rsp)
2019-09-06 07:53:04 +00:00
}
rsp.Data = id
rsp.Status = 0
rsp.Msg = "OK"
2019-09-06 16:31:01 +00:00
}
2020-03-21 03:17:24 +00:00
func DeleteArticleTree(c *gin.Context) {
rsp := RespBase{Msg: "FAIL", Status: 210}
defer func() {
c.JSON(200, rsp)
}()
sid := c.Param("id")
id, err := strconv.Atoi(sid)
if nil != err {
rsp.Status = -234
c.JSON(200, rsp)
}
err = model.DeleteDocTree(int64(id))
if nil != err {
rsp.Status = -234
c.JSON(200, rsp)
}
rsp.Data = id
rsp.Status = 0
rsp.Msg = "OK"
}
2020-08-23 18:08:26 +00:00
func ArticlesTypes(c *gin.Context) {
resp := RespBase{"unkown error", -231, nil}
2019-09-06 16:31:01 +00:00
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, resp)
2019-09-06 16:31:01 +00:00
}()
2020-09-26 04:30:23 +00:00
docTypes := []model.ArticleType{}
2019-09-06 16:31:01 +00:00
e := db.GetMysqlClient().Query2("select * from doc_type",
&docTypes)
2020-08-23 18:08:26 +00:00
if nil != e {
2019-09-06 16:31:01 +00:00
log.Print(e.Error())
return
}
resp.Data = docTypes
resp.Msg = "OK"
resp.Status = 0
2020-06-24 17:15:46 +00:00
}
2021-05-22 17:27:55 +00:00
func SearchArticles(c *gin.Context) {
type ReqSearch struct{
Term string `json:"term"`
}
resp := RespBase{"unkown error", -231, nil}
defer func() {
c.JSON(200, resp)
}()
docTypes := []model.Doc{}
e := db.GetMysqlClient().Query2("select * from doc_type",
&docTypes)
if nil != e {
log.Print(e.Error())
return
}
resp.Data = docTypes
resp.Msg = "OK"
resp.Status = 0
}
2020-08-23 18:08:26 +00:00
func CreateMemo(c *gin.Context) {
resp := RespBase{"unkown error", -231, nil}
2021-01-09 14:55:22 +00:00
defer func() {
c.JSON(200, resp)
}()
var req model.Memo
e := c.BindJSON(&req)
if nil != e {
logs.Error(e.Error())
resp.Msg = "wrong input"
return
}
e = model.CreateMemo(req)
if nil != e {
logs.Error(e.Error())
return
}
resp.Data = nil
resp.Msg = "OK"
resp.Status = 0
}
func CreateBook(c *gin.Context) {
resp := RespBase{"unkown error", -231, nil}
2020-06-24 17:15:46 +00:00
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, resp)
2020-06-24 17:15:46 +00:00
}()
2021-01-12 15:39:31 +00:00
var req model.Book
2020-06-24 17:15:46 +00:00
e := c.BindJSON(&req)
2020-08-23 18:08:26 +00:00
if nil != e {
2020-06-26 10:20:36 +00:00
logs.Error(e.Error())
2020-06-24 17:15:46 +00:00
resp.Msg = "wrong input"
return
}
2021-01-12 15:39:31 +00:00
req.ID = 0
e = db.GetOrm().Create(&req).Error
if nil != e {
logs.Error(e.Error())
return
}
2021-02-09 15:31:30 +00:00
resp.Data = req
resp.Msg = "OK"
resp.Status = 0
}
func BookCount(c *gin.Context) {
resp := RespBase{"unkown error", -231, nil}
defer func() {
c.JSON(200, resp)
}()
count := 0
db.GetOrm().Model(&model.Book{}).Count(&count)
resp.Data = count
resp.Msg = "OK"
resp.Status = 0
}
func Delbook(c *gin.Context) {
type Req struct{
ID int32 `json:"id"`
}
var req Req
resp := RespBase{"unkown error", -231, nil}
defer func() {
c.JSON(200, resp)
}()
e := c.BindJSON(&req)
if nil != e {
logs.Error(e.Error())
resp.Msg = "wrong input"
return
}
count := 0
e = db.GetOrm().Model(&model.Book{}).Delete(model.Book{ID: int64(req.ID)}).Error
if nil != e{
log.Printf(e.Error())
return
}
resp.Data = count
2021-01-12 15:39:31 +00:00
resp.Msg = "OK"
resp.Status = 0
}
func UpdateBook(c *gin.Context) {
resp := RespBase{"unkown error", -231, nil}
defer func() {
c.JSON(200, resp)
}()
var req model.Book
e := c.BindJSON(&req)
if nil != e {
resp.Msg = "wrong input"
2021-02-14 13:31:10 +00:00
log.Print(e.Error())
2021-01-12 15:39:31 +00:00
return
}
2021-01-12 15:46:51 +00:00
e = db.GetOrm().Model(&model.Book{}).Update(&req).Error
2020-08-23 18:08:26 +00:00
if nil != e {
2020-06-24 17:15:46 +00:00
logs.Error(e.Error())
return
}
resp.Data = nil
resp.Msg = "OK"
resp.Status = 0
}
2021-01-25 09:28:50 +00:00
func GetBook(c *gin.Context) {
type ReqGetBook struct {
ID int32 `json:"id"`
}
resp := RespBase{}
var req ReqGetBook
defer func() {
c.JSON(200, resp)
}()
e := c.BindJSON(&req)
if nil != e {
logs.Error(e.Error())
return
}
var book model.Book
book.ID = int64(req.ID)
2021-01-25 14:34:45 +00:00
e = db.GetOrm().First(&book, req.ID).Error
if e != nil {
2021-01-25 09:28:50 +00:00
logs.Error(e.Error)
return
}
resp.Data = book
resp.Msg = "OK"
resp.Status = 0
}
func GetPageBook(c *gin.Context) {
type ReqGetPageBook struct {
2021-02-09 15:31:30 +00:00
BookName string `json:"book_name"`
2021-01-25 09:28:50 +00:00
}
var req ReqGetPageBook
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
limit := c.Query("limit")
offset := c.Query("offset")
iLmit, e := strconv.Atoi(limit)
if nil != e {
2021-01-25 16:25:18 +00:00
log.Print(e.Error())
2021-01-25 09:28:50 +00:00
return
}
iOffset, e := strconv.Atoi(offset)
if nil != e {
2021-01-25 16:25:18 +00:00
log.Print(e.Error())
2021-01-25 09:28:50 +00:00
return
}
e = c.BindJSON(&req)
if nil != e {
logs.Error(e.Error())
return
}
books := []model.Book{}
2021-02-09 15:31:30 +00:00
if req.BookName != ""{
e = db.GetOrm().Model(&model.Book{}).
Where(fmt.Sprintf("book_name like '%%%s%%' ",req.BookName)).
Limit(iLmit).Offset(iOffset).Find(&books).Error
if nil != e {
log.Print(e.Error())
return
}
}else{
e = db.GetOrm().Model(&model.Book{}).
Limit(iLmit).Offset(iOffset).Find(&books).Error
if nil != e {
log.Print(e.Error())
return
}
2021-01-25 09:28:50 +00:00
}
2021-02-09 15:31:30 +00:00
2021-01-25 09:28:50 +00:00
resp.Status = 0
resp.Msg = "OK"
resp.Data = books
}
2020-08-23 18:08:26 +00:00
func UpdateMemo(c *gin.Context) {
resp := RespBase{"unkown error", -231, nil}
2020-06-24 17:15:46 +00:00
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, resp)
2020-06-24 17:15:46 +00:00
}()
var req model.Memo
e := c.BindJSON(&req)
2020-08-23 18:08:26 +00:00
if nil != e {
2020-06-24 17:15:46 +00:00
resp.Msg = "wrong input"
return
}
e = model.UpdateMemo(req)
2020-08-23 18:08:26 +00:00
if nil != e {
2020-06-24 17:15:46 +00:00
logs.Error(e.Error())
return
}
resp.Data = nil
resp.Msg = "OK"
resp.Status = 0
}
2020-09-26 04:30:23 +00:00
func GetDocTypeGroup(c *gin.Context) {
type DocType struct {
Id int32 `json:"id"`
}
rsp := RespBase{"ERR", -1, nil}
defer func() {
c.JSON(200, rsp)
}()
var id DocType
e := c.BindJSON(&id)
if nil != e {
log.Print(e.Error)
return
}
group, e := model.GetTypeGroup(id.Id)
if nil != e {
log.Print(e.Error)
return
}
rsp.Data = group
rsp.Status = 0
rsp.Msg = "OK"
}
func GetDoGroupcType(c *gin.Context) {
type DocType struct {
Id int32 `json:"id"`
}
rsp := RespBase{"ERR", -1, nil}
defer func() {
c.JSON(200, rsp)
}()
var id DocType
e := c.BindJSON(&id)
if nil != e {
log.Print(e.Error)
return
}
group, e := model.GetGroupTypes(id.Id)
if nil != e {
log.Print(e.Error)
return
}
rsp.Data = group
rsp.Status = 0
rsp.Msg = "OK"
}
2020-07-26 07:03:42 +00:00
func GetDocGroup(c *gin.Context) {
2020-08-23 18:08:26 +00:00
rsp := RespBase{"ERR", -1, nil}
2020-07-26 07:03:42 +00:00
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, rsp)
2020-07-26 07:03:42 +00:00
}()
2020-08-23 18:08:26 +00:00
group, e := model.GetAllGroup()
if nil != e {
2020-07-26 07:03:42 +00:00
log.Print(e.Error())
return
}
rsp.Data = group
rsp.Status = 0
rsp.Msg = "OK"
}
2020-08-23 18:08:26 +00:00
func GetTemplates(c *gin.Context) {
rsp := RespBase{"ERR", -1, nil}
defer func() {
c.JSON(200, rsp)
}()
}
2020-06-24 17:15:46 +00:00
func GetMemos(c *gin.Context) {
2020-08-23 18:08:26 +00:00
rsp := RespBase{"ERR", -1, nil}
2020-06-24 17:15:46 +00:00
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, rsp)
2020-06-24 17:15:46 +00:00
}()
type ReqMemos struct {
2020-06-26 10:20:36 +00:00
Title string `json:"title"`
2020-06-24 17:15:46 +00:00
}
req := ReqMemos{}
e := c.BindJSON(&req)
2020-08-23 18:08:26 +00:00
if nil != e {
2020-06-24 17:15:46 +00:00
logs.Error(e.Error())
return
}
2020-08-23 18:08:26 +00:00
limit, offset := GetPageParaFromQuery(c)
if limit != 0 && offset != 0 {
2021-06-24 18:05:58 +00:00
dat, e := model.GetMemos(req.Title, limit, offset)
2020-08-23 18:08:26 +00:00
if nil != e {
2020-06-24 17:15:46 +00:00
return
}
rsp.Data = dat
rsp.Status = 0
rsp.Msg = "OK"
2020-08-23 18:08:26 +00:00
} else {
2021-06-24 18:05:58 +00:00
dat, e := model.GetMemos(req.Title, limit, offset)
2020-08-23 18:08:26 +00:00
if nil != e {
2020-06-24 17:15:46 +00:00
return
}
rsp.Data = dat
rsp.Status = 0
rsp.Msg = "OK"
}
}
2021-01-25 09:28:50 +00:00
2021-06-24 18:05:58 +00:00
func GetMemoCnt(c *gin.Context) {
rsp := RespBase{"ERR", -1, nil}
defer func() {
c.JSON(200, rsp)
}()
count := model.MemoCnt()
rsp.Msg = "OK"
rsp.Status = 0
rsp.Data = count
}
2020-06-24 17:15:46 +00:00
func GetMemo(c *gin.Context) {
2020-08-23 18:08:26 +00:00
rsp := RespBase{"ERR", -1, nil}
2020-06-24 17:15:46 +00:00
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, rsp)
2020-06-24 17:15:46 +00:00
}()
id := c.Query("id")
dat, e := model.ReadMemo(int32(db.Atoi(id)))
2020-08-23 18:08:26 +00:00
if nil != e {
2020-06-24 17:15:46 +00:00
return
}
rsp.Msg = "OK"
rsp.Status = 0
rsp.Data = dat
}
2021-02-09 15:31:30 +00:00
2020-06-24 17:15:46 +00:00
func DeleteMemos(c *gin.Context) {
2020-08-23 18:08:26 +00:00
resp := RespBase{"unkown error", -231, nil}
2020-06-24 17:15:46 +00:00
defer func() {
2020-08-23 18:08:26 +00:00
c.JSON(200, resp)
2020-06-24 17:15:46 +00:00
}()
type DelReq struct {
Id int32 `json:"id"`
}
var req DelReq
e := c.BindJSON(&req)
2020-08-23 18:08:26 +00:00
if nil != e {
2020-06-24 17:15:46 +00:00
resp.Msg = "wrong input"
return
}
e = model.DeleteMemo(req.Id)
2020-08-23 18:08:26 +00:00
if nil != e {
2020-06-24 17:15:46 +00:00
logs.Error(e.Error())
return
}
resp.Data = nil
resp.Msg = "OK"
resp.Status = 0
2020-08-23 18:08:26 +00:00
}
2021-03-26 05:13:23 +00:00
func CreateDocTemplate(c *gin.Context) {
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
var req model.DocTemplate
e := c.BindJSON(&req)
if nil != e {
logs.Error(e.Error())
resp.Msg = "wrong input"
return
}
e = model.CreateDocTemplate(req)
if nil != e{
log.Print(e.Error())
return
}
resp.Data = nil
resp.Msg = "OK"
resp.Status = 0
}
func UpdateDocTemplate(c *gin.Context) {
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
var req model.DocTemplate
e := c.BindJSON(&req)
if nil != e {
resp.Msg = "wrong input"
return
}
e = model.UpdateDocTemplate(req)
if nil != e {
logs.Error(e.Error())
return
}
resp.Data = nil
resp.Msg = "OK"
resp.Status = 0
}
func GetDocTemplate(c *gin.Context) {
type ReqGet struct{
Title string `json:"title"`
}
req := ReqGet{}
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
e := c.BindJSON(&req)
if nil != e{
log.Print(e.Error())
return
}
ret,e := model.GetDocTemplate(req.Title,5,0)
if nil != e{
log.Print(e.Error())
return
}
resp.Data = ret
resp.Msg = "OK"
resp.Status = 0
}
func DeleteDocTemplate(c *gin.Context) {
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
2021-03-26 13:44:35 +00:00
sid := c.Param("id")
id,e := strconv.Atoi(sid)
if nil != e{
log.Print(e.Error())
return
2021-03-26 05:13:23 +00:00
}
2021-03-26 13:44:35 +00:00
e = model.DeleteDocTemplate(int32(id))
if nil != e{
log.Print(e.Error())
return
}
resp.Data = nil
resp.Msg = "OK"
resp.Status = 0
2021-03-27 05:58:44 +00:00
}
func GetDocTemplateId(c *gin.Context) {
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
sid := c.Param("id")
id,e := strconv.Atoi(sid)
if nil != e{
log.Print(e.Error())
return
}
ret,e := model.ReadDocTemplate(int32(id))
if nil != e{
log.Print(e.Error())
return
}
if len(ret) > 0{
resp.Data = ret[0]
}else{
resp.Data = nil
}
resp.Msg = "OK"
resp.Status = 0
2021-03-26 05:13:23 +00:00
}