// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// Upsert inserts or updates the posted Script document into the database. // 204 SuccessNoContent, 400 Bad Request, 404 Not Found, 500 Internal func (scriptHandle) Upsert(c *web.Context) error { var scr script.Script if err := json.NewDecoder(c.Request.Body).Decode(&scr); err != nil { return err } if err := script.Upsert(c.SessionID, c.Ctx["DB"].(*db.DB), scr); err != nil { return err } c.Respond(nil, http.StatusNoContent) return nil }
// AddAnswer removes an answer from a form gallery in the store. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (formGalleryHandle) RemoveAnswer(c *web.Context) error { id := c.Params["id"] submissionID := c.Params["submission_id"] answerID := c.Params["answer_id"] gallery, err := gallery.RemoveAnswer(c.SessionID, c.Ctx["DB"].(*db.DB), id, submissionID, answerID) if err != nil { return err } c.Respond(gallery, http.StatusOK) return nil }
// Upsert inserts or updates the posted Pattern document into the database. // 204 SuccessNoContent, 400 Bad Request, 404 Not Found, 500 Internal func (patternHandle) Upsert(c *web.Context) error { var p pattern.Pattern if err := json.NewDecoder(c.Request.Body).Decode(&p); err != nil { return err } if err := pattern.Upsert(c.SessionID, c.Ctx["DB"].(*db.DB), &p); err != nil { return err } c.Respond(nil, http.StatusNoContent) return nil }
// Upsert inserts or updates the posted mask document into the database. // 204 SuccessNoContent, 400 Bad Request, 404 Not Found, 500 Internal func (maskHandle) Upsert(c *web.Context) error { var msk mask.Mask if err := json.NewDecoder(c.Request.Body).Decode(&msk); err != nil { return err } if err := mask.Upsert(c.SessionID, c.Ctx["DB"].(*db.DB), msk); err != nil { return err } c.Respond(nil, http.StatusNoContent) return nil }
// Upsert inserts or updates the posted Set document into the database. // 204 SuccessNoContent, 400 Bad Request, 404 Not Found, 500 Internal func (queryHandle) Upsert(c *web.Context) error { var set query.Set if err := json.NewDecoder(c.Request.Body).Decode(&set); err != nil { return err } if err := query.Upsert(c.SessionID, c.Ctx["DB"].(*db.DB), &set); err != nil { return err } c.Respond(nil, http.StatusNoContent) return nil }
// Upsert inserts or updates the posted Regex document into the database. // 204 SuccessNoContent, 400 Bad Request, 404 Not Found, 500 Internal func (regexHandle) Upsert(c *web.Context) error { var rgx regex.Regex if err := json.NewDecoder(c.Request.Body).Decode(&rgx); err != nil { return err } if err := regex.Upsert(c.SessionID, c.Ctx["DB"].(*db.DB), rgx); err != nil { return err } c.Respond(nil, http.StatusNoContent) return nil }
// AddFlag adds a new flag to a given FormSubmission based on the provided route // params. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (formSubmissionHandle) AddFlag(c *web.Context) error { id := c.Params["id"] flag := c.Params["flag"] s, err := submission.AddFlag(c.SessionID, c.Ctx["DB"].(*db.DB), id, flag) if err != nil { return err } c.Respond(s, http.StatusOK) return nil }
// Upsert inserts or updates the posted View document into the database. // 204 SuccessNoContent, 400 Bad Request, 404 Not Found, 500 Internal func (viewHandle) Upsert(c *web.Context) error { var v view.View if err := json.NewDecoder(c.Request.Body).Decode(&v); err != nil { return err } if err := view.Upsert(c.SessionID, c.Ctx["DB"].(*db.DB), &v); err != nil { return err } c.Respond(nil, http.StatusNoContent) return nil }
// Removes a FormSubmission based on the route params. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (formSubmissionHandle) Delete(c *web.Context) error { id := c.Params["id"] formID := c.Params["form_id"] err := ask.DeleteSubmission(c.SessionID, c.Ctx["DB"].(*db.DB), id, formID) if err != nil { return err } c.Respond(nil, http.StatusOK) return nil }
// SubmissionGroup retrieves submissions on a form by search criteria, // groups them then returns text aggregations. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (aggregationHandle) SubmissionGroup(c *web.Context) error { id := c.Params["form_id"] // Unpack the search headers and create a SearchOpts for Group Submissions. 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 } groups, err := form.GroupSubmissions(c.SessionID, c.Ctx["DB"].(*db.DB), id, limit, skip, opts) if err != nil { return err } groupKey, ok := c.Params["group_id"] if !ok { c.Respond(nil, http.StatusNotFound) } ta := []form.TextAggregation{} for group, submissions := range groups { if group.ID == groupKey { ta, err = form.TextAggregate(c.SessionID, c.Ctx["DB"].(*db.DB), id, submissions) if err != nil { return err } } } c.Respond(ta, http.StatusOK) return nil }