Golang github.com-ardanlabs-kit-web.Context类(方法)实例源码

下面列出了Golang github.com-ardanlabs-kit-web.Context 类(方法)源码代码实例,从而了解它的用法。

作者:coralprojec    项目:xeni   
// Search retrieves a set of FormSubmission's based on the search params
// provided in the query string.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (formSubmissionHandle) Search(c *web.Context) error {
	formID := c.Params["form_id"]

	limit, err := strconv.Atoi(c.Request.URL.Query().Get("limit"))
	if err != nil {
		limit = 0
	}

	skip, err := strconv.Atoi(c.Request.URL.Query().Get("skip"))
	if err != nil {
		skip = 0
	}

	opts := submission.SearchOpts{
		Query:    c.Request.URL.Query().Get("search"),
		FilterBy: c.Request.URL.Query().Get("filterby"),
	}

	if c.Request.URL.Query().Get("orderby") == "dsc" {
		opts.DscOrder = true
	}

	results, err := submission.Search(c.SessionID, c.Ctx["DB"].(*db.DB), formID, limit, skip, opts)
	if err != nil {
		return err
	}

	c.Respond(results, http.StatusOK)

	return nil
}

作者:coralprojec    项目:xeni   
// Retrieve returns the specified mask from the system.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (maskHandle) Retrieve(c *web.Context) error {
	collection := c.Params["collection"]
	field := c.Params["field"]

	if collection == "" {
		collection = "*"
	}

	if field == "" {
		masks, err := mask.GetByCollection(c.SessionID, c.Ctx["DB"].(*db.DB), collection)
		if err != nil {
			if err == mask.ErrNotFound {
				err = web.ErrNotFound
			}
			return err
		}

		c.Respond(masks, http.StatusOK)
		return nil
	}

	msk, err := mask.GetByName(c.SessionID, c.Ctx["DB"].(*db.DB), collection, field)
	if err != nil {
		if err == mask.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(msk, http.StatusOK)
	return nil
}

作者:coralprojec    项目:xeni   
// Aggregate performs all aggregations across a form's submissions.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (aggregationHandle) Aggregate(c *web.Context) error {
	id := c.Params["form_id"]

	// Aggregations can be based on Queries or Filters.
	opts := submission.SearchOpts{
		Query:    c.Request.URL.Query().Get("search"),
		FilterBy: c.Request.URL.Query().Get("filterby"),
	}

	aggregations, err := form.AggregateFormSubmissions(c.SessionID, c.Ctx["DB"].(*db.DB), id, opts)
	if err == mgo.ErrNotFound {
		c.Respond(nil, http.StatusBadRequest)
	}
	if err != nil {
		return err
	}

	ak := AggregationKeys{
		Aggregations: aggregations,
	}

	c.Respond(ak, http.StatusOK)

	return nil
}

作者:coralprojec    项目:xeni   
// Download retrieves a set of FormSubmission's based on the search params
// provided in the query string and generates a CSV with the replies.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (formSubmissionHandle) Download(c *web.Context) error {

	if c.Request.URL.Query().Get("download") != "true" {

		// Returns a URL To the CSV file.
		results := submission.SearchResults{}
		results.CSVURL = fmt.Sprintf("http://%v%v?download=true", c.Request.Host, c.Request.URL.Path)

		c.Respond(results, http.StatusOK)

		return nil
	}

	// Generates and returns the CSV file.

	// It will only arrive to this handler if the formID exists.
	formID := c.Params["form_id"]

	var (
		limit int
		skip  int
	)

	opts := submission.SearchOpts{
		Query:    c.Request.URL.Query().Get("search"),
		FilterBy: c.Request.URL.Query().Get("filterby"),
	}

	if c.Request.URL.Query().Get("orderby") == "dsc" {
		opts.DscOrder = true
	}

	results, err := submission.Search(c.SessionID, c.Ctx["DB"].(*db.DB), formID, limit, skip, opts)
	if err != nil {
		return err
	}

	// Convert into [][]string to encode the CSV.
	csvData, err := encodeSubmissionsToCSV(results.Submissions)
	if err != nil {
		return err
	}

	// Set the content type.
	c.Header().Set("Content-Type", "text/csv")
	c.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"ask_%s_%s.csv\"", formID, time.Now().String()))

	c.WriteHeader(http.StatusOK)
	c.Status = http.StatusOK

	c.Write(csvData)

	return nil
}

作者:coralprojec    项目:xeni   
// Retrieve retrieves a FormGallery based on it's id.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (formGalleryHandle) Retrieve(c *web.Context) error {
	id := c.Params["id"]

	gallery, err := gallery.Retrieve(c.SessionID, c.Ctx["DB"].(*db.DB), id)
	if err != nil {
		return err
	}

	c.Respond(gallery, http.StatusOK)
	return nil
}

作者:coralprojec    项目:xeni   
// Delete removes the specified mask from the system.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (maskHandle) Delete(c *web.Context) error {
	if err := mask.Delete(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["collection"], c.Params["field"]); err != nil {
		if err == mask.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(nil, http.StatusNoContent)
	return nil
}

作者:coralprojec    项目:xeni   
// Delete removes the specified Relationship from the system.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (relationshipHandle) Delete(c *web.Context) error {
	if err := relationship.Delete(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["predicate"]); err != nil {
		if err == relationship.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(nil, http.StatusNoContent)
	return nil
}

作者:coralprojec    项目:xeni   
// Delete removes a single form from the store.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (formHandle) Delete(c *web.Context) error {
	id := c.Params["id"]

	err := form.Delete(c.SessionID, c.Ctx["DB"].(*db.DB), id)
	if err != nil {
		return err
	}

	c.Respond(nil, http.StatusOK)
	return nil
}

作者:coralprojec    项目:xeni   
// RetrieveForForm retrieves a collection of galleries based on a specific form
// id.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (formGalleryHandle) RetrieveForForm(c *web.Context) error {
	formID := c.Params["form_id"]

	galleries, err := gallery.List(c.SessionID, c.Ctx["DB"].(*db.DB), formID)
	if err != nil {
		return err
	}

	c.Respond(galleries, http.StatusOK)
	return nil
}

作者:coralprojec    项目:xeni   
// Delete removes the specified View from the system.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (viewHandle) Delete(c *web.Context) error {
	if err := view.Delete(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["name"]); err != nil {
		if err == view.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(nil, http.StatusNoContent)
	return nil
}

作者:coralprojec    项目:xeni   
// Retrieves a given FormSubmission based on the route params.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (formSubmissionHandle) Retrieve(c *web.Context) error {
	id := c.Params["id"]

	s, err := submission.Retrieve(c.SessionID, c.Ctx["DB"].(*db.DB), id)
	if err != nil {
		return err
	}

	c.Respond(s, http.StatusOK)

	return nil
}

作者:coralprojec    项目:xeni   
// List returns all the existing scripts in the system.
// 200 Success, 404 Not Found, 500 Internal
func (scriptHandle) List(c *web.Context) error {
	scrs, err := script.GetAll(c.SessionID, c.Ctx["DB"].(*db.DB), nil)
	if err != nil {
		if err == script.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(scrs, http.StatusOK)
	return nil
}

作者:coralprojec    项目:xeni   
// Retrieve returns the specified Pattern from the system.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (patternHandle) Retrieve(c *web.Context) error {
	p, err := pattern.GetByType(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["type"])
	if err != nil {
		if err == pattern.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(p, http.StatusOK)
	return nil
}

作者:coralprojec    项目:xeni   
// List returns all the existing relationships in the system.
// 200 Success, 404 Not Found, 500 Internal
func (relationshipHandle) List(c *web.Context) error {
	rels, err := relationship.GetAll(c.SessionID, c.Ctx["DB"].(*db.DB))
	if err != nil {
		if err == relationship.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(rels, http.StatusOK)
	return nil
}

作者:coralprojec    项目:xeni   
// Retrieve returns the specified Relationship from the system.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (relationshipHandle) Retrieve(c *web.Context) error {
	rel, err := relationship.GetByPredicate(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["predicate"])
	if err != nil {
		if err == relationship.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(rel, http.StatusOK)
	return nil
}

作者:coralprojec    项目:xeni   
// List returns all the existing views in the system.
// 200 Success, 404 Not Found, 500 Internal
func (viewHandle) List(c *web.Context) error {
	views, err := view.GetAll(c.SessionID, c.Ctx["DB"].(*db.DB))
	if err != nil {
		if err == view.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(views, http.StatusOK)
	return nil
}

作者:coralprojec    项目:xeni   
// List returns all the existing patterns in the system.
// 200 Success, 404 Not Found, 500 Internal
func (patternHandle) List(c *web.Context) error {
	ps, err := pattern.GetAll(c.SessionID, c.Ctx["DB"].(*db.DB))
	if err != nil {
		if err == pattern.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(ps, http.StatusOK)
	return nil
}

作者:coralprojec    项目:xeni   
// Retrieve returns the specified View from the system.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (viewHandle) Retrieve(c *web.Context) error {
	v, err := view.GetByName(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["name"])
	if err != nil {
		if err == view.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(v, http.StatusOK)
	return nil
}

作者:coralprojec    项目:xeni   
// UpdateStatus updates the status of a form in the store.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (formHandle) UpdateStatus(c *web.Context) error {
	id := c.Params["id"]
	status := c.Params["status"]

	f, err := form.UpdateStatus(c.SessionID, c.Ctx["DB"].(*db.DB), id, status)
	if err != nil {
		return err
	}

	c.Respond(f, http.StatusOK)
	return nil
}

作者:coralprojec    项目:xeni   
// Upsert inserts or updates the posted Relationship document into the database.
// 204 SuccessNoContent, 400 Bad Request, 404 Not Found, 500 Internal
func (relationshipHandle) Upsert(c *web.Context) error {
	var rel relationship.Relationship
	if err := json.NewDecoder(c.Request.Body).Decode(&rel); err != nil {
		return err
	}

	if err := relationship.Upsert(c.SessionID, c.Ctx["DB"].(*db.DB), &rel); err != nil {
		return err
	}

	c.Respond(nil, http.StatusNoContent)
	return nil
}


问题


面经


文章

微信
公众号

扫码关注公众号