Example #1
0
// MarshalManyPayload marshal the struct and return as jsonaapi
func (c *Context) MarshalManyPayload(status int, models []interface{}) {
	c.Writer.WriteHeader(status)
	c.Writer.Header().Set("Content-Type", "application/vnd.api+json")
	if err := jsonapi.MarshalManyPayload(c.Writer, models); err != nil {
		http.Error(c.Writer, err.Error(), 500)
	}
}
Example #2
0
func (s *APIServer) getFeeds(c echo.Context) error {
	dbuser := c.Get("dbuser").(*db.User)
	b := new(feedsReqBinder)

	if err := c.Bind(b); err != nil {
		return newError(c, "Unable to parse request", err)
	}

	var feeds []*db.FeedInfo
	var err error
	if b.Name != "" {
		feeds, err = s.DBH.GetUsersFeedsByName(dbuser, b.Name)
	} else {
		feeds, err = s.DBH.GetUsersFeeds(dbuser)
	}
	if err != nil {
		return newError(c, "Unable to get feeds", err)
	}

	feedInterface := make([]interface{}, len(feeds))

	for i, f := range feeds {
		feedInterface[i] = f
	}

	c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
	c.Response().WriteHeader(http.StatusOK)
	return jsonapi.MarshalManyPayload(c.Response(), feedInterface)
}