func (withUser WithUser) ServeHTTP(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) defer r.Body.Close() var body struct { FirstName string `json:"firstName" valid:"required"` LastName string `json:"lastName" valid:"required"` Password string `json:"password" valid:"required"` Email string `json:"email" valid:"required,email"` } if err := decoder.Decode(&body); err != nil { respond.With(w, r, http.StatusBadRequest, err) return } if ok, err := govalidator.ValidateStruct(body); ok == false || err != nil { errs := govalidator.ErrorsByField(err) respond.With(w, r, http.StatusBadRequest, errs) return } user := &User{ FirstName: body.FirstName, LastName: body.LastName, Password: body.Password, Email: body.Email, } withUser.next(user).ServeHTTP(w, r) }
func jobs(w http.ResponseWriter, r *http.Request) { searchRequest, err := getJobSearchRequest(r) if err != nil { respond.With(w, r, http.StatusBadRequest, err) return } jobsSearchResults, err := doRequest("jobs", searchRequest) if err != nil { respond.With(w, r, http.StatusBadGateway, err) return } jobs := []interface{}{} for _, hit := range jobsSearchResults.Hits { jobs = append(jobs, hit.Fields) } respond.With(w, r, http.StatusOK, struct { Total int `json:"total"` Items []interface{} `json:"items"` }{ Total: len(jobs), Items: jobs, }) }
func jobTags(w http.ResponseWriter, r *http.Request) { query := bleve.NewMatchAllQuery() searchRequest := bleve.NewSearchRequest(query) searchRequest.Fields = []string{"tags"} tagsFacet := bleve.NewFacetRequest("tags", 99999) searchRequest.AddFacet("tags", tagsFacet) jobsSearchResults, err := doRequest("jobs", searchRequest) if err != nil { respond.With(w, r, http.StatusInternalServerError, err) return } tags := []string{} for _, t := range jobsSearchResults.Facets["tags"].Terms { tags = append(tags, fmt.Sprintf("%s", t.Term)) } respond.With(w, r, http.StatusOK, struct { Total int `json:"total"` Tags []string `json:"tags"` }{ Total: len(tags), Tags: tags, }) }
func freelancers(w http.ResponseWriter, r *http.Request) { searchRequest := bleve.NewSearchRequest(bleve.NewMatchAllQuery()) searchRequest.Fields = []string{"*"} freelnacersSearchResults, err := doRequest("freelancers", searchRequest) if err != nil { respond.With(w, r, http.StatusInternalServerError, err) return } freelancers := []interface{}{} for _, hit := range freelnacersSearchResults.Hits { freelancers = append(freelancers, hit.Fields) } respond.With(w, r, http.StatusOK, struct { Total int `json:"total"` Items []interface{} `json:"items"` }{ Total: len(freelancers), Items: freelancers, }) }