// MarshalOnePayload marshal the struct and return as jsonaapi func (c *Context) MarshalOnePayload(status int, model interface{}) { c.Writer.WriteHeader(status) c.Writer.Header().Set("Content-Type", "application/vnd.api+json") if err := jsonapi.MarshalOnePayload(c.Writer, model); err != nil { http.Error(c.Writer, err.Error(), 500) } }
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 (s *APIServer) getFeed(c echo.Context) error { feedID, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { return newError(c, "Unknown Feed ID", err) } feed, err := s.DBH.GetFeedByID(feedID) if err != nil { return newError(c, "Couldn't retrieve Feed information", err) } c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().WriteHeader(http.StatusOK) return jsonapi.MarshalOnePayload(c.Response(), feed) }
func (s *APIServer) subFeed(c echo.Context) error { feedID, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { return newError(c, "Unknown feed id", err) } feed, err := s.DBH.GetFeedByID(feedID) if err != nil { return newError(c, "Unable to retrieve feed", err) } dbuser := c.Get("dbuser").(*db.User) err = s.DBH.AddFeedsToUser(dbuser, []*db.FeedInfo{feed}) if err != nil { return newError(c, "Unable to subscribe user to feed", err) } c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().WriteHeader(http.StatusOK) return jsonapi.MarshalOnePayload(c.Response(), feed) }