From bec263bf0c684316909f4cf32a09d014481619ae Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 22 Apr 2020 18:30:27 +0800 Subject: [PATCH] =?UTF-8?q?:building=5Fconstruction:=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=20PostgreSQL=20=E6=95=B0=E6=8D=AE=E5=BA=93=20#31=20Where=20?= =?UTF-8?q?=E5=AD=90=E5=8F=A5=E4=BD=BF=E7=94=A8=E7=BB=93=E6=9E=84=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/articlesrv.go | 65 ++++++++++++++++++++-------------------- service/categorysrv.go | 20 ++++++------- service/commentsrv.go | 18 +++++------ service/exportsrv.go | 2 +- service/initsrv.go | 2 +- service/navigationsrv.go | 6 ++-- service/settingsrv.go | 10 +++---- service/statisticsrv.go | 16 +++++----- service/tagsrv.go | 6 ++-- service/upgradesrv.go | 12 ++++---- 10 files changed, 78 insertions(+), 79 deletions(-) diff --git a/service/articlesrv.go b/service/articlesrv.go index 6d8b0e17..7ebeda3f 100644 --- a/service/articlesrv.go +++ b/service/articlesrv.go @@ -44,8 +44,8 @@ const ( ) func (srv *articleService) GetPlatMostViewArticles(size int) (ret []*model.Article) { - if err := db.Model(&model.Article{}).Select(" id, created_at, author_id, title, path, view_count, comment_count, blog_id "). - Where(" status = ?", model.ArticleStatusOK). + if err := db.Model(&model.Article{}).Select("id, created_at, author_id, title, path, view_count, comment_count, blog_id"). + Where("status = ?", model.ArticleStatusOK). Order("view_count DESC, created_at DESC").Limit(size).Find(&ret).Error; nil != err { logger.Errorf("get platform most view articles failed: " + err.Error()) } @@ -54,7 +54,7 @@ func (srv *articleService) GetPlatMostViewArticles(size int) (ret []*model.Artic } func (srv *articleService) GetUnpushedArticles() (ret []*model.Article) { - if err := db.Where(" pushed_at <= ?", model.ZeroPushTime).Find(&ret).Error; nil != err { + if err := db.Where("pushed_at <= ?", model.ZeroPushTime).Find(&ret).Error; nil != err { return } @@ -67,7 +67,7 @@ func (srv *articleService) GetArchiveArticles(archiveID uint64, page int, blogID count := 0 var rels []*model.Correlation - if err := db.Where(" id2 = ? AND type = ? AND blog_id = ?", archiveID, model.CorrelationArticleArchive, blogID). + if err := db.Where(&model.Correlation{ID2: archiveID, Type: model.CorrelationArticleArchive, BlogID: blogID}). Find(&rels).Error; nil != err { return } @@ -78,7 +78,7 @@ func (srv *articleService) GetArchiveArticles(archiveID uint64, page int, blogID } if err := db.Model(&model.Article{}). - Where(" id IN (?) AND status = ? AND blog_id = ?", articleIDs, model.ArticleStatusOK, blogID). + Where("id IN (?) AND status = ? AND blog_id = ?", articleIDs, model.ArticleStatusOK, blogID). Order("topped DESC, created_at DESC").Count(&count). Offset(offset).Limit(pageSize). Find(&ret).Error; nil != err { @@ -92,7 +92,7 @@ func (srv *articleService) GetArchiveArticles(archiveID uint64, page int, blogID func (srv *articleService) GetPreviousArticle(id uint64, blogID uint64) *model.Article { ret := &model.Article{} - if err := db.Where("id < ? AND blog_id = ?", id, blogID).Order("created_at DESC").Limit(1).Find(ret).Error; nil != err { + if err := db.Where("id < ? AND blog_id = ?", id, blogID).Order("created_at DESC").Limit(1).Find(ret).Error; nil != err { return nil } @@ -101,7 +101,7 @@ func (srv *articleService) GetPreviousArticle(id uint64, blogID uint64) *model.A func (srv *articleService) GetNextArticle(id uint64, blogID uint64) *model.Article { ret := &model.Article{} - if err := db.Where(" id > ? AND blog_id = ?", id, blogID).Limit(1).Find(ret).Error; nil != err { + if err := db.Where("id > ? AND blog_id = ?", id, blogID).Limit(1).Find(ret).Error; nil != err { return nil } @@ -115,8 +115,8 @@ func (srv *articleService) GetArticleByPath(path string, blogID uint64) *model.A } path, _ = url.PathUnescape(path) - ret := &model.Article{} - if err := db.Where(" path = ? AND blog_id = ?", path, blogID).Find(ret).Error; nil != err { + ret := &model.Article{Path: path, BlogID: blogID} + if err := db.Where(ret).Find(ret).Error; nil != err { return nil } @@ -162,9 +162,8 @@ func (srv *articleService) AddArticle(article *model.Article) (err error) { if err = tx.Model(author).Updates(author).Error; nil != err { return } - blogUserRel := &model.Correlation{} - if err = tx.Where(" id1 = ? AND id2 = ? AND type = ? AND blog_id = ?", - article.BlogID, author.ID, model.CorrelationBlogUser, article.BlogID).First(blogUserRel).Error; nil != err { + blogUserRel := &model.Correlation{ID1: article.BlogID, ID2: author.ID, Type: model.CorrelationBlogUser, BlogID: article.BlogID} + if err = tx.Where(blogUserRel).First(blogUserRel).Error; nil != err { return } blogUserRel.Int2 += 1 @@ -189,7 +188,7 @@ func (srv *articleService) ConsoleGetArticles(keyword string, page int, blogID u whereArgs = append(whereArgs, "%"+keyword+"%") } - if err := db.Model(&model.Article{}).Select(" id , created_at , author_id , title , tags , path , topped , view_count , comment_count "). + if err := db.Model(&model.Article{}).Select("id, created_at, author_id, title, tags, path, topped, view_count, comment_count"). Where(where, whereArgs...). Order("topped DESC, created_at DESC").Count(&count). Offset(offset).Limit(adminConsoleArticleListPageSize).Find(&ret).Error; nil != err { @@ -213,7 +212,7 @@ func (srv *articleService) GetArticles(keyword string, page int, blogID uint64) whereArgs = append(whereArgs, "%"+keyword+"%") } - if err := db.Model(&model.Article{}).Select(" id , created_at , author_id , title , abstract , content , tags , path , topped , view_count , comment_count "). + if err := db.Model(&model.Article{}).Select("id, created_at, author_id, title, abstract, content, tags, path, topped, view_count, comment_count"). Where(where, whereArgs...). Order("topped DESC, created_at DESC").Count(&count). Offset(offset).Limit(pageSize). @@ -231,7 +230,7 @@ func (srv *articleService) GetCategoryArticles(categoryID uint64, page int, blog offset := (page - 1) * pageSize var rels []*model.Correlation - if err := db.Model(&model.Correlation{}).Where(" id1 = ? AND type = ? AND blog_id = ?", categoryID, model.CorrelationCategoryTag, blogID). + if err := db.Model(&model.Correlation{}).Where("id1 = ? AND type = ? AND blog_id = ?", categoryID, model.CorrelationCategoryTag, blogID). Find(&rels).Error; nil != err { return } @@ -243,7 +242,7 @@ func (srv *articleService) GetCategoryArticles(categoryID uint64, page int, blog count := 0 rels = []*model.Correlation{} - if err := db.Model(&model.Correlation{}).Where(" id2 IN (?) AND type = ? AND blog_id = ?", tagIDs, model.CorrelationArticleTag, blogID). + if err := db.Model(&model.Correlation{}).Where("id2 IN (?) AND type = ? AND blog_id = ?", tagIDs, model.CorrelationArticleTag, blogID). Order("id DESC").Count(&count).Offset(offset).Limit(pageSize). Find(&rels).Error; nil != err { return @@ -256,7 +255,7 @@ func (srv *articleService) GetCategoryArticles(categoryID uint64, page int, blog articleIDs = append(articleIDs, articleTagRel.ID1) } - if err := db.Where(" id IN (?) AND blog_id = ?", articleIDs, blogID).Find(&ret).Error; nil != err { + if err := db.Where("id IN (?) AND blog_id = ?", articleIDs, blogID).Find(&ret).Error; nil != err { return } @@ -269,7 +268,7 @@ func (srv *articleService) GetTagArticles(tagID uint64, page int, blogID uint64) count := 0 var rels []*model.Correlation - if err := db.Where(" id2 = ? AND type = ? AND blog_id = ?", tagID, model.CorrelationArticleTag, blogID). + if err := db.Where("id2 = ? AND type = ? AND blog_id = ?", tagID, model.CorrelationArticleTag, blogID). Find(&rels).Error; nil != err { return } @@ -280,7 +279,7 @@ func (srv *articleService) GetTagArticles(tagID uint64, page int, blogID uint64) } if err := db.Model(&model.Article{}). - Where(" id IN (?) AND status = ? AND blog_id = ?", articleIDs, model.ArticleStatusOK, blogID). + Where("id IN (?) AND status = ? AND blog_id = ?", articleIDs, model.ArticleStatusOK, blogID). Order("topped DESC, created_at DESC").Count(&count).Offset(offset).Limit(pageSize). Find(&ret).Error; nil != err { logger.Errorf("get tag articles failed: " + err.Error()) @@ -297,7 +296,7 @@ func (srv *articleService) GetAuthorArticles(authorID uint64, page int, blogID u count := 0 if err := db.Model(&model.Article{}). - Where(" author_id = ? AND status = ? AND blog_id = ?", authorID, model.ArticleStatusOK, blogID). + Where("author_id = ? AND status = ? AND blog_id = ?", authorID, model.ArticleStatusOK, blogID). Order("topped DESC, created_at DESC").Count(&count). Offset(offset).Limit(pageSize). Find(&ret).Error; nil != err { @@ -311,7 +310,7 @@ func (srv *articleService) GetAuthorArticles(authorID uint64, page int, blogID u func (srv *articleService) GetMostViewArticles(size int, blogID uint64) (ret []*model.Article) { if err := db.Model(&model.Article{}).Select(" id , created_at , author_id , title , path "). - Where(" status = ? AND blog_id = ?", model.ArticleStatusOK, blogID). + Where("status = ? AND blog_id = ?", model.ArticleStatusOK, blogID). Order("view_count DESC, created_at DESC").Limit(size).Find(&ret).Error; nil != err { logger.Errorf("get most view articles failed: " + err.Error()) } @@ -321,7 +320,7 @@ func (srv *articleService) GetMostViewArticles(size int, blogID uint64) (ret []* func (srv *articleService) GetMostCommentArticles(size int, blogID uint64) (ret []*model.Article) { if err := db.Model(&model.Article{}).Select(" id , created_at , author_id , title , path "). - Where(" status = ? AND blog_id = ?", model.ArticleStatusOK, blogID). + Where("status = ? AND blog_id = ?", model.ArticleStatusOK, blogID). Order("comment_count DESC, id DESC").Limit(size).Find(&ret).Error; nil != err { logger.Errorf("get most comment articles failed: " + err.Error()) } @@ -398,7 +397,7 @@ func (srv *articleService) RemoveArticle(id, blogID uint64) (err error) { tx.Rollback() } }() - if err = tx.Where(" id = ? AND blog_id = ?", id, blogID).Find(article).Error; nil != err { + if err = tx.Where("id = ? AND blog_id = ?", id, blogID).Find(article).Error; nil != err { return } author := &model.User{} @@ -410,7 +409,7 @@ func (srv *articleService) RemoveArticle(id, blogID uint64) (err error) { return } blogUserRel := &model.Correlation{} - if err = tx.Where(" id1 = ? AND id2 = ? AND type = ? AND blog_id = ?", + if err = tx.Where("id1 = ? AND id2 = ? AND type = ? AND blog_id = ?", article.BlogID, author.ID, model.CorrelationBlogUser, article.BlogID).First(blogUserRel).Error; nil != err { return } @@ -431,11 +430,11 @@ func (srv *articleService) RemoveArticle(id, blogID uint64) (err error) { return } var comments []*model.Comment - if err = tx.Model(&model.Comment{}).Where(" article_id = ? AND blog_id = ?", id, article.BlogID).Find(&comments).Error; nil != err { + if err = tx.Model(&model.Comment{}).Where("article_id = ? AND blog_id = ?", id, article.BlogID).Find(&comments).Error; nil != err { return } if 0 < len(comments) { - if err = tx.Where(" article_id = ? AND blog_id = ?", id, article.BlogID).Delete(&model.Comment{}).Error; nil != err { + if err = tx.Where("article_id = ? AND blog_id = ?", id, article.BlogID).Delete(&model.Comment{}).Error; nil != err { return } for range comments { @@ -462,7 +461,7 @@ func (srv *articleService) UpdateArticle(article *model.Article) (err error) { defer srv.mutex.Unlock() oldArticle := &model.Article{} - if err = db.Model(&model.Article{}).Where(" id = ? AND blog_id = ?", article.ID, article.BlogID). + if err = db.Model(&model.Article{}).Where("id = ? AND blog_id = ?", article.ID, article.BlogID). Find(oldArticle).Error; nil != err { return } @@ -538,7 +537,7 @@ func normalizeArticle(article *model.Article) error { return errors.New("title can not be empty") } count := 0 - if err := db.Model(&model.Article{}).Where(" title = ?", title).Count(&count).Error; nil != err { + if err := db.Model(&model.Article{}).Where("title = ?", title).Count(&count).Error; nil != err { return err } if 0 < count { @@ -601,13 +600,13 @@ func normalizeTagStr(tagStr string) string { func removeTagArticleRels(tx *gorm.DB, article *model.Article) error { var rels []*model.Correlation - if err := tx.Where(" id1 = ? AND type = ? AND blog_id = ?", + if err := tx.Where("id1 = ? AND type = ? AND blog_id = ?", article.ID, model.CorrelationArticleTag, article.BlogID).Find(&rels).Error; nil != err { return err } for _, rel := range rels { tag := &model.Tag{} - if err := tx.Where(" id = ? AND blog_id = ?", rel.ID2, article.BlogID).First(tag).Error; nil != err { + if err := tx.Where("id = ? AND blog_id = ?", rel.ID2, article.BlogID).First(tag).Error; nil != err { continue } tag.ArticleCount = tag.ArticleCount - 1 @@ -616,7 +615,7 @@ func removeTagArticleRels(tx *gorm.DB, article *model.Article) error { } } - if err := tx.Where(" id1 = ? AND type = ? AND blog_id = ?", article.ID, model.CorrelationArticleTag, article.BlogID). + if err := tx.Where("id1 = ? AND type = ? AND blog_id = ?", article.ID, model.CorrelationArticleTag, article.BlogID). Delete(&model.Correlation{}).Error; nil != err { return err } @@ -628,7 +627,7 @@ func tagArticle(tx *gorm.DB, article *model.Article) error { tags := strings.Split(article.Tags, ",") for _, tagTitle := range tags { tag := &model.Tag{BlogID: article.BlogID} - tx.Where(" title = ? AND blog_id = ?", tagTitle, article.BlogID).First(tag) + tx.Where("title = ? AND blog_id = ?", tagTitle, article.BlogID).First(tag) if "" == tag.Title { tag.Title = tagTitle tag.ArticleCount = 1 @@ -678,7 +677,7 @@ func normalizeArticlePath(article *model.Article) error { } count := 0 - if db.Model(&model.Article{}).Where(" path = ? AND id != ? AND blog_id = ?", path, article.ID, article.BlogID).Count(&count); 0 < count { + if db.Model(&model.Article{}).Where("path = ? AND id != ? AND blog_id = ?", path, article.ID, article.BlogID).Count(&count); 0 < count { return errors.New("path [" + path + "] is reduplicated") } diff --git a/service/categorysrv.go b/service/categorysrv.go index 91ca9551..48b253b9 100644 --- a/service/categorysrv.go +++ b/service/categorysrv.go @@ -79,7 +79,7 @@ func (srv *categoryService) GetCategoryByPath(path string, blogID uint64) *model path, _ = url.PathUnescape(path) ret := &model.Category{} - if err := db.Where(" path = ? AND blog_ID = ?", path, blogID).First(ret).Error; nil != err { + if err := db.Where("path = ? AND blog_ID = ?", path, blogID).First(ret).Error; nil != err { return nil } @@ -91,7 +91,7 @@ func (srv *categoryService) UpdateCategory(category *model.Category) error { defer srv.mutex.Unlock() count := 0 - if db.Model(&model.Category{}).Where(" id = ? AND blog_id = ?", category.ID, category.BlogID). + if db.Model(&model.Category{}).Where("id = ? AND blog_id = ?", category.ID, category.BlogID). Count(&count); 1 > count { return fmt.Errorf("not found category [id=%d] to update", category.ID) } @@ -109,7 +109,7 @@ func (srv *categoryService) UpdateCategory(category *model.Category) error { return err } - if err := tx.Where(" id1 = ? AND type = ? AND blog_id = ?", + if err := tx.Where("id1 = ? AND type = ? AND blog_id = ?", category.ID, model.CorrelationCategoryTag, category.BlogID).Delete(model.Correlation{}).Error; nil != err { tx.Rollback() @@ -162,7 +162,7 @@ func (srv *categoryService) AddCategory(category *model.Category) error { } func (srv *categoryService) GetCategoriesByTag(tagTitle string, blogID uint64) (ret []*model.Category) { - if err := db.Where(" blog_id = ? AND tags LIKE ?", blogID, tagTitle).Find(&ret).Error; nil != err { + if err := db.Where("blog_id = ? AND tags LIKE ?", blogID, tagTitle).Find(&ret).Error; nil != err { logger.Errorf("get categories failed: " + err.Error()) } @@ -173,7 +173,7 @@ func (srv *categoryService) ConsoleGetCategories(page int, blogID uint64) (ret [ offset := (page - 1) * adminConsoleCategoryListPageSize count := 0 if err := db.Model(&model.Category{}).Order("number ASC, id DESC"). - Where(" blog_id = ?", blogID). + Where("blog_id = ?", blogID). Count(&count).Offset(offset).Limit(adminConsoleCategoryListPageSize).Find(&ret).Error; nil != err { logger.Errorf("get categories failed: " + err.Error()) } @@ -184,7 +184,7 @@ func (srv *categoryService) ConsoleGetCategories(page int, blogID uint64) (ret [ } func (srv *categoryService) GetCategories(size int, blogID uint64) (ret []*model.Category) { - if err := db.Where("blog_id = ?", blogID).Order("number asc").Limit(size).Find(&ret).Error; nil != err { + if err := db.Where("blog_id = ?", blogID).Order("number asc").Limit(size).Find(&ret).Error; nil != err { logger.Errorf("get categories failed: " + err.Error()) } @@ -198,11 +198,11 @@ func (srv *categoryService) RemoveCategory(id, blogID uint64) error { category := &model.Category{} tx := db.Begin() - if err := tx.Where(" id = ? AND blog_id = ?", id, blogID).Find(category).Error; nil != err { + if err := tx.Where("id = ? AND blog_id = ?", id, blogID).Find(category).Error; nil != err { return err } - if err := tx.Where(" id1 = ? AND type = ? AND blog_id = ?", + if err := tx.Where("id1 = ? AND type = ? AND blog_id = ?", category.ID, model.CorrelationCategoryTag, category.BlogID).Delete(model.Correlation{}).Error; nil != err { tx.Rollback() @@ -229,7 +229,7 @@ func normalizeCategoryPath(category *model.Category) error { } count := 0 - if db.Model(&model.Category{}).Where(" path = ? AND id != ? AND blog_id = ?", path, category.ID, category.BlogID).Count(&count); 0 < count { + if db.Model(&model.Category{}).Where("path = ? AND id != ? AND blog_id = ?", path, category.ID, category.BlogID).Count(&count); 0 < count { return errors.New("path is reduplicated") } @@ -242,7 +242,7 @@ func tagCategory(tx *gorm.DB, category *model.Category) error { tags := strings.Split(category.Tags, ",") for _, tagTitle := range tags { tag := &model.Tag{BlogID: category.BlogID} - tx.Where(" title = ? AND blog_id = ?", tagTitle, category.BlogID).First(tag) + tx.Where("title = ? AND blog_id = ?", tagTitle, category.BlogID).First(tag) if "" == tag.Title { tag.Title = tagTitle if err := tx.Create(tag).Error; nil != err { diff --git a/service/commentsrv.go b/service/commentsrv.go index e9528bcb..8630486a 100644 --- a/service/commentsrv.go +++ b/service/commentsrv.go @@ -51,7 +51,7 @@ func (srv *commentService) UpdatePushedAt(comment *model.Comment) error { } func (srv *commentService) GetUnpushedComments() (ret []*model.Comment) { - if err := db.Where(" pushed_at <= ?", model.ZeroPushTime).Find(&ret).Error; nil != err { + if err := db.Where("pushed_at <= ?", model.ZeroPushTime).Find(&ret).Error; nil != err { return } @@ -69,7 +69,7 @@ func (srv *commentService) GetComment(commentID uint64) *model.Comment { func (srv *commentService) GetCommentPage(articleID, commentID uint64, blogID uint64) int { count := 0 - if err := db.Model(&model.Comment{}).Where(" article_id = ? AND id < ? AND blog_id = ?", articleID, commentID, blogID). + if err := db.Model(&model.Comment{}).Where("article_id = ? AND id < ? AND blog_id = ?", articleID, commentID, blogID). Count(&count).Error; nil != err { return 1 } @@ -79,7 +79,7 @@ func (srv *commentService) GetCommentPage(articleID, commentID uint64, blogID ui func (srv *commentService) GetRepliesCount(parentCommentID uint64, blogID uint64) int { ret := 0 - if err := db.Model(&model.Comment{}).Where(" parent_comment_id = ? AND blog_id = ?", parentCommentID, blogID).Count(&ret).Error; nil != err { + if err := db.Model(&model.Comment{}).Where("parent_comment_id = ? AND blog_id = ?", parentCommentID, blogID).Count(&ret).Error; nil != err { logger.Errorf("count comment [id=%d]'s replies failed: "+err.Error(), parentCommentID) } @@ -87,7 +87,7 @@ func (srv *commentService) GetRepliesCount(parentCommentID uint64, blogID uint64 } func (srv *commentService) GetReplies(parentCommentID uint64, blogID uint64) (ret []*model.Comment) { - if err := db.Where(" parent_comment_id = ? AND blog_id = ?", parentCommentID, blogID).Find(&ret).Error; nil != err { + if err := db.Where("parent_comment_id = ? AND blog_id = ?", parentCommentID, blogID).Find(&ret).Error; nil != err { logger.Errorf("get comment [id=%d]'s replies failed: "+err.Error(), parentCommentID) } @@ -101,7 +101,7 @@ func (srv *commentService) ConsoleGetComments(keyword string, page int, blogID u where := " blog_id = ?" whereArgs := []interface{}{blogID} if "" != keyword { - where += " AND content LIKE ?" + where += " AND content LIKE ?" whereArgs = append(whereArgs, "%"+keyword+"%") } @@ -117,8 +117,8 @@ func (srv *commentService) ConsoleGetComments(keyword string, page int, blogID u } func (srv *commentService) GetRecentComments(size int, blogID uint64) (ret []*model.Comment) { - if err := db.Model(&model.Comment{}).Select(" id , created_at , content , author_id , article_id , author_name , author_avatar_url , author_url "). - Where(" blog_id = ?", blogID). + if err := db.Model(&model.Comment{}).Select("id, created_at, content, author_id, article_id, author_name, author_avatar_url, author_url"). + Where("blog_id = ?", blogID). Order("created_at DESC, id DESC").Limit(size).Find(&ret).Error; nil != err { logger.Errorf("get recent comments failed: " + err.Error()) } @@ -130,7 +130,7 @@ func (srv *commentService) GetArticleComments(articleID uint64, page int, blogID offset := (page - 1) * themeCommentListPageSize count := 0 if err := db.Model(&model.Comment{}).Order("id ASC"). - Where(" article_id = ? AND blog_id = ?", articleID, blogID). + Where("article_id = ? AND blog_id = ?", articleID, blogID). Count(&count).Offset(offset).Limit(themeCommentListPageSize).Find(&ret).Error; nil != err { logger.Errorf("get comments failed: " + err.Error()) } @@ -176,7 +176,7 @@ func (srv *commentService) RemoveComment(id, blogID uint64) error { comment := &model.Comment{} tx := db.Begin() - if err := tx.Where(" id = ? AND blog_id = ?", id, blogID).Find(comment).Error; nil != err { + if err := tx.Where("id = ? AND blog_id = ?", id, blogID).Find(comment).Error; nil != err { tx.Rollback() return err diff --git a/service/exportsrv.go b/service/exportsrv.go index 83d332f4..9af8ff2e 100644 --- a/service/exportsrv.go +++ b/service/exportsrv.go @@ -26,7 +26,7 @@ type exportService struct { func (srv *exportService) ExportMarkdowns(blogID uint64) (ret []*MarkdownFile) { var articles []*model.Article - if err := db.Where(" blog_id = ?", blogID).Find(&articles).Error; nil != err { + if err := db.Where("blog_id = ?", blogID).Find(&articles).Error; nil != err { logger.Errorf("export markdowns failed: " + err.Error()) return diff --git a/service/initsrv.go b/service/initsrv.go index e3f9bee3..973571f7 100644 --- a/service/initsrv.go +++ b/service/initsrv.go @@ -151,7 +151,7 @@ func (srv *initService) InitPlatform(platformAdmin *model.User) error { blogID := uint64(1) saCount := 0 - db.Model(&model.Correlation{}).Where(" id1 = ? AND type = ? AND int1 = ? AND blog_id = ?", + db.Model(&model.Correlation{}).Where("id1 = ? AND type = ? AND int1 = ? AND blog_id = ?", blogID, model.CorrelationBlogUser, model.UserRoleBlogAdmin, blogID). Count(&saCount) if 0 < saCount { diff --git a/service/navigationsrv.go b/service/navigationsrv.go index dce82dab..139fffdd 100644 --- a/service/navigationsrv.go +++ b/service/navigationsrv.go @@ -55,7 +55,7 @@ func (srv *navigationService) RemoveNavigation(id, blogID uint64) error { navigation := &model.Navigation{} tx := db.Begin() - if err := tx.Where(" id = ? AND blog_id = ?", id, blogID).Find(navigation).Error; nil != err { + if err := tx.Where("id = ? AND blog_id = ?", id, blogID).Find(navigation).Error; nil != err { tx.Rollback() return err @@ -75,7 +75,7 @@ func (srv *navigationService) UpdateNavigation(navigation *model.Navigation) err defer srv.mutex.Unlock() count := 0 - if db.Model(&model.Navigation{}).Where(" id = ? AND blog_id = ?", navigation.ID, navigation.BlogID). + if db.Model(&model.Navigation{}).Where("id = ? AND blog_id = ?", navigation.ID, navigation.BlogID). Count(&count); 1 > count { return fmt.Errorf("not found navigation [id=%d] to update", navigation.ID) } @@ -111,7 +111,7 @@ func (srv *navigationService) ConsoleGetNavigations(page int, blogID uint64) (re func (srv *navigationService) GetNavigations(blogID uint64) (ret []*model.Navigation) { if err := db.Model(&model.Navigation{}).Order("number ASC, id DESC"). - Where("blog_id = ?", blogID).Find(&ret).Error; nil != err { + Where("blog_id = ?", blogID).Find(&ret).Error; nil != err { logger.Errorf("get navigations failed: " + err.Error()) } diff --git a/service/settingsrv.go b/service/settingsrv.go index 13fd6897..ad7dae50 100644 --- a/service/settingsrv.go +++ b/service/settingsrv.go @@ -33,7 +33,7 @@ func (srv *settingService) GetSetting(category, name string, blogID uint64) *mod } ret = &model.Setting{} - if err := db.Where(" category = ? AND name = ? AND blog_id = ?", category, name, blogID).Find(ret).Error; nil != err { + if err := db.Where("category = ? AND name = ? AND blog_id = ?", category, name, blogID).Find(ret).Error; nil != err { return nil } @@ -45,7 +45,7 @@ func (srv *settingService) GetSetting(category, name string, blogID uint64) *mod func (srv *settingService) GetCategorySettings(category string, blogID uint64) []*model.Setting { var ret []*model.Setting - if err := db.Where(" category = ? AND blog_id = ?", category, blogID).Find(&ret).Error; nil != err { + if err := db.Where("category = ? AND blog_id = ?", category, blogID).Find(&ret).Error; nil != err { return nil } @@ -55,7 +55,7 @@ func (srv *settingService) GetCategorySettings(category string, blogID uint64) [ func (srv *settingService) GetAllSettings(blogID uint64) []*model.Setting { var ret []*model.Setting - if err := db.Where(" category != ? AND blog_id = ?", model.SettingCategoryStatistic, blogID).Find(&ret).Error; nil != err { + if err := db.Where("category != ? AND blog_id = ?", model.SettingCategoryStatistic, blogID).Find(&ret).Error; nil != err { return nil } @@ -65,7 +65,7 @@ func (srv *settingService) GetAllSettings(blogID uint64) []*model.Setting { func (srv *settingService) GetSettings(category string, names []string, blogID uint64) map[string]*model.Setting { ret := map[string]*model.Setting{} var settings []*model.Setting - if err := db.Where(" category = ? AND name IN (?) AND blog_id = ?", category, names, blogID).Find(&settings).Error; nil != err { + if err := db.Where("category = ? AND name IN (?) AND blog_id = ?", category, names, blogID).Find(&settings).Error; nil != err { return nil } @@ -99,7 +99,7 @@ func (srv *settingService) UpdateSettings(category string, settings []*model.Set tx := db.Begin() for _, setting := range settings { - if err := tx.Model(&model.Setting{}).Where(" category = ? AND name = ? AND blog_id = ?", + if err := tx.Model(&model.Setting{}).Where("category = ? AND name = ? AND blog_id = ?", category, setting.Name, blogID).Select("value").Updates(map[string]interface{}{"value": setting.Value}).Error; nil != err { tx.Rollback() diff --git a/service/statisticsrv.go b/service/statisticsrv.go index 0b85ad72..1b6e4684 100644 --- a/service/statisticsrv.go +++ b/service/statisticsrv.go @@ -30,7 +30,7 @@ type statisticService struct { func (srv *statisticService) GetAllStatistics(blogID uint64) []*model.Setting { var ret []*model.Setting - if err := db.Where(" category = ? AND blog_id = ?", model.SettingCategoryStatistic, blogID).Find(&ret).Error; nil != err { + if err := db.Where("category = ? AND blog_id = ?", model.SettingCategoryStatistic, blogID).Find(&ret).Error; nil != err { logger.Errorf("get all statistics failed: " + err.Error()) return nil @@ -41,7 +41,7 @@ func (srv *statisticService) GetAllStatistics(blogID uint64) []*model.Setting { func (srv *statisticService) GetStatistic(statisticName string, blogID uint) *model.Setting { ret := &model.Setting{} - if err := db.Where(" name = ? AND category = ? AND blog_id = ?", statisticName, model.SettingCategoryStatistic, blogID).Find(ret).Error; nil != err { + if err := db.Where("name = ? AND category = ? AND blog_id = ?", statisticName, model.SettingCategoryStatistic, blogID).Find(ret).Error; nil != err { logger.Errorf("get statistic failed: " + err.Error()) return nil @@ -53,7 +53,7 @@ func (srv *statisticService) GetStatistic(statisticName string, blogID uint) *mo func (srv *statisticService) GetStatistics(blogID uint, statisticNames ...string) map[string]*model.Setting { ret := map[string]*model.Setting{} var settings []*model.Setting - if err := db.Where(" name IN (?) AND category = ? AND blog_id = ?", statisticNames, model.SettingCategoryStatistic, blogID).Find(&settings).Error; nil != err { + if err := db.Where("name IN (?) AND category = ? AND blog_id = ?", statisticNames, model.SettingCategoryStatistic, blogID).Find(&settings).Error; nil != err { logger.Errorf("get statistics failed: " + err.Error()) return nil @@ -85,7 +85,7 @@ func (srv *statisticService) IncViewCountWithoutTx(tx *gorm.DB, blogID uint64) e defer srv.mutex.Unlock() setting := &model.Setting{} - if err := tx.Where(" name = ? AND category = ? AND blog_id = ?", model.SettingNameStatisticViewCount, model.SettingCategoryStatistic, blogID).Find(setting).Error; nil != err { + if err := tx.Where("name = ? AND category = ? AND blog_id = ?", model.SettingNameStatisticViewCount, model.SettingCategoryStatistic, blogID).Find(setting).Error; nil != err { return err } @@ -119,7 +119,7 @@ func (srv *statisticService) IncArticleCountWithoutTx(tx *gorm.DB, blogID uint64 defer srv.mutex.Unlock() setting := &model.Setting{} - if err := tx.Where(" name = ? AND category = ? AND blog_id = ?", model.SettingNameStatisticArticleCount, model.SettingCategoryStatistic, blogID).Find(setting).Error; nil != err { + if err := tx.Where("name = ? AND category = ? AND blog_id = ?", model.SettingNameStatisticArticleCount, model.SettingCategoryStatistic, blogID).Find(setting).Error; nil != err { return err } @@ -153,7 +153,7 @@ func (srv *statisticService) DecArticleCountWithoutTx(tx *gorm.DB, blogID uint64 defer srv.mutex.Unlock() setting := &model.Setting{} - if err := tx.Where(" name = ? AND category = ? AND blog_id = ?", model.SettingNameStatisticArticleCount, model.SettingCategoryStatistic, blogID).Find(setting).Error; nil != err { + if err := tx.Where("name = ? AND category = ? AND blog_id = ?", model.SettingNameStatisticArticleCount, model.SettingCategoryStatistic, blogID).Find(setting).Error; nil != err { return err } @@ -187,7 +187,7 @@ func (srv *statisticService) IncCommentCountWithoutTx(tx *gorm.DB, blogID uint64 defer srv.mutex.Unlock() setting := &model.Setting{} - if err := tx.Where(" name = ? AND category = ? AND blog_id = ?", model.SettingNameStatisticCommentCount, model.SettingCategoryStatistic, blogID).Find(setting).Error; nil != err { + if err := tx.Where("name = ? AND category = ? AND blog_id = ?", model.SettingNameStatisticCommentCount, model.SettingCategoryStatistic, blogID).Find(setting).Error; nil != err { return err } @@ -221,7 +221,7 @@ func (srv *statisticService) DecCommentCountWithoutTx(tx *gorm.DB, blogID uint64 defer srv.mutex.Unlock() setting := &model.Setting{} - if err := tx.Where(" name = ? AND category = ? AND blog_id = ?", model.SettingNameStatisticCommentCount, model.SettingCategoryStatistic, blogID).Find(setting).Error; nil != err { + if err := tx.Where("name = ? AND category = ? AND blog_id = ?", model.SettingNameStatisticCommentCount, model.SettingCategoryStatistic, blogID).Find(setting).Error; nil != err { return err } diff --git a/service/tagsrv.go b/service/tagsrv.go index 22ec631b..f2b0d4c6 100644 --- a/service/tagsrv.go +++ b/service/tagsrv.go @@ -55,7 +55,7 @@ func (srv *tagService) ConsoleGetTags(keyword string, page int, blogID uint64) ( } func (srv *tagService) GetTags(size int, blogID uint64) (ret []*model.Tag) { - if err := db.Where(" blog_id = ?", blogID).Order("article_count DESC, id DESC").Limit(size).Find(&ret).Error; nil != err { + if err := db.Where("blog_id = ?", blogID).Order("article_count DESC, id DESC").Limit(size).Find(&ret).Error; nil != err { logger.Errorf("get tags failed: " + err.Error()) } @@ -64,7 +64,7 @@ func (srv *tagService) GetTags(size int, blogID uint64) (ret []*model.Tag) { func (srv *tagService) GetTagByTitle(title string, blogID uint64) *model.Tag { ret := &model.Tag{} - if err := db.Where(" title = ? AND blog_id = ?", title, blogID).First(ret).Error; nil != err { + if err := db.Where("title = ? AND blog_id = ?", title, blogID).First(ret).Error; nil != err { return nil } @@ -73,7 +73,7 @@ func (srv *tagService) GetTagByTitle(title string, blogID uint64) *model.Tag { func (srv *tagService) RemoveTag(id, blogID uint64) (err error) { tag := &model.Tag{} - if err := db.Where(" id = ? AND blog_id = ?", id, blogID).Find(tag).Error; nil != err { + if err := db.Where("id = ? AND blog_id = ?", id, blogID).Find(tag).Error; nil != err { return err } diff --git a/service/upgradesrv.go b/service/upgradesrv.go index 711729f2..d8e693bf 100644 --- a/service/upgradesrv.go +++ b/service/upgradesrv.go @@ -68,7 +68,7 @@ func perform191_200() { logger.Infof("upgrading from version [" + fromVer + "] to version [" + model.Version + "]....") var verSettings []model.Setting - if err := db.Model(&model.Setting{}).Where(" name = ?", model.SettingNameSystemVer).Find(&verSettings).Error; nil != err { + if err := db.Model(&model.Setting{}).Where("name = ?", model.SettingNameSystemVer).Find(&verSettings).Error; nil != err { logger.Fatalf("load settings failed: %s", err) } @@ -91,7 +91,7 @@ func perform190_191() { logger.Infof("upgrading from version [" + fromVer + "] to version [" + model.Version + "]....") var verSettings []model.Setting - if err := db.Model(&model.Setting{}).Where(" name = ?", model.SettingNameSystemVer).Find(&verSettings).Error; nil != err { + if err := db.Model(&model.Setting{}).Where("name = ?", model.SettingNameSystemVer).Find(&verSettings).Error; nil != err { logger.Fatalf("load settings failed: %s", err) } @@ -114,7 +114,7 @@ func perform189_190() { logger.Infof("upgrading from version [" + fromVer + "] to version [" + model.Version + "]....") var verSettings []model.Setting - if err := db.Model(&model.Setting{}).Where(" name = ?", model.SettingNameSystemVer).Find(&verSettings).Error; nil != err { + if err := db.Model(&model.Setting{}).Where("name = ?", model.SettingNameSystemVer).Find(&verSettings).Error; nil != err { logger.Fatalf("load settings failed: %s", err) } @@ -136,7 +136,7 @@ func perform188_189() { logger.Infof("upgrading from version [1.8.8] to version [1.8.9]....") var verSettings []model.Setting - if err := db.Model(&model.Setting{}).Where(" name = ?", model.SettingNameSystemVer).Find(&verSettings).Error; nil != err { + if err := db.Model(&model.Setting{}).Where("name = ?", model.SettingNameSystemVer).Find(&verSettings).Error; nil != err { logger.Fatalf("load settings failed: %s", err) } @@ -158,7 +158,7 @@ func perform187_188() { logger.Infof("upgrading from version [1.8.7] to version [1.8.8]....") var verSettings []model.Setting - if err := db.Model(&model.Setting{}).Where(" name = ?", model.SettingNameSystemVer).Find(&verSettings).Error; nil != err { + if err := db.Model(&model.Setting{}).Where("name = ?", model.SettingNameSystemVer).Find(&verSettings).Error; nil != err { logger.Fatalf("load settings failed: %s", err) } @@ -212,7 +212,7 @@ func perform186_187() { logger.Infof("upgrading from version [1.8.6] to version [1.8.7]....") var verSettings []model.Setting - if err := db.Model(&model.Setting{}).Where(" name = ?", model.SettingNameSystemVer).Find(&verSettings).Error; nil != err { + if err := db.Model(&model.Setting{}).Where("name = ?", model.SettingNameSystemVer).Find(&verSettings).Error; nil != err { logger.Fatalf("load settings failed: %s", err) }