Exemplo n.º 1
0
func (ac *AccountControllerImpl) deleteAccount(w http.ResponseWriter, r *http.Request) {
	if user := ac.currentUser.Get(r); user != nil {
		db := ac.database.Get(r)

		analytics := new(models.Analytics)
		analytics.AddDeletedUser(user, db)

		user.Delete(db)
	}
	ac.session.Delete(r, "UserID")
	http.Redirect(w, r, "/", http.StatusFound)
}
Exemplo n.º 2
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
}
func (rc *ReferralCodeControllerImpl) create(w http.ResponseWriter, r *http.Request) {
	body, _ := ioutil.ReadAll(r.Body)
	var values map[string]string
	json.Unmarshal(body, &values)

	// verify serviceID
	rawServiceID := values["serviceId"]
	if !bson.IsObjectIdHex(rawServiceID) {
		rc.renderer.JSON(w, http.StatusBadRequest, map[string]string{
			"error": "Bad service ID",
		})
		return
	}

	db := rc.database.Get(r)
	service := new(models.Service)
	serviceID := bson.ObjectIdHex(rawServiceID)
	if err := service.FindByID(serviceID, db); err != nil || !service.ID.Valid() {
		rc.renderer.JSON(w, http.StatusBadRequest, map[string]string{
			"error": "Bad service request",
		})
		return
	}

	userID := rc.currentUser.Get(r).ID
	code := values["code"]
	refCode := new(models.ReferralCode)
	if refCode.FindByUserAndService(userID, serviceID, db); refCode.ID.Valid() {
		if code == "" {
			analytics := new(models.Analytics)
			defer analytics.AddDeletedReferralCode(refCode, db)
			defer refCode.Delete(db)

			rc.renderer.JSON(w, http.StatusOK, nil)
			return
		}

		if err := refCode.Edit(code, db); err != nil {
			rc.renderer.JSON(w, http.StatusBadRequest, map[string]string{
				"error": err.Error(),
			})
			return
		}
	} else {
		if code == "" {
			rc.renderer.JSON(w, http.StatusBadRequest, map[string]string{
				"error": "Empty referral code not allowed",
			})
			return
		}
		refCode = models.NewReferralCode(code, rc.currentUser.Get(r).ID, service.ID)
		if err := refCode.Save(db); err != nil {
			rc.renderer.JSON(w, http.StatusBadRequest, map[string]string{
				"error": err.Error(),
			})
			return
		}
	}

	rc.renderer.JSON(w, http.StatusCreated, refCode)
}