Esempio n. 1
0
// get an article
//
//  in  : *ArticleRouteParam
//  out : {
//          "article":*Article
//        }
//
func (_ *ArticleApi) Get(v interface{}) *Res {
	param, ok := v.(*ArticleRouteParam)
	if !ok {
		return Fail(paramTypeError(new(ArticleRouteParam)))
	}
	var (
		article *model.Article
		err     error
	)
	if param.Id > 0 {
		article, err = model.GetArticleBy("id", param.Id)
		if err != nil {
			return Fail(err)
		}
	}
	if param.Link != "" {
		article, err = model.GetArticleBy("link", param.Link)
		if err != nil {
			return Fail(err)
		}
	}

	// check value
	if param.Id > 0 && param.Id != article.Id {
		return Fail(ERR_ARTICLE_MISSING)
	}
	if param.Link != "" && param.Link != article.Link {
		return Fail(ERR_ARTICLE_MISSING)
	}

	return Success(map[string]interface{}{
		"article": article,
	})
}
Esempio n. 2
0
// write an article,
// if input id, update article by id,
// or save new article
//
//  in  : *ArticleForm
//  out : {
//          "article":*Article
//        }
//
func (_ *ArticleApi) Write(v interface{}) *Res {
	form, ok := v.(*ArticleForm)
	if !ok {
		return Fail(paramTypeError(new(ArticleForm)))
	}
	// check link
	a, err := model.GetArticleBy("link", form.Link)
	if err != nil {
		return Fail(err)
	}
	if a != nil {
		if form.Id == 0 || form.Id != a.Id {
			return Fail(ERR_ARTICLE_LINK)
		}
	}

	// create article object
	article := &model.Article{
		Id:            form.Id,
		AuthorId:      form.AuthorId,
		Title:         form.Title,
		Link:          form.Link,
		Body:          form.Body,
		TagString:     form.Tag,
		Hits:          1,
		Comments:      0,
		Status:        model.ARTICLE_STATUS_PUBLISH,
		CommentStatus: model.ARTICLE_COMMENT_OPEN,
	}
	if form.Draft != "" {
		article.Status = model.ARTICLE_STATUS_DRAFT
	}
	if strings.Contains(article.Body, ARTICLE_MORE_LINK) {
		article.Preview = strings.Split(article.Body, ARTICLE_MORE_LINK)[0]
	}

	// save article
	article, err = model.SaveArticle(article)
	if err != nil {
		return Fail(err)
	}

	return Success(map[string]interface{}{
		"article": article,
	})
}
Esempio n. 3
0
func (w *Write) Get() {
	if w.Form("type") == "page" {
		w.getPage()
		return
	}
	w.Title("Write")
	id := w.FormInt64("id")
	if id > 0 {
		article, err := model.GetArticleBy("id", id)
		if err != nil {
			w.RenderError(500, err)
			return
		}
		w.Title("Write - " + article.Title)
		w.Assign("Article", article)
	}
	w.Render("write.tmpl")
}
Esempio n. 4
0
func (d *Delete) Get() {
	id := d.FormInt64("id")
	// go back if no id
	if id == 0 {
		d.Redirect(d.Req().Referer())
		return
	}
	// get article
	article, err := model.GetArticleBy("id", id)
	if err != nil {
		panic(err)
	}
	if article.Id != id {
		d.Redirect(d.Req().Referer())
		return
	}
	d.Title("Delete - " + article.Title)
	d.Assign("Article", article)
	d.Render("delete.tmpl")
}