Пример #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)
	}
}
Пример #2
0
/*
Mark article read or unread.
method:     PUT
path:       /api/article/read/{id}
            /api/article/unread/{id}
*/
func MarkReadStatus(markread bool) 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
		}

		var t struct {
			Id     int64  `json:"id"`
			Status string `json:"status"`
		}
		t.Id = id
		if markread {
			t.Status = "read"
		} else {
			t.Status = "unread"
		}

		ok, err := model.MarkArticleRead(id, markread)
		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}
		if !ok {
			result.Error = ErrNoDataChanged
			result.IntError = fmt.Errorf("No data changed when set article:%d as %s", id, t.Status)
			result.Response(w)
			return
		}

		result.Success = true
		result.Result = t
		result.Response(w)
	}
}
Пример #3
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)
	}
}