Esempio n. 1
0
/*
Get one article.

method:     GET
path:       /api/article/{article id}
example:    /api/article/1
*/
func Article() martini.Handler {
	return func(w http.ResponseWriter, params martini.Params, rid httphelper.RequestId) {
		var result Result
		result.RequestId = rid

		id, err := strconv.ParseInt(params["id"], 10, 64)
		if err != nil || id <= 0 {
			result.Error = ErrBadRequest
			if err != nil {
				result.IntError = err
			} else {
				result.IntError = fmt.Errorf("Parameter 'id' is not correct.")
			}
			result.Response(w)
			return
		}

		article, ok, err := model.GetArticle(id)
		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}
		if !ok {
			result.Error = ErrNoResultsFound
			result.IntError = fmt.Errorf("Article of id:%d is not found.", id)
			result.Response(w)
			return
		}

		if article.Read == false {
			// read article read
			_, err = model.MarkArticleRead(id, true)
			if err != nil {
				result.Error = ErrQueryDB
				result.IntError = err
				result.Response(w)
				return
			}
		}
		article.Read = true

		utils.SanitizeSelf(&article.Name)
		utils.SanitizeSelf(&article.Author)
		utils.SanitizeSelf(&article.Title)
		utils.SanitizeSelf(&article.Content, true)

		result.Success = true
		result.Result = article
		result.Response(w)
	}
}
Esempio n. 2
0
/*
Get one article.

method:     GET
path:       /api/article/{article id}
example:    /api/article/1
*/
func Article() martini.Handler {
	return func(w http.ResponseWriter, params martini.Params, rid httphelper.RequestId) {
		var result Result
		result.RequestId = rid

		id, err := strconv.ParseInt(params["id"], 10, 64)
		if err != nil || id <= 0 {
			result.Error = ErrBadRequest
			if err != nil {
				result.IntError = err
			} else {
				result.IntError = fmt.Errorf("Parameter 'id' is not correct.")
			}
			result.Response(w)
			return
		}

		article, ok, err := model.GetArticle(id)
		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}
		if !ok {
			result.Error = ErrNoResultsFound
			result.IntError = fmt.Errorf("Article of id:%d is not found.", id)
			result.Response(w)
			return
		}

		if article.Read == false {
			// read article read
			_, err = model.MarkArticleRead(id, true)
			if err != nil {
				result.Error = ErrQueryDB
				result.IntError = err
				result.Response(w)
				return
			}
		}
		article.Read = true

		// Get five ralated articles
		related, err := model.GetRelatedArticles(id, 5)
		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}

		utils.SanitizeSelf(&article.Name)
		utils.SanitizeSelf(&article.Author)
		utils.SanitizeSelf(&article.Title)
		utils.SanitizeSelf(&article.Content, true)

		for i := range related {
			utils.SanitizeSelf(&related[i].Name)
			utils.SanitizeSelf(&related[i].Author)
			utils.SanitizeSelf(&related[i].Title)
		}

		var t struct {
			Article *model.Article   `json:"article"`
			Related []*model.Article `json:"related"`
		}
		t.Article = article
		t.Related = related

		result.Success = true
		result.Result = t
		result.Response(w)
	}
}