Ejemplo n.º 1
0
func (c *appContext) createarticleHandler(w http.ResponseWriter, r *http.Request) {
	body := context.Get(r, "body").(*articlesModel.ArticleResource)
	repo := articlesModel.ArticleRepo{c.db.C("articles")}
	doc, err := goquery.NewDocument(body.Data.URL)
	if err != nil {
		log.Fatal(err)
	}

	body.Data.Title = doc.Find("h1").Text()
	doc.Find("p").Each(func(i int, s *goquery.Selection) {
		html, err := s.Html()
		if err != nil {
			log.Fatal(err)
		}

		body.Data.Article += html
	})
	body.Data.Date = time.Now()

	err = repo.Create(&body.Data)
	if err != nil {
		log.Fatal(err)
	}

	w.Header().Set("Content-Type", "application/vnd.api+json")
	w.WriteHeader(201)
	json.NewEncoder(w).Encode(body)
}
Ejemplo n.º 2
0
func (c *appContext) articlesHandler(w http.ResponseWriter, r *http.Request) {
	repo := articlesModel.ArticleRepo{c.db.C("articles")}
	articles, err := repo.All()
	if err != nil {
		log.Fatal(err)
	}

	w.Header().Set("Content-Type", "application/vnd.api+json")
	json.NewEncoder(w).Encode(articles)
}
Ejemplo n.º 3
0
func (c *appContext) articleHandler(w http.ResponseWriter, r *http.Request) {
	params := context.Get(r, "params").(httprouter.Params)
	repo := articlesModel.ArticleRepo{c.db.C("articles")}
	article, err := repo.Find(params.ByName("id"))
	if err != nil {
		log.Fatal(err)
	}

	w.Header().Set("Content-Type", "application/vnd.api+json")
	json.NewEncoder(w).Encode(article)
}
Ejemplo n.º 4
0
func (c *appContext) deletearticleHandler(w http.ResponseWriter, r *http.Request) {
	params := context.Get(r, "params").(httprouter.Params)
	repo := articlesModel.ArticleRepo{c.db.C("articles")}
	err := repo.Delete(params.ByName("id"))
	if err != nil {
		log.Fatal(err)
	}

	w.WriteHeader(204)
	w.Write([]byte("\n"))
}
Ejemplo n.º 5
0
func (c *appContext) updatearticleHandler(w http.ResponseWriter, r *http.Request) {
	params := context.Get(r, "params").(httprouter.Params)
	body := context.Get(r, "body").(*articlesModel.ArticleResource)
	body.Data.Id = bson.ObjectIdHex(params.ByName("id"))
	repo := articlesModel.ArticleRepo{c.db.C("articles")}
	err := repo.Update(&body.Data)
	if err != nil {
		log.Fatal(err)
	}

	w.WriteHeader(204)
	w.Write([]byte("\n"))
}