本文整理匯總了Golang中github.com/microcosm-cc/microcosm/models.Context.RespondWithData方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.RespondWithData方法的具體用法?Golang Context.RespondWithData怎麽用?Golang Context.RespondWithData使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/microcosm-cc/microcosm/models.Context
的用法示例。
在下文中一共展示了Context.RespondWithData方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Read
// Read handles GET
func (ctl *AttributeController) Read(c *models.Context) {
_, itemTypeID, itemID, status, err := c.GetItemTypeAndItemID()
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
perms := models.GetPermission(models.MakeAuthorisationContext(c, 0, itemTypeID, itemID))
if !perms.CanRead {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
attributeID, status, err := models.GetAttributeID(itemTypeID, itemID, c.RouteVars["key"])
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
m, status, err := models.GetAttribute(attributeID)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
c.RespondWithData(m)
}
示例2: Read
// Read handles GET
func (ctl *UserController) Read(c *models.Context) {
_, itemTypeID, itemID, status, err := c.GetItemTypeAndItemID()
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
// Start Authorisation
perms := models.GetPermission(
models.MakeAuthorisationContext(
c, 0, itemTypeID, itemID),
)
if !perms.CanRead {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
// End Authorisation
m, status, err := models.GetUser(itemID)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
if !models.UserIsOnSite(m.ID, c.Site.ID) {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
c.RespondWithData(m)
}
示例3: Read
// Read handles GET either by /site or /sites/{site_id}
func (ctl *SiteController) Read(c *models.Context) {
// Check whether this site is being accessed by ID
siteQuery, exists := c.RouteVars["site_id"]
if exists {
siteID, err := strconv.ParseInt(siteQuery, 10, 64)
if err != nil {
c.RespondWithErrorMessage(
fmt.Sprintf("The supplied site ID ('%s') is not a number.", siteQuery),
http.StatusBadRequest,
)
return
}
site, status, err := models.GetSite(siteID)
if err != nil {
c.RespondWithErrorMessage(
fmt.Sprintf("No site with ID %d found", siteID),
status,
)
return
}
c.RespondWithData(site)
return
}
// Site already populated in context, so only fetch permissions
c.Site.Meta.Permissions = models.GetPermission(
models.MakeAuthorisationContext(
c, 0, h.ItemTypes[h.ItemTypeSite], c.Site.ID),
)
c.RespondWithData(c.Site)
return
}
示例4: ReadMany
// ReadMany handles GET for the collection
func (ctl *TrendingController) ReadMany(c *models.Context) {
limit, offset, status, err := h.GetLimitAndOffset(c.Request.URL.Query())
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
trending, total, pages, status, err := models.GetTrending(c.Site.ID, c.Auth.ProfileID, limit, offset)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
response := models.TrendingItems{}
response.Items = h.ConstructArray(
trending,
"items",
total,
limit,
offset,
pages,
c.Request.URL,
)
thisLink := h.GetLinkToThisPage(*c.Request.URL, offset, limit, total)
response.Meta.Links =
[]h.LinkType{
h.LinkType{Rel: "self", Href: thisLink.String()},
}
c.RespondWithData(response)
}
示例5: Read
// Read handles GET
// Returns information on a profile assigned to this role
func (ctl *RoleProfileController) Read(c *models.Context) {
// Validate inputs
var microcosmID int64
if sid, exists := c.RouteVars["microcosm_id"]; exists {
id, err := strconv.ParseInt(sid, 10, 64)
if err != nil {
c.RespondWithErrorMessage("microcosm_id in URL is not a number", http.StatusBadRequest)
return
}
microcosmID = id
}
roleID, err := strconv.ParseInt(c.RouteVars["role_id"], 10, 64)
if err != nil {
c.RespondWithErrorMessage("role_id in URL is not a number", http.StatusBadRequest)
return
}
_, status, err := models.GetRole(c.Site.ID, microcosmID, roleID, c.Auth.ProfileID)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
profileID, err := strconv.ParseInt(c.RouteVars["profile_id"], 10, 64)
if err != nil {
c.RespondWithErrorMessage("profile_id in URL is not a number", http.StatusBadRequest)
return
}
// Authorisation
perms := models.GetPermission(
models.MakeAuthorisationContext(c, microcosmID, h.ItemTypes[h.ItemTypeMicrocosm], microcosmID),
)
if microcosmID > 0 {
// Related to a Microcosm
if !perms.IsModerator && !c.Auth.IsSiteOwner {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
} else {
// Default role for the site
if !c.Auth.IsSiteOwner {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
}
m, status, err := models.GetRoleProfile(c.Site.ID, roleID, profileID)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
c.RespondWithData(m)
}
示例6: ReadMany
// ReadMany handles GET
// If microcosm_id is provided in request args then these are the roles for this
// microcosm, otherwise this is a list of the default roles on this site
func (ctl *RolesController) ReadMany(c *models.Context) {
var microcosmID int64
if sid, exists := c.RouteVars["microcosm_id"]; exists {
id, err := strconv.ParseInt(sid, 10, 64)
if err != nil {
c.RespondWithErrorMessage("microcosm_id in URL is not a number", http.StatusBadRequest)
return
}
microcosmID = id
}
perms := models.GetPermission(
models.MakeAuthorisationContext(c, 0, h.ItemTypes[h.ItemTypeMicrocosm], microcosmID),
)
if microcosmID > 0 {
// Related to a Microcosm
if !perms.CanRead {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
} else {
// Default role for the site
if !c.Auth.IsSiteOwner {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
}
// Fetch query string args if any exist
limit, offset, status, err := h.GetLimitAndOffset(c.Request.URL.Query())
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
ems, total, pages, status, err := models.GetRoles(c.Site.ID, microcosmID, c.Auth.ProfileID, limit, offset)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
// Construct the response
m := models.RolesType{}
m.Roles = h.ConstructArray(
ems,
h.APITypeRole,
total,
limit,
offset,
pages,
c.Request.URL,
)
c.RespondWithData(m)
}
示例7: Read
// Read handles GET
func (ctl *PermissionController) Read(c *models.Context) {
ac, status, err := GetAuthContext(c)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
m := models.GetPermission(ac)
c.RespondWithData(m)
}
示例8: Read
// Read handles GET
func (ctl *MenuController) Read(c *models.Context, siteID int64) {
ems, status, err := models.GetMenu(siteID)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
c.RespondWithData(ems)
}
示例9: ReadMany
// ReadMany handles GET
func (ctl *EventsController) ReadMany(c *models.Context) {
// Start Authorisation
perms := models.GetPermission(
models.MakeAuthorisationContext(
c, 0, h.ItemTypes[h.ItemTypeEvent], 0),
)
if !perms.CanRead {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
// End Authorisation
// Fetch query string args if any exist
query := c.Request.URL.Query()
limit, offset, status, err := h.GetLimitAndOffset(query)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
attending, status, err := h.GetAttending(query)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
ems, total, pages, status, err := models.GetEvents(c.Site.ID, c.Auth.ProfileID, attending, limit, offset)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
// Construct the response
thisLink := h.GetLinkToThisPage(*c.Request.URL, offset, limit, total)
m := models.EventsType{}
m.Events = h.ConstructArray(
ems,
h.APITypeEvent,
total,
limit,
offset,
pages,
c.Request.URL,
)
m.Meta.Links =
[]h.LinkType{
h.LinkType{Rel: "self", Href: thisLink.String()},
}
m.Meta.Permissions = perms
c.RespondWithData(m)
}
示例10: Read
// Read handles GET
func (ctl *AuthController) Read(c *models.Context) {
// Extract access token from request and retrieve its metadata
m, status, err := models.GetAccessToken(c.RouteVars["id"])
if err != nil {
c.RespondWithErrorMessage(
fmt.Sprintf("Error retrieving access token: %v", err.Error()),
status,
)
return
}
c.RespondWithData(m)
}
示例11: Read
// Read handles GET
func (ctl *SearchController) Read(c *models.Context) {
results, status, err := models.Search(
c.Site.ID,
*c.Request.URL,
c.Auth.ProfileID,
)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
c.RespondWithData(results)
}
示例12: ReadMany
// ReadMany handles GET for the collection
func (ctl *HuddleParticipantsController) ReadMany(c *models.Context) {
// Validate inputs
huddleID, err := strconv.ParseInt(c.RouteVars["huddle_id"], 10, 64)
if err != nil {
c.RespondWithErrorMessage("huddle_id in URL is not a number", http.StatusBadRequest)
return
}
r, status, err := models.GetHuddle(c.Site.ID, c.Auth.ProfileID, huddleID)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
// Start Authorisation
perms := models.GetPermission(
models.MakeAuthorisationContext(
c, 0, h.ItemTypes[h.ItemTypeHuddle], huddleID),
)
if !perms.CanRead {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
// End Authorisation
limit, offset, status, err := h.GetLimitAndOffset(c.Request.URL.Query())
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
ems, total, pages, status, err := models.GetHuddleParticipants(c.Site.ID, huddleID, limit, offset)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
// Construct the response
m := models.HuddleParticipantsType{}
m.HuddleParticipants = h.ConstructArray(
ems,
fmt.Sprintf("%s/participants", r.GetLink()),
total,
limit,
offset,
pages,
c.Request.URL,
)
c.RespondWithData(m)
}
示例13: Read
// Read handles GET
func (ctl *ProfileOptionsController) Read(c *models.Context) {
if c.Auth.ProfileID < 1 {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
m, status, err := models.GetProfileOptions(c.Auth.ProfileID)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
c.RespondWithData(m)
}
示例14: ReadMany
// ReadMany handles GET for a collection
func (ctl *UpdateOptionsController) ReadMany(c *models.Context) {
if c.Auth.ProfileID < 1 {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
prefs, status, err := models.GetUpdateOptions(c.Site.ID, c.Auth.ProfileID)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
c.RespondWithData(prefs)
}
示例15: ReadMany
// ReadMany handles GET for the collection
func (ctl *AttachmentsController) ReadMany(c *models.Context) {
itemTypeID, itemID, perms, status, err := ParseItemInfo(c)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
if !perms.CanRead {
c.RespondWithErrorMessage(
h.NoAuthMessage,
http.StatusForbidden,
)
return
}
query := c.Request.URL.Query()
limit, offset, status, err := h.GetLimitAndOffset(query)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
attachments, total, pages, status, err := models.GetAttachments(itemTypeID, itemID, limit, offset)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
thisLink := h.GetLinkToThisPage(*c.Request.URL, offset, limit, total)
m := models.AttachmentsType{}
m.Attachments = h.ConstructArray(
attachments,
h.APITypeAttachment,
total,
limit,
offset,
pages,
c.Request.URL,
)
m.Meta.Links =
[]h.LinkType{
h.LinkType{Rel: "self", Href: thisLink.String()},
}
m.Meta.Permissions = perms
c.RespondWithData(m)
}