Skip to content

Commit

Permalink
🔥 Remove article draft status
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Nov 5, 2017
1 parent dd1b4e2 commit ec16078
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 119 deletions.
13 changes: 6 additions & 7 deletions controller/console/userctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ func GetUsersAction(c *gin.Context) {
userModels := service.User.GetBlogUsers(session.BID)
for _, userModel := range userModels {
users = append(users, &ConsoleUser{
ID: userModel.ID,
Name: userModel.Name,
Nickname: userModel.Nickname,
Role: userModel.Role,
URL: blogURLSetting.Value + util.PathAuthors + "/" + userModel.Name,
AvatarURL: userModel.AvatarURL,
PublishedArticleCount: userModel.PublishedArticleCount,
ID: userModel.ID,
Name: userModel.Name,
Nickname: userModel.Nickname,
Role: userModel.Role,
URL: blogURLSetting.Value + util.PathAuthors + "/" + userModel.Name,
AvatarURL: userModel.AvatarURL,
})
}

Expand Down
1 change: 0 additions & 1 deletion model/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@ type Article struct {
// Article statuses.
const (
ArticleStatusPublished = iota
ArticleStatusDraft
)
7 changes: 3 additions & 4 deletions model/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ const (
const (
SettingCategoryStatistic = "statistic"

SettingNameStatisticArticleCount = "statisticArticleCount"
SettingNameStatisticPublishedArticleCount = "statisticPublishedArticleCount"
SettingNameStatisticCommentCount = "statisticCommentCount"
SettingNameStatisticViewCount = "statisticViewCount"
SettingNameStatisticArticleCount = "statisticArticleCount"
SettingNameStatisticCommentCount = "statisticCommentCount"
SettingNameStatisticViewCount = "statisticViewCount"
)

// Setting values of category "preference".
Expand Down
5 changes: 2 additions & 3 deletions model/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ package model
type Tag struct {
Model

Title string `gorm:"size:128" json:"title"`
ArticleCount int `json:"articleCount"` // including drafts and published articles
PublishedArticleCount int `json:"publishedArticleCount"` // just including published articles
Title string `gorm:"size:128" json:"title"`
ArticleCount int `json:"articleCount"`

BlogID uint `json:"blogID"`
}
15 changes: 7 additions & 8 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ package model
type User struct {
Model

Name string `gorm:"size:32" json:"name"`
Nickname string `gorm:"size:32" json:"nickname"`
Role int `json:"role"`
AvatarURL string `gorm:"size:255" json:"avatarURL"`
B3Key string `gorm:"size:32" json:"b3Key"`
Locale string `gorm:"size:32 json:"locale"`
ArticleCount int `json:"articleCount"` // including drafts and published articles
PublishedArticleCount int `json:"publishedArticleCount"` // just including published articles
Name string `gorm:"size:32" json:"name"`
Nickname string `gorm:"size:32" json:"nickname"`
Role int `json:"role"`
AvatarURL string `gorm:"size:255" json:"avatarURL"`
B3Key string `gorm:"size:32" json:"b3Key"`
Locale string `gorm:"size:32 json:"locale"`
ArticleCount int `json:"articleCount"`

BlogID uint `json:"blogID"`
}
Expand Down
14 changes: 0 additions & 14 deletions service/articlesrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ func (srv *articleService) AddArticle(article *model.Article) error {

return err
}
if err := Statistic.IncPublishedArticleCountWithoutTx(tx, article.BlogID); nil != err {
tx.Rollback()

return err
}
tx.Commit()

return nil
Expand Down Expand Up @@ -222,7 +217,6 @@ func (srv *articleService) RemoveArticle(id uint) error {
return err
}
author.ArticleCount = author.ArticleCount - 1
author.PublishedArticleCount = author.PublishedArticleCount - 1
if err := tx.Model(author).Updates(author).Error; nil != err {
tx.Rollback()

Expand All @@ -243,11 +237,6 @@ func (srv *articleService) RemoveArticle(id uint) error {

return err
}
if err := Statistic.DecPublishedArticleCountWithoutTx(tx, author.BlogID); nil != err {
tx.Rollback()

return err
}
comments := []*model.Comment{}
if err := tx.Model(&model.Comment{}).Where("article_id = ?", id).Find(&comments).Error; nil != err {
tx.Rollback()
Expand Down Expand Up @@ -364,7 +353,6 @@ func removeTagArticleRels(tx *gorm.DB, article *model.Article) error {
continue
}
tag.ArticleCount = tag.ArticleCount - 1
tag.PublishedArticleCount = tag.PublishedArticleCount - 1
if err := tx.Save(tag).Error; nil != err {
continue
}
Expand All @@ -385,14 +373,12 @@ func tagArticle(tx *gorm.DB, article *model.Article) error {
if "" == tag.Title {
tag.Title = tagTitle
tag.ArticleCount = 1
tag.PublishedArticleCount = 1
tag.BlogID = article.BlogID
if err := tx.Create(tag).Error; nil != err {
return err
}
} else {
tag.ArticleCount = tag.ArticleCount + 1
tag.PublishedArticleCount = tag.PublishedArticleCount + 1
if err := tx.Model(tag).Updates(tag).Error; nil != err {
return err
}
Expand Down
16 changes: 4 additions & 12 deletions service/initsrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (srv *initService) InitPlatform(platformAdmin *model.User) error {

func initPlatformAdmin(tx *gorm.DB, admin *model.User, blogID uint) error {
admin.Role = model.UserRolePlatformAdmin
admin.ArticleCount, admin.PublishedArticleCount = 1, 1 // article "Hello, World!"
admin.ArticleCount = 1 // article "Hello, World!"
admin.BlogID = blogID
admin.Locale = "zh_CN"

Expand Down Expand Up @@ -237,10 +237,9 @@ Pipe 博客系统是一个开源项目,如果你觉得它很赞,请到[项
}

tag := &model.Tag{
Title: "Pipe",
ArticleCount: 1,
PublishedArticleCount: 1,
BlogID: blogID,
Title: "Pipe",
ArticleCount: 1,
BlogID: blogID,
}
if err := tx.Create(tag).Error; nil != err {
return err
Expand Down Expand Up @@ -494,13 +493,6 @@ func initStatisticSettings(tx *gorm.DB, blogID uint) error {
BlogID: blogID}).Error; nil != err {
return err
}
if err := tx.Create(&model.Setting{
Category: model.SettingCategoryStatistic,
Name: model.SettingNameStatisticPublishedArticleCount,
Value: "1", // article "Hello, World!"
BlogID: blogID}).Error; nil != err {
return err
}
if err := tx.Create(&model.Setting{
Category: model.SettingCategoryStatistic,
Name: model.SettingNameStatisticCommentCount,
Expand Down
68 changes: 0 additions & 68 deletions service/statisticsrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,74 +140,6 @@ func (srv *statisticService) DecArticleCountWithoutTx(tx *gorm.DB, blogID uint)
return nil
}

func (srv *statisticService) IncPublishedArticleCount(blogID uint) error {
tx := db.Begin()
if err := srv.IncPublishedArticleCountWithoutTx(tx, blogID); nil != err {
tx.Rollback()

return err
}
tx.Commit()

return nil
}

func (srv *statisticService) IncPublishedArticleCountWithoutTx(tx *gorm.DB, blogID uint) error {
srv.mutex.Lock()
defer srv.mutex.Unlock()

setting := &model.Setting{}
if err := tx.Where("name = ? AND category = ? AND blog_id = ?", model.SettingNameStatisticPublishedArticleCount, model.SettingCategoryStatistic, blogID).Find(setting).Error; nil != err {
return err
}

count, err := strconv.Atoi(setting.Value)
if nil != err {
return err
}

setting.Value = strconv.Itoa(count + 1)
if err := tx.Model(setting).Updates(setting).Error; nil != err {
return err
}

return nil
}

func (srv *statisticService) DecPublishedArticleCount(blogID uint) error {
tx := db.Begin()
if err := srv.DecPublishedArticleCountWithoutTx(tx, blogID); nil != err {
tx.Rollback()

return err
}
tx.Commit()

return nil
}

func (srv *statisticService) DecPublishedArticleCountWithoutTx(tx *gorm.DB, blogID uint) error {
srv.mutex.Lock()
defer srv.mutex.Unlock()

setting := &model.Setting{}
if err := tx.Where("name = ? AND category = ? AND blog_id = ?", model.SettingNameStatisticPublishedArticleCount, model.SettingCategoryStatistic, blogID).Find(setting).Error; nil != err {
return err
}

count, err := strconv.Atoi(setting.Value)
if nil != err {
return err
}

setting.Value = strconv.Itoa(count - 1)
if err := tx.Model(setting).Updates(setting).Error; nil != err {
return err
}

return nil
}

func (srv *statisticService) IncCommentCount(blogID uint) error {
tx := db.Begin()
if err := srv.IncCommentCountWithoutTx(tx, blogID); nil != err {
Expand Down
4 changes: 2 additions & 2 deletions service/statisticsrv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (

func TestGetAllStatistics(t *testing.T) {
settings := Statistic.GetAllStatistics(1)
if 4 != len(settings) {
t.Errorf("expected is [%d], actual is [%d]", 4, len(settings))
if 3 != len(settings) {
t.Errorf("expected is [%d], actual is [%d]", 3, len(settings))
}
}

Expand Down

0 comments on commit ec16078

Please sign in to comment.