Skip to content

Commit

Permalink
🏗️ 支持 PostgreSQL 数据库 #31
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Apr 22, 2020
1 parent 9d2fa79 commit 8973468
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion service/archivesrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type archiveService struct {

func (srv *archiveService) GetArchives(blogID uint64) []*model.Archive {
var ret []*model.Archive
if err := db.Where(" blog_id = ? AND article_count > 0", blogID).Order(" year DESC, month DESC").Find(&ret).Error; nil != err {
if err := db.Where(" blog_id = ? AND article_count > 0", blogID).Order("year DESC, month DESC").Find(&ret).Error; nil != err {
logger.Error("get archives failed: " + err.Error())
}

Expand Down
20 changes: 10 additions & 10 deletions service/articlesrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ 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).
Order(" view_count DESC, created_at DESC").Limit(size).Find(&ret).Error; nil != err {
Order("view_count DESC, created_at DESC").Limit(size).Find(&ret).Error; nil != err {
logger.Errorf("get platform most view articles failed: " + err.Error())
}

Expand Down Expand Up @@ -79,7 +79,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).
Order(" topped DESC, created_at DESC").Count(&count).
Order("topped DESC, created_at DESC").Count(&count).
Offset(offset).Limit(pageSize).
Find(&ret).Error; nil != err {
logger.Errorf("get archive articles failed: " + err.Error())
Expand All @@ -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
}

Expand Down Expand Up @@ -191,7 +191,7 @@ func (srv *articleService) ConsoleGetArticles(keyword string, page int, blogID u

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).
Order("topped DESC, created_at DESC").Count(&count).
Offset(offset).Limit(adminConsoleArticleListPageSize).Find(&ret).Error; nil != err {
logger.Errorf("get articles failed: " + err.Error())
}
Expand All @@ -215,7 +215,7 @@ func (srv *articleService) GetArticles(keyword string, page int, blogID uint64)

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).
Order("topped DESC, created_at DESC").Count(&count).
Offset(offset).Limit(pageSize).
Find(&ret).Error; nil != err {
logger.Errorf("get articles failed: " + err.Error())
Expand Down Expand Up @@ -244,7 +244,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).
Order(" id DESC").Count(&count).Offset(offset).Limit(pageSize).
Order("id DESC").Count(&count).Offset(offset).Limit(pageSize).
Find(&rels).Error; nil != err {
return
}
Expand Down Expand Up @@ -281,7 +281,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).
Order(" topped DESC, created_at DESC").Count(&count).Offset(offset).Limit(pageSize).
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())
}
Expand All @@ -298,7 +298,7 @@ func (srv *articleService) GetAuthorArticles(authorID uint64, page int, blogID u

if err := db.Model(&model.Article{}).
Where(" author_id = ? AND status = ? AND blog_id = ?", authorID, model.ArticleStatusOK, blogID).
Order(" topped DESC, created_at DESC").Count(&count).
Order("topped DESC, created_at DESC").Count(&count).
Offset(offset).Limit(pageSize).
Find(&ret).Error; nil != err {
logger.Errorf("get author articles failed: " + err.Error())
Expand All @@ -312,7 +312,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).
Order(" view_count DESC, created_at DESC").Limit(size).Find(&ret).Error; nil != err {
Order("view_count DESC, created_at DESC").Limit(size).Find(&ret).Error; nil != err {
logger.Errorf("get most view articles failed: " + err.Error())
}

Expand All @@ -322,7 +322,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).
Order(" comment_count DESC, id DESC").Limit(size).Find(&ret).Error; nil != err {
Order("comment_count DESC, id DESC").Limit(size).Find(&ret).Error; nil != err {
logger.Errorf("get most comment articles failed: " + err.Error())
}

Expand Down
4 changes: 2 additions & 2 deletions service/categorysrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (srv *categoryService) GetCategoriesByTag(tagTitle string, blogID uint64) (
func (srv *categoryService) ConsoleGetCategories(page int, blogID uint64) (ret []*model.Category, pagination *util.Pagination) {
offset := (page - 1) * adminConsoleCategoryListPageSize
count := 0
if err := db.Model(&model.Category{}).Order(" number ASC, id DESC").
if err := db.Model(&model.Category{}).Order("number ASC, id DESC").
Where(" blog_id = ?", blogID).
Count(&count).Offset(offset).Limit(adminConsoleCategoryListPageSize).Find(&ret).Error; nil != err {
logger.Errorf("get categories failed: " + err.Error())
Expand All @@ -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())
}

Expand Down
6 changes: 3 additions & 3 deletions service/commentsrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (srv *commentService) ConsoleGetComments(keyword string, page int, blogID u
}

if err := db.Model(&model.Comment{}).
Where(where, whereArgs...).Order(" created_at DESC").
Where(where, whereArgs...).Order("created_at DESC").
Count(&count).Offset(offset).Limit(adminConsoleCommentListPageSize).Find(&ret).Error; nil != err {
logger.Errorf("get comments failed: " + err.Error())
}
Expand All @@ -119,7 +119,7 @@ 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).
Order(" created_at DESC, id DESC").Limit(size).Find(&ret).Error; nil != err {
Order("created_at DESC, id DESC").Limit(size).Find(&ret).Error; nil != err {
logger.Errorf("get recent comments failed: " + err.Error())
}

Expand All @@ -129,7 +129,7 @@ func (srv *commentService) GetRecentComments(size int, blogID uint64) (ret []*mo
func (srv *commentService) GetArticleComments(articleID uint64, page int, blogID uint64) (ret []*model.Comment, pagination *util.Pagination) {
offset := (page - 1) * themeCommentListPageSize
count := 0
if err := db.Model(&model.Comment{}).Order(" id ASC").
if err := db.Model(&model.Comment{}).Order("id ASC").
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())
Expand Down
8 changes: 4 additions & 4 deletions service/navigationsrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func (srv *navigationService) UpdateNavigation(navigation *model.Navigation) err
func (srv *navigationService) ConsoleGetNavigations(page int, blogID uint64) (ret []*model.Navigation, pagination *util.Pagination) {
offset := (page - 1) * adminConsoleNavigationListPageSize
count := 0
if err := db.Model(&model.Navigation{}).Order(" number ASC, id DESC").
Where(" blog_id = ?", blogID).
if err := db.Model(&model.Navigation{}).Order("number ASC, id DESC").
Where("blog_id = ?", blogID).
Count(&count).Offset(offset).Limit(adminConsoleNavigationListPageSize).Find(&ret).Error; nil != err {
logger.Errorf("get navigations failed: " + err.Error())
}
Expand All @@ -110,8 +110,8 @@ 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 {
if err := db.Model(&model.Navigation{}).Order("number ASC, id DESC").
Where("blog_id = ?", blogID).Find(&ret).Error; nil != err {
logger.Errorf("get navigations failed: " + err.Error())
}

Expand Down
4 changes: 2 additions & 2 deletions service/tagsrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (srv *tagService) ConsoleGetTags(keyword string, page int, blogID uint64) (
whereArgs = append(whereArgs, "%"+keyword+"%")
}

if err := db.Model(&model.Tag{}).Order(" id DESC").
if err := db.Model(&model.Tag{}).Order("id DESC").
Where(where, whereArgs...).
Count(&count).Offset(offset).Limit(adminConsoleTagListPageSize).Find(&ret).Error; nil != err {
logger.Errorf("get tags failed: " + err.Error())
Expand All @@ -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())
}

Expand Down
4 changes: 2 additions & 2 deletions service/usersrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (srv *userService) GetBlogAdmin(blogID uint64) *model.User {

func (srv *userService) GetPlatformAdmin() *model.User {
rel := &model.Correlation{ID1: 1}
if err := db.Where(rel).Order("id2 asc").First(rel).Error; nil != err {
if err := db.Where(rel).Order("id2 ASC").First(rel).Error; nil != err {
logger.Errorf("can't get platform admin: " + err.Error())

return nil
Expand Down Expand Up @@ -258,7 +258,7 @@ func (srv *userService) AddUserToBlog(userID, blogID uint64) error {

func (srv *userService) GetTopBlogs(size int) (ret []*UserBlog) {
var users []*model.User
if err := db.Model(&model.User{}).Order(" total_article_count DESC, id DESC").Limit(size).
if err := db.Model(&model.User{}).Order("total_article_count DESC, id DESC").Limit(size).
Find(&users).Error; nil != err {
return
}
Expand Down

0 comments on commit 8973468

Please sign in to comment.