func (s *APIServer) updateFeed(c echo.Context) error { b := new(db.FeedInfo) err := jsonapi.UnmarshalPayload(c.Request().Body, b) if err != nil { return newError(c, "Bad Input", err) } err = s.DBH.SaveFeed(b) if err != nil { return newError(c, "Error saving Feed", err) } return c.NoContent(http.StatusNoContent) }
func (s *APIServer) addFeed(c echo.Context) error { dbuser := c.Get("dbuser").(*db.User) feed := new(db.FeedInfo) err := jsonapi.UnmarshalPayload(c.Request().Body, feed) if err != nil { return newError(c, "Bad Input", err) } //scrub //- id //- last pool //- last error feed.ID = 0 feed.LastPollTime = time.Time{} feed.LastErrorResponse = "" feed.LastPollError = "" // See if Feed already exists: // - subscribe // Else add & subscribe // TODO: normalize the url dbfeed, err := s.DBH.GetFeedByURL(feed.URL) if err == nil { err = s.DBH.AddFeedsToUser(dbuser, []*db.FeedInfo{dbfeed}) if err != nil { return newError(c, "Bad Input", err) } c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().WriteHeader(http.StatusOK) return jsonapi.MarshalOnePayload(c.Response(), dbfeed) } err = s.DBH.SaveFeed(feed) if err != nil { return newError(c, "Error saving Feed", err) } err = s.DBH.AddFeedsToUser(dbuser, []*db.FeedInfo{feed}) if err != nil { return newError(c, "Error subscribing to Feed", err) } c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().WriteHeader(http.StatusOK) return jsonapi.MarshalOnePayload(c.Response(), dbfeed) }
func exerciseHandler() { // list req, _ := http.NewRequest("GET", "/blogs", nil) req.Header.Set("Accept", "application/vnd.api+json") w := httptest.NewRecorder() fmt.Println("============ start list ===========\n") http.DefaultServeMux.ServeHTTP(w, req) fmt.Println("============ stop list ===========\n") jsonReply, _ := ioutil.ReadAll(w.Body) fmt.Println("============ jsonapi response from list ===========\n") fmt.Println(string(jsonReply)) fmt.Println("============== end raw jsonapi from list =============") // show req, _ = http.NewRequest("GET", "/blogs?id=1", nil) req.Header.Set("Accept", "application/vnd.api+json") w = httptest.NewRecorder() fmt.Println("============ start show ===========\n") http.DefaultServeMux.ServeHTTP(w, req) fmt.Println("============ stop show ===========\n") jsonReply, _ = ioutil.ReadAll(w.Body) fmt.Println("\n============ jsonapi response from show ===========\n") fmt.Println(string(jsonReply)) fmt.Println("============== end raw jsonapi from show =============") // create blog := testBlogForCreate(1) in := bytes.NewBuffer(nil) jsonapi.MarshalOnePayloadEmbedded(in, blog) req, _ = http.NewRequest("POST", "/blogs", in) req.Header.Set("Accept", "application/vnd.api+json") w = httptest.NewRecorder() fmt.Println("============ start create ===========\n") http.DefaultServeMux.ServeHTTP(w, req) fmt.Println("============ stop create ===========\n") buf := bytes.NewBuffer(nil) io.Copy(buf, w.Body) fmt.Println("\n============ jsonapi response from create ===========\n") fmt.Println(buf.String()) fmt.Println("============== end raw jsonapi response =============") responseBlog := new(Blog) jsonapi.UnmarshalPayload(buf, responseBlog) out := bytes.NewBuffer(nil) json.NewEncoder(out).Encode(responseBlog) fmt.Println("\n================ Viola! Converted back our Blog struct =================\n") fmt.Printf("%s\n", out.Bytes()) fmt.Println("================ end marshal materialized Blog struct =================") }