Esempio n. 1
0
File: init.go Progetto: Ganben/blog
func InitDbDefault(_ interface{}) *action.Result {
	// init administrator user
	user := model.NewDefaultUser()
	if err := model.SaveUser(user); err != nil {
		return action.ErrorResult(err)
	}

	// init welcome article
	article := model.NewDefaultArticle(user.Id)
	if err := model.SaveArticle(article); err != nil {
		return action.ErrorResult(err)
	}

	// init first comment
	comment := model.NewDefaultComment(article.Id)
	if err := model.SaveComment(comment); err != nil {
		return action.ErrorResult(err)
	}

	// init default settings
	settings := model.NewDefaultSetting(user.Id)
	if err := model.SaveSettings(settings); err != nil {
		return action.ErrorResult(err)
	}

	return action.OkResult(nil)
}
Esempio n. 2
0
func ArticleSave(v interface{}) *Result {
	form, ok := v.(*ArticleForm)
	if !ok {
		return ErrorResult(paramTypeError(new(ArticleForm)))
	}
	article := &model.Article{
		Title:         form.Title,
		Content:       form.Content,
		UserId:        form.UserId,
		ContentType:   model.ARTICLE_CNT_TYPE_MARKDOWN,
		CommentStatus: model.ARTICLE_CMT_STATUS_OPEN,
		HitCount:      1,
		CommentCount:  0,
		TagString:     strings.TrimSpace(strings.Replace(form.Tag, ",", ",", -1)),
	}
	if form.Status == "publish" {
		article.Status = model.ARTICLE_STATUS_PUBLISH
	} else {
		article.Status = model.ARTICLE_STATUS_DRAFT
	}

	// generate link
	article.Link = generateLink()

	// save article
	if err := model.SaveArticle(article); err != nil {
		return ErrorResult(err)
	}

	// means update, remove old tags
	if form.Id > 0 {
		if err := model.RemoveArticleTags(form.Id); err != nil {
			return ErrorResult(err)
		}
	}

	// save tags
	if err := model.SaveTags(article.Id, article.UserId, strings.Split(article.TagString, ",")); err != nil {
		return ErrorResult(err)
	}

	return OkResult(map[string]interface{}{
		"article": article,
	})
}