diff --git a/controller/blog.go b/controller/blog.go index c78fc86..50ec92f 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -392,7 +392,48 @@ func CreateBook(c *gin.Context) { logs.Error(e.Error()) return } - resp.Data = nil + 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 resp.Msg = "OK" resp.Status = 0 } @@ -446,6 +487,7 @@ func GetBook(c *gin.Context) { func GetPageBook(c *gin.Context) { type ReqGetPageBook struct { + BookName string `json:"book_name"` } var req ReqGetPageBook resp := RespBase{} @@ -472,11 +514,24 @@ func GetPageBook(c *gin.Context) { return } books := []model.Book{} - e = db.GetOrm().Limit(iLmit).Offset(iOffset).Find(&books).Error - if nil != e { - log.Print(e.Error()) - return + + 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 + } } + resp.Status = 0 resp.Msg = "OK" resp.Data = books @@ -627,6 +682,8 @@ func GetMemo(c *gin.Context) { rsp.Status = 0 rsp.Data = dat } + + func DeleteMemos(c *gin.Context) { resp := RespBase{"unkown error", -231, nil} defer func() { diff --git a/main.go b/main.go index c0780e8..5c67911 100644 --- a/main.go +++ b/main.go @@ -172,8 +172,9 @@ func main() { api.POST("/book", controller.UpdateBook) // api.POST("/getbook", controller.GetBook) // 单书籍数据 api.POST("/getbooks", controller.GetPageBook) // 批量书籍 - api.POST("/delbook", controller.DeleteMemos) // 删除书籍 - + api.POST("/delbook", controller.Delbook) // 删除书籍 + api.GET("/bookcount", controller.BookCount) // 删除书籍 + } openapi := r.Group("openapi") diff --git a/model/port.go b/model/port.go index 8139b71..aa98c6f 100644 --- a/model/port.go +++ b/model/port.go @@ -78,7 +78,6 @@ func QueryDocument(query elastic.Query, limit int, offset int) ([]Hardware, erro return ret, nil } func InsertDocToElaticSearch(doc Doc) error { - matchPhraseQuery := elastic.NewMatchQuery("title", doc.Title) existedHardware, e := QueryHardwares(matchPhraseQuery, 10, 0) log.Print(e, existedHardware)