Пример #1
0
func SearchArticle(w http.ResponseWriter, r *http.Request) {
	query := r.FormValue("q")
	if len(query) == 0 {
		middleware.BadRequest(w)
		return
	}
	articles, _ := article.Search(query)
	middleware.RenderJSON(w, articles)
}
Пример #2
0
func GetArticle(c web.C, w http.ResponseWriter, r *http.Request) {
	id := c.URLParams["id"]
	article, err := article.Get(id)
	if err != nil {
		if err == model.ErrNotFound {
			middleware.NotFound(w)
		} else {
			middleware.Fatal(w)
		}
		return
	}
	middleware.RenderJSON(w, article)
}
Пример #3
0
func ReadSection(c web.C, w http.ResponseWriter, r *http.Request) {
	err := r.ParseMultipartForm(1024 * 1024)
	if err != nil {
		middleware.Fatal(w)
		return
	}
	articleId := r.FormValue("article_id")
	sectionId := r.FormValue("section_id")
	article, err := article.Get(articleId)
	if err != nil {
		if err == model.ErrNotFound {
			middleware.NotFound(w)
		} else {
			middleware.Fatal(w)
		}
		return
	}
	uploadFile, _, err := r.FormFile("audio")
	if err != nil {
		middleware.BadRequest(w)
		return
	}
	err = article.AddAudio(sectionId, uploadFile)
	if err != nil {
		if err == model.ErrNotFound {
			middleware.NotFound(w)
		} else {
			fmt.Println(err)
			middleware.Fatal(w)
		}
		return
	}
	middleware.RenderJSON(w, &struct {
		Code int `json:"code"`
	}{
		Code: 0,
	})
}
Пример #4
0
func ListArticle(w http.ResponseWriter, r *http.Request) {
	articles, _ := article.List()
	middleware.RenderJSON(w, articles)
}