From 170d97145d1d69e652045bd0ad3338ae7e05074c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=87=95=E9=B9=8F?= Date: Fri, 21 May 2021 11:37:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7beego=20V2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/BlogController.go | 4 ++-- controllers/CatController.go | 2 +- controllers/CommentController.go | 6 +++--- controllers/FileController.go | 31 ++++++++++++++++++------------- controllers/IndexController.go | 2 +- controllers/LikeController.go | 14 +++++++------- controllers/MapController.go | 2 +- controllers/NoteController.go | 2 +- controllers/PageController.go | 5 ++--- controllers/UPageController.go | 2 +- controllers/UserController.go | 12 ++++++------ filter/Security.go | 8 ++++---- go.mod | 6 +++--- main.go | 14 +++++++------- mcontrollers/MBlogController.go | 17 +++++++---------- models/DataInit.go | 29 +++++++++-------------------- routers/BlogRouter.go | 2 +- routers/CatRouter.go | 2 +- routers/CommentRouter.go | 4 ++-- routers/LikeRouter.go | 2 +- routers/MBlogRouter.go | 2 +- routers/NoteRouter.go | 2 +- routers/UPageRouter.go | 2 +- routers/UserRouter.go | 2 +- routers/router.go | 10 +++++----- service/BlogService.go | 20 +++++++++----------- service/CategoryService.go | 2 +- service/CommentService.go | 10 +++++----- service/LikeService.go | 4 ++-- service/NoteService.go | 2 +- service/UserService.go | 11 +++++------ utils/EsUtil.go | 2 +- 32 files changed, 111 insertions(+), 124 deletions(-) diff --git a/controllers/BlogController.go b/controllers/BlogController.go index 6c3114b..f00f9a5 100644 --- a/controllers/BlogController.go +++ b/controllers/BlogController.go @@ -4,7 +4,7 @@ import ( "beeblog/models" "beeblog/service" "beeblog/utils" - "github.com/astaxie/beego" + beego "github.com/beego/beego/v2/server/web" "strconv" "time" ) @@ -51,7 +51,7 @@ func (this *BlogController) Save() { catory := this.GetString("catory") catoryId, _ := strconv.ParseInt(catory, 10, 64) labels := this.GetStrings("labels[]") - blog := &models.Blog{Title: title, BlogHtml: blogHtml, BlogValue: blogValue,CategoryId: catoryId, UserId: uid.(int64), BlogDesc: blogDesc} + blog := &models.Blog{Title: title, BlogHtml: blogHtml, BlogValue: blogValue, CategoryId: catoryId, UserId: uid.(int64), BlogDesc: blogDesc} err := blogService.SaveBlog(blog, labels) if err == nil { this.Data["json"] = models.ReurnData("", blog.Id) diff --git a/controllers/CatController.go b/controllers/CatController.go index c6507cd..a7283d8 100644 --- a/controllers/CatController.go +++ b/controllers/CatController.go @@ -3,7 +3,7 @@ package controllers import ( "beeblog/models" "beeblog/service" - "github.com/astaxie/beego" + beego "github.com/beego/beego/v2/server/web" ) type CatController struct { diff --git a/controllers/CommentController.go b/controllers/CommentController.go index 0f29cd9..dd203d4 100644 --- a/controllers/CommentController.go +++ b/controllers/CommentController.go @@ -1,9 +1,9 @@ package controllers import ( - "github.com/astaxie/beego" "beeblog/models" "beeblog/service" + beego "github.com/beego/beego/v2/server/web" "strconv" ) @@ -49,7 +49,7 @@ func (this *CommentController) Save() { this.Data["json"] = models.ReurnError(500, "保存失败") } this.ServeJSON() - userService.CountComments(uid.(int64),blogId) + userService.CountComments(uid.(int64), blogId) return } @@ -83,6 +83,6 @@ func (this *CommentController) Del() { this.Data["json"] = models.ReurnError(500, "保存失败") } this.ServeJSON() - userService.CountComments(uid.(int64),id) + userService.CountComments(uid.(int64), id) return } diff --git a/controllers/FileController.go b/controllers/FileController.go index f1109d0..4a0048c 100644 --- a/controllers/FileController.go +++ b/controllers/FileController.go @@ -1,15 +1,16 @@ package controllers import ( - "github.com/astaxie/beego" - "path" - "time" - "os" - "fmt" - "crypto/md5" - "math/rand" "beeblog/models" "beeblog/service" + "crypto/md5" + "fmt" + "github.com/beego/beego/v2/core/config" + beego "github.com/beego/beego/v2/server/web" + "math/rand" + "os" + "path" + "time" ) type FileController struct { @@ -45,7 +46,8 @@ func (this *FileController) Upload() { } //创建目录 urlDir := time.Now().Format("2006/01/02/") - uploadDir := beego.AppConfig.String("uploadDir") + urlDir + uploadDir, _ := config.String("uploadDir") + uploadDir = uploadDir + urlDir err := os.MkdirAll(uploadDir, 777) if err != nil { this.Data["json"] = models.ReurnError(500, "") @@ -55,7 +57,7 @@ func (this *FileController) Upload() { //构造文件名称 rand.Seed(time.Now().UnixNano()) randNum := fmt.Sprintf("%d", rand.Intn(9999)+1000) - hashName := md5.Sum([]byte( time.Now().Format("2006_01_02_15_04_05_") + randNum )) + hashName := md5.Sum([]byte(time.Now().Format("2006_01_02_15_04_05_") + randNum)) fileName := fmt.Sprintf("%x", hashName) + ext fpath := uploadDir + fileName urlDir += fileName @@ -67,7 +69,8 @@ func (this *FileController) Upload() { this.ServeJSON() return } - urlDir = beego.AppConfig.String("imgUrlPrefix") + urlDir + imgUrlPrefix, _ := config.String("imgUrlPrefix") + urlDir = imgUrlPrefix + urlDir this.Data["json"] = models.ReurnData("", urlDir) this.ServeJSON() return @@ -106,7 +109,8 @@ func (this *FileController) HeadImgUpload() { } //创建目录 urlDir := time.Now().Format("2006/01/02/") - uploadDir := beego.AppConfig.String("uploadDir") + urlDir + uploadDir, _ := config.String("uploadDir") + uploadDir = uploadDir + urlDir err := os.MkdirAll(uploadDir, 777) if err != nil { this.Data["json"] = models.ReurnError(500, "") @@ -116,7 +120,7 @@ func (this *FileController) HeadImgUpload() { //构造文件名称 rand.Seed(time.Now().UnixNano()) randNum := fmt.Sprintf("%d", rand.Intn(9999)+1000) - hashName := md5.Sum([]byte( time.Now().Format("2006_01_02_15_04_05_") + randNum )) + hashName := md5.Sum([]byte(time.Now().Format("2006_01_02_15_04_05_") + randNum)) fileName := fmt.Sprintf("%x", hashName) + ext fpath := uploadDir + fileName urlDir += fileName @@ -128,7 +132,8 @@ func (this *FileController) HeadImgUpload() { this.ServeJSON() return } - urlDir = beego.AppConfig.String("imgUrlPrefix") + urlDir + imgUrlPrefix, _ := config.String("imgUrlPrefix") + urlDir = imgUrlPrefix + urlDir user := &models.User{Id: uid.(int64), Headimg: urlDir} userSerivce := service.UserService{} if _, err = userSerivce.EditHeadImg(user); err != nil { diff --git a/controllers/IndexController.go b/controllers/IndexController.go index 91e8adc..ae43217 100644 --- a/controllers/IndexController.go +++ b/controllers/IndexController.go @@ -1,8 +1,8 @@ package controllers import ( - "github.com/astaxie/beego" "beeblog/service" + beego "github.com/beego/beego/v2/server/web" ) type IndexController struct { diff --git a/controllers/LikeController.go b/controllers/LikeController.go index 21b8dcd..06d05c2 100644 --- a/controllers/LikeController.go +++ b/controllers/LikeController.go @@ -1,10 +1,10 @@ package controllers import ( - "strconv" - "beeblog/service" - "github.com/astaxie/beego" "beeblog/models" + "beeblog/service" + beego "github.com/beego/beego/v2/server/web" + "strconv" ) type LikeController struct { @@ -25,11 +25,11 @@ func (this *LikeController) Save() { like := &models.Like{BlogId: id, UserId: uid.(int64)} if _, err := likeService.SaveLike(like); err != nil { this.Data["json"] = models.ReurnError(500, "保存失败") - }else{ + } else { this.Data["json"] = models.ReurnSuccess("") } this.ServeJSON() - userService.CountLike(uid.(int64),id) + userService.CountLike(uid.(int64), id) return } @@ -47,10 +47,10 @@ func (this *LikeController) Delete() { like := &models.Like{BlogId: id, UserId: uid.(int64)} if _, err := likeService.DelLike(like); err != nil { this.Data["json"] = models.ReurnError(500, "保存失败") - }else{ + } else { this.Data["json"] = models.ReurnSuccess("") } this.ServeJSON() - userService.CountLike(uid.(int64),id) + userService.CountLike(uid.(int64), id) return } diff --git a/controllers/MapController.go b/controllers/MapController.go index e6280b0..c89a243 100644 --- a/controllers/MapController.go +++ b/controllers/MapController.go @@ -1,6 +1,6 @@ package controllers -import "github.com/astaxie/beego" +import beego "github.com/beego/beego/v2/server/web" type MapController struct { beego.Controller diff --git a/controllers/NoteController.go b/controllers/NoteController.go index 50c0efc..27658a2 100644 --- a/controllers/NoteController.go +++ b/controllers/NoteController.go @@ -3,7 +3,7 @@ package controllers import ( "beeblog/models" "beeblog/service" - "github.com/astaxie/beego" + beego "github.com/beego/beego/v2/server/web" "strconv" ) diff --git a/controllers/PageController.go b/controllers/PageController.go index 87f2560..262f46c 100644 --- a/controllers/PageController.go +++ b/controllers/PageController.go @@ -1,16 +1,15 @@ package controllers import ( - "github.com/astaxie/beego" - "beeblog/service" "beeblog/models" + "beeblog/service" + beego "github.com/beego/beego/v2/server/web" ) type PageController struct { beego.Controller } - func (this *PageController) IframeUser() { userService := service.UserService{} uid := this.GetSession("userid") diff --git a/controllers/UPageController.go b/controllers/UPageController.go index e4f9491..c91d108 100644 --- a/controllers/UPageController.go +++ b/controllers/UPageController.go @@ -1,6 +1,6 @@ package controllers -import "github.com/astaxie/beego" +import beego "github.com/beego/beego/v2/server/web" type UPageController struct { beego.Controller diff --git a/controllers/UserController.go b/controllers/UserController.go index e32059f..4f924b3 100644 --- a/controllers/UserController.go +++ b/controllers/UserController.go @@ -1,14 +1,14 @@ package controllers import ( - "github.com/astaxie/beego" - "beeblog/service" "beeblog/models" - "strings" + "beeblog/service" "crypto/md5" - "time" - "strconv" "encoding/hex" + beego "github.com/beego/beego/v2/server/web" + "strconv" + "strings" + "time" ) type UserController struct { @@ -295,7 +295,7 @@ func (this *UserController) Regist() { return } h := md5.New() - h.Write([]byte(strconv.FormatInt(time.Now().Unix(), 10) + beego.AppConfig.String("host"))) + h.Write([]byte(strconv.FormatInt(time.Now().Unix(), 10) + "aiprose:nelson")) salt := hex.EncodeToString(h.Sum(nil)) h = md5.New() h.Write([]byte(userpwd + salt)) diff --git a/filter/Security.go b/filter/Security.go index 70c1e99..957d366 100644 --- a/filter/Security.go +++ b/filter/Security.go @@ -1,10 +1,10 @@ package filter import ( - "github.com/astaxie/beego/context" - "github.com/astaxie/beego/logs" "beeblog/models" "encoding/json" + "github.com/beego/beego/v2/core/logs" + "github.com/beego/beego/v2/server/web/context" ) var LogFilter = func(ctx *context.Context) { @@ -13,9 +13,9 @@ var LogFilter = func(ctx *context.Context) { var FilterAdmin = func(ctx *context.Context) { if ctx.Input.Session("userid") != nil { ctx.Output.SetStatus(200) - ctx.Output.Header("Access-Control-Allow-Origin","*") + ctx.Output.Header("Access-Control-Allow-Origin", "*") result := models.ReurnError(401, "") - if b,err := json.Marshal(result); err != nil { + if b, err := json.Marshal(result); err != nil { ctx.Output.Body(b) } return diff --git a/go.mod b/go.mod index 8cc99e0..2fddf6f 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,10 @@ module beeblog require ( - github.com/astaxie/beego v1.12.0 + github.com/beego/bee/v2 v2.0.2 // indirect + github.com/beego/beego/v2 v2.0.1 github.com/fortytw2/leaktest v1.3.0 // indirect - github.com/go-sql-driver/mysql v1.4.1 - github.com/google/go-cmp v0.3.1 // indirect + github.com/go-sql-driver/mysql v1.5.0 github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e // indirect github.com/olivere/elastic v6.2.22+incompatible github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect diff --git a/main.go b/main.go index cac3094..11e180b 100644 --- a/main.go +++ b/main.go @@ -4,10 +4,10 @@ import ( "beeblog/filter" "beeblog/models" _ "beeblog/routers" - "github.com/astaxie/beego" - "github.com/astaxie/beego/logs" - "github.com/astaxie/beego/orm" - "github.com/astaxie/beego/plugins/cors" + "github.com/beego/beego/v2/client/orm" + "github.com/beego/beego/v2/core/logs" + beego "github.com/beego/beego/v2/server/web" + "github.com/beego/beego/v2/server/web/filter/cors" ) func init() { @@ -17,8 +17,8 @@ func init() { } func main() { orm.Debug = false - orm.RunSyncdb("default", false, true) - beego.AddFuncMap("NAdd",NAdd) + + beego.AddFuncMap("NAdd", NAdd) logs.SetLogger(logs.AdapterFile, `{"filename":"/opt/logs/aiprose.log","level":1}`) beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{ @@ -31,6 +31,6 @@ func main() { beego.Run() } -func NAdd(n1 int, n2 int) int{ +func NAdd(n1 int, n2 int) int { return n1 + n2 } diff --git a/mcontrollers/MBlogController.go b/mcontrollers/MBlogController.go index acd4440..18bd557 100644 --- a/mcontrollers/MBlogController.go +++ b/mcontrollers/MBlogController.go @@ -3,7 +3,7 @@ package mcontrollers import ( "beeblog/models" "beeblog/service" - "github.com/astaxie/beego" + beego "github.com/beego/beego/v2/server/web" "strconv" "time" ) @@ -12,7 +12,6 @@ type MBlogController struct { beego.Controller } - func (this *MBlogController) Get() { blogService := service.BlogService{} userService := service.UserService{} @@ -24,14 +23,13 @@ func (this *MBlogController) Get() { this.ServeJSON() return } - this.Data["json"] = models.ReurnData("",blog) + this.Data["json"] = models.ReurnData("", blog) this.ServeJSON() userService.CountBrows(blog.UserId) blogService.EditBlogBrows(id) return } - func (this *MBlogController) BlogsPage() { blogService := service.BlogService{} num, _ := this.GetInt("num") @@ -53,12 +51,11 @@ func (this *MBlogController) BlogsPage() { this.ServeJSON() return } - this.Data["json"] = models.ReurnData("",pages) + this.Data["json"] = models.ReurnData("", pages) this.ServeJSON() return } - func (this *MBlogController) EditPage() { blogService := service.BlogService{} uid := this.GetSession("userid") @@ -98,7 +95,7 @@ func (this *MBlogController) Save() { blog := &models.Blog{Title: title, BlogHtml: blogHtml, CategoryId: catoryId, UserId: uid.(int64)} err := blogService.SaveBlog(blog, labels) if err == nil { - this.Data["json"] = models.ReurnData("",blog.Id) + this.Data["json"] = models.ReurnData("", blog.Id) } else { this.Data["json"] = models.ReurnError(500, "保存失败") } @@ -116,13 +113,13 @@ func (this *MBlogController) Edit() { this.ServeJSON() return } - id,_ := this.GetInt64("id") + id, _ := this.GetInt64("id") title := this.GetString("title") blogHtml := this.GetString("blogHtml") catory := this.GetString("catory") catoryId, _ := strconv.ParseInt(catory, 10, 64) labels := this.GetStrings("labels[]") - blog,err :=blogService.GetBlog(id) + blog, err := blogService.GetBlog(id) if err != nil { this.Data["json"] = models.ReurnError(500, "保存失败") this.ServeJSON() @@ -185,4 +182,4 @@ func (this *MBlogController) New() { return } this.TplName = "newblog.html" -} \ No newline at end of file +} diff --git a/models/DataInit.go b/models/DataInit.go index def4bff..a3a1ae8 100644 --- a/models/DataInit.go +++ b/models/DataInit.go @@ -1,29 +1,18 @@ package models import ( - "github.com/astaxie/beego" - //"github.com/Unknwon/com" - //"os" - //"path" - "github.com/astaxie/beego/orm" - //_ "github.com/mattn/go-sqlite3" + "fmt" + "github.com/beego/beego/v2/client/orm" + "github.com/beego/beego/v2/core/config" _ "github.com/go-sql-driver/mysql" ) -const ( - _DB_NAME = "data/beeblog.db" - _SQLITE3_DRIVER = "sqlite3" -) - func RegistDB() { - //if !com.IsExist(_DB_NAME){ - // os.MkdirAll(path.Dir(_DB_NAME),os.ModePerm) - // os.Create(_DB_NAME) - //} - //orm.RegisterModel(new(Attachment),new(Topic)) - //orm.RegisterDriver(_SQLITE3_DRIVER,orm.DRSqlite) - //orm.RegisterDataBase("default",_SQLITE3_DRIVER,_DB_NAME,10) - + dbUrl, _ := config.String("dburl") + fmt.Println(dbUrl) + orm.RegisterDriver("mysql", orm.DRMySQL) + orm.RegisterDataBase("default", "mysql", "root:"+dbUrl+"/beeblog?charset=utf8&loc=Local") orm.RegisterModel(new(User), new(Blog), new(NLabel), new(Note), new(NoteColl), new(Category), new(Like), new(Comment)) - orm.RegisterDataBase("default", "mysql", "root:"+beego.AppConfig.String("dburl")+"/beeblog?charset=utf8&loc=Local", 30) + + //orm.RunSyncdb("default", false, true) } diff --git a/routers/BlogRouter.go b/routers/BlogRouter.go index 623b002..14ad250 100644 --- a/routers/BlogRouter.go +++ b/routers/BlogRouter.go @@ -2,7 +2,7 @@ package routers import ( "beeblog/controllers" - "github.com/astaxie/beego" + beego "github.com/beego/beego/v2/server/web" ) func init() { diff --git a/routers/CatRouter.go b/routers/CatRouter.go index b6f1675..c2f83c1 100644 --- a/routers/CatRouter.go +++ b/routers/CatRouter.go @@ -2,7 +2,7 @@ package routers import ( "beeblog/controllers" - "github.com/astaxie/beego" + beego "github.com/beego/beego/v2/server/web" ) func init() { diff --git a/routers/CommentRouter.go b/routers/CommentRouter.go index 25876b4..8c435b5 100644 --- a/routers/CommentRouter.go +++ b/routers/CommentRouter.go @@ -1,11 +1,11 @@ package routers import ( - "github.com/astaxie/beego" "beeblog/controllers" + beego "github.com/beego/beego/v2/server/web" ) func init() { beego.Router("/api/comms/save", &controllers.CommentController{}, "post:Save") beego.Router("/api/comms/del/:id([0-9]+)", &controllers.CommentController{}, "get:Del") -} \ No newline at end of file +} diff --git a/routers/LikeRouter.go b/routers/LikeRouter.go index e23c8f9..4791d25 100644 --- a/routers/LikeRouter.go +++ b/routers/LikeRouter.go @@ -2,7 +2,7 @@ package routers import ( "beeblog/controllers" - "github.com/astaxie/beego" + beego "github.com/beego/beego/v2/server/web" ) func init() { diff --git a/routers/MBlogRouter.go b/routers/MBlogRouter.go index b22e2eb..b58063b 100644 --- a/routers/MBlogRouter.go +++ b/routers/MBlogRouter.go @@ -2,7 +2,7 @@ package routers import ( "beeblog/mcontrollers" - "github.com/astaxie/beego" + beego "github.com/beego/beego/v2/server/web" ) func init() { diff --git a/routers/NoteRouter.go b/routers/NoteRouter.go index 887accc..f91cfff 100644 --- a/routers/NoteRouter.go +++ b/routers/NoteRouter.go @@ -1,8 +1,8 @@ package routers import ( - "github.com/astaxie/beego" "beeblog/controllers" + beego "github.com/beego/beego/v2/server/web" ) func init() { diff --git a/routers/UPageRouter.go b/routers/UPageRouter.go index d8c66ec..1696f2c 100644 --- a/routers/UPageRouter.go +++ b/routers/UPageRouter.go @@ -2,7 +2,7 @@ package routers import ( "beeblog/controllers" - "github.com/astaxie/beego" + beego "github.com/beego/beego/v2/server/web" ) func init() { diff --git a/routers/UserRouter.go b/routers/UserRouter.go index 61325c1..c146674 100644 --- a/routers/UserRouter.go +++ b/routers/UserRouter.go @@ -2,7 +2,7 @@ package routers import ( "beeblog/controllers" - "github.com/astaxie/beego" + beego "github.com/beego/beego/v2/server/web" ) func init() { diff --git a/routers/router.go b/routers/router.go index 1f5be24..31788ad 100644 --- a/routers/router.go +++ b/routers/router.go @@ -2,16 +2,16 @@ package routers import ( "beeblog/controllers" - "github.com/astaxie/beego" + beego "github.com/beego/beego/v2/server/web" ) func init() { beego.Router("/", &controllers.IndexController{}) - beego.Router("/404", &controllers.PageController{},"get:PageNotFound") - beego.Router("/500", &controllers.PageController{},"get:ServerError") + beego.Router("/404", &controllers.PageController{}, "get:PageNotFound") + beego.Router("/500", &controllers.PageController{}, "get:ServerError") beego.Router("/map", &controllers.MapController{}) - beego.Router("/us", &controllers.PageController{},"get:UsPage") - beego.Router("/iframe/user", &controllers.PageController{},"get:IframeUser") + beego.Router("/us", &controllers.PageController{}, "get:UsPage") + beego.Router("/iframe/user", &controllers.PageController{}, "get:IframeUser") beego.Router("/file/upload", &controllers.FileController{}, "post:Upload") beego.Router("/himg/upload", &controllers.FileController{}, "post:HeadImgUpload") //beego.Include(&controllers.PageController{}) diff --git a/service/BlogService.go b/service/BlogService.go index 73ca55d..a0cb824 100644 --- a/service/BlogService.go +++ b/service/BlogService.go @@ -1,9 +1,9 @@ package service import ( - "github.com/astaxie/beego/orm" "beeblog/models" "beeblog/utils" + "github.com/beego/beego/v2/client/orm" ) type BlogService struct { @@ -133,7 +133,6 @@ func (this *BlogService) FindBlogs(num int, size int, cat int64, flag int) (*uti return page, nil } - func (this *BlogService) AllBlogs() ([]*models.Blog, error) { var blogs []*models.Blog @@ -147,13 +146,12 @@ func (this *BlogService) AllBlogs() ([]*models.Blog, error) { return blogs, nil } - func (this *BlogService) SaveBlog(blog *models.Blog, strs []string) error { o := orm.NewOrm() - o.Begin() + tx, _ := o.Begin() id, eror := o.Insert(blog) if eror != nil { - o.Rollback() + tx.Rollback() return eror } else { blog.Id = id @@ -163,21 +161,21 @@ func (this *BlogService) SaveBlog(blog *models.Blog, strs []string) error { nlabels[i] = &models.NLabel{Title: strs[i], BlogId: id, UserId: blog.UserId} } if _, err := o.InsertMulti(len(nlabels), nlabels); err != nil { - o.Rollback() + tx.Rollback() return err } } - o.Commit() + tx.Commit() } return nil } func (this *BlogService) EditBlog(blog *models.Blog, strs []string) error { o := orm.NewOrm() - o.Begin() + tx, _ := o.Begin() _, eror := o.Update(blog) if eror != nil { - o.Rollback() + tx.Rollback() return eror } else { o.QueryTable(models.NLabel{}).Filter("BlogId", blog.Id).Delete() @@ -187,11 +185,11 @@ func (this *BlogService) EditBlog(blog *models.Blog, strs []string) error { nlabels[i] = &models.NLabel{Title: strs[i], BlogId: blog.Id, UserId: blog.UserId} } if _, err := o.InsertMulti(len(nlabels), nlabels); err != nil { - o.Rollback() + tx.Rollback() return err } } - o.Commit() + tx.Commit() } return nil } diff --git a/service/CategoryService.go b/service/CategoryService.go index 6654c78..2ac425f 100644 --- a/service/CategoryService.go +++ b/service/CategoryService.go @@ -1,8 +1,8 @@ package service import ( - "github.com/astaxie/beego/orm" "beeblog/models" + "github.com/beego/beego/v2/client/orm" ) type CategoryService struct { diff --git a/service/CommentService.go b/service/CommentService.go index 812e3fa..04b0570 100644 --- a/service/CommentService.go +++ b/service/CommentService.go @@ -2,8 +2,8 @@ package service import ( "beeblog/models" - "github.com/astaxie/beego/orm" "fmt" + "github.com/beego/beego/v2/client/orm" ) type CommentService struct { @@ -12,7 +12,7 @@ type CommentService struct { func (this *CommentService) FindCommentByBlog(bid int64) ([]*models.Comment, error) { var comms []*models.Comment o := orm.NewOrm() - _, err := o.QueryTable(&models.Comment{}).Filter("Pid", 0).Filter("BlogId",bid).OrderBy("-Ctime").All(&comms) + _, err := o.QueryTable(&models.Comment{}).Filter("Pid", 0).Filter("BlogId", bid).OrderBy("-Ctime").All(&comms) if err != nil { return nil, err } @@ -21,9 +21,9 @@ func (this *CommentService) FindCommentByBlog(bid int64) ([]*models.Comment, err var childs []*models.Comment _, childerrr := o.QueryTable(&models.Comment{}).Filter("Pid", comms[i].Id).OrderBy("-Ctime").All(&childs) if childerrr == nil { - if len(childs) > 0{ + if len(childs) > 0 { comms[i].Childs = childs - for j:=0 ; j