Exemplo n.º 1
0
func (sc *ServiceControllerImpl) recent(w http.ResponseWriter, r *http.Request) {
	services := new(models.Services)
	if err := services.FindMostRecent(3, sc.database.Get(r)); err != nil {
		sc.renderer.JSON(w, http.StatusInternalServerError, map[string]string{
			"error": err.Error(),
		})
		return
	}

	sc.renderer.JSON(w, http.StatusOK, services)
}
Exemplo n.º 2
0
func (ac *AccountControllerImpl) services(w http.ResponseWriter, r *http.Request) {
	user := ac.currentUser.Get(r)
	if user == nil {
		ac.renderer.JSON(w, http.StatusUnauthorized, map[string]string{
			"error": "Must be logged in to view this page",
		})
	}

	var limit int
	var err error

	if limit, err = strconv.Atoi(r.FormValue("limit")); err != nil {
		limit = 11
	}

	if limit > 50 {
		limit = 50
	}

	var skip int
	if skip, err = strconv.Atoi(r.FormValue("skip")); err != nil {
		skip = 0
	}

	var total int
	codes := new(models.ReferralCodes)
	if total, err = codes.FindByUserID(user.ID, limit, skip, ac.database.Get(r)); err != nil {
		ac.renderer.JSON(w, http.StatusInternalServerError, map[string]string{
			"error": "There was an issue fetching codes from the database",
		})
	}

	var serviceIDs []bson.ObjectId
	for _, code := range []models.ReferralCode(*codes) {
		serviceIDs = append(serviceIDs, code.ServiceID)
	}

	services := new(models.Services)
	if err = services.FindByIDs(serviceIDs, ac.database.Get(r)); err != nil {
		ac.renderer.JSON(w, http.StatusInternalServerError, map[string]string{
			"error": "There was an issue fetching services from the database",
		})
	}

	ac.renderer.JSON(w, http.StatusOK, serviceFetchResult{services, total})
}
Exemplo n.º 3
0
func (sc *SearchControllerImpl) get(w http.ResponseWriter, r *http.Request) (searchResult, error) {
	services := new(models.Services)
	query := r.FormValue("q")
	if query == "" {
		return searchResult{services, 0}, nil
	}

	var limit int
	var err error

	if limit, err = strconv.Atoi(r.FormValue("limit")); err != nil {
		limit = 11
	}

	if limit > 50 {
		limit = 50
	}

	var skip int
	if skip, err = strconv.Atoi(r.FormValue("skip")); err != nil {
		skip = 0
	}

	var total int
	db := sc.database.Get(r)
	if total, err = services.FindRelevant(query, limit, skip, db); err != nil {
		return searchResult{}, errors.New("Database error: " + err.Error())
	}

	var userID bson.ObjectId
	if user := sc.currentUser.Get(r); user != nil {
		userID = user.ID
	}

	analytics := new(models.Analytics)
	analytics.AddSearch(query, limit, userID, db)

	return searchResult{services, total}, nil
}