コード例 #1
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})
}