Beispiel #1
0
func GetCustomer(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var err error
	var c customer.Customer

	if err = c.GetCustomerIdFromKey(dtx.APIKey); err != nil {
		apierror.GenerateError("Trouble getting customer ID", err, rw, r)
		return ""
	}

	if err = c.GetCustomer(dtx.APIKey); err != nil {
		apierror.GenerateError("Trouble getting customer", err, rw, r, http.StatusServiceUnavailable)
		return ""
	}

	lowerKey := strings.ToLower(dtx.APIKey)
	for i, u := range c.Users {
		for _, k := range u.Keys {
			if strings.ToLower(k.Key) == lowerKey {
				c.Users[i].Current = true
			}
		}
	}

	return encoding.Must(enc.Encode(c))
}
Beispiel #2
0
func approveuser(r *http.Request) bool {
	api := r.URL.Query().Get("key")
	if api == "" {
		return false
	}
	c := customer.Customer{}
	err := c.GetCustomerIdFromKey(api)
	if err != nil || c.Id == 0 {
		return false
	}
	return true
}
func GetByPrivateKey(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var err error
	var webProperties webProperty_model.WebProperties

	privateKey := r.FormValue("key")
	cust := customer.Customer{}

	if err = cust.GetCustomerIdFromKey(privateKey); err != nil {
		apierror.GenerateError("Trouble getting customer", err, rw, r)
		return ""
	}

	if webProperties, err = webProperty_model.GetByCustomer(cust.Id, dtx); err != nil {
		apierror.GenerateError("Trouble getting web property", err, rw, r)
		return ""
	}

	return encoding.Must(enc.Encode(webProperties))
}
func CreateUpdateWebProperty(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var w webProperty_model.WebProperty
	var err error

	cust := customer.Customer{}

	if err = cust.GetCustomerIdFromKey(dtx.APIKey); err != nil {
		apierror.GenerateError("Trouble getting customer ID from API Key", err, rw, r)
		return ""
	}

	w.CustID = cust.Id

	//determine content type
	contType := r.Header.Get("Content-Type")
	if contType == "application/json" {
		//json
		requestBody, err := ioutil.ReadAll(r.Body)
		if err != nil {
			apierror.GenerateError("Trouble reading request body while saving web property", err, rw, r)
			return ""
		}

		if err = json.Unmarshal(requestBody, &w); err != nil {
			apierror.GenerateError("Trouble unmarshalling request body while saving web property", err, rw, r)
			return ""
		}
	} else {
		if r.FormValue("id") != "" || params["id"] != "" {
			idStr := r.FormValue("id")
			if idStr == "" {
				idStr = params["id"]
			}

			if w.ID, err = strconv.Atoi(idStr); err != nil {
				apierror.GenerateError("Trouble getting web property ID", err, rw, r)
				return ""
			}

			if err = w.Get(dtx); err != nil {
				apierror.GenerateError("Trouble getting web property", err, rw, r)
				return ""
			}
		}

		name := r.FormValue("name")
		url := r.FormValue("url")
		isEnabled := r.FormValue("isEnabled")
		sellerID := r.FormValue("sellerID")
		webPropertyTypeID := r.FormValue("webPropertyTypeID")
		isFinalApproved := r.FormValue("isApproved")
		enabledDate := r.FormValue("enabledDate")
		isDenied := r.FormValue("isDenied")
		requestedDate := r.FormValue("requestedDate")
		typeID := r.FormValue("typeID")

		if name != "" {
			w.Name = name
		}
		if url != "" {
			w.Url = url
		}
		if isEnabled != "" {
			if w.IsEnabled, err = strconv.ParseBool(isEnabled); err != nil {
				apierror.GenerateError("Trouble parsing boolean value for webproperty.isEnabled", err, rw, r)
				return ""
			}
		}
		if sellerID != "" {
			w.SellerID = sellerID
		}
		if webPropertyTypeID != "" {
			if w.WebPropertyType.ID, err = strconv.Atoi(webPropertyTypeID); err != nil {
				apierror.GenerateError("Trouble getting web property type ID", err, rw, r)
				return ""
			}
		}
		if isFinalApproved != "" {
			if w.IsFinalApproved, err = strconv.ParseBool(isFinalApproved); err != nil {
				apierror.GenerateError("Trouble parsing boolean value for webproperty.isApproved", err, rw, r)
				return ""
			}
		}
		if enabledDate != "" {
			en, err := time.Parse(timeFormat, enabledDate)
			if err != nil {
				apierror.GenerateError("Trouble parsing date for webproperty.enabledDate", err, rw, r)
				return ""
			}
			w.IsEnabledDate = &en
		}
		if isDenied != "" {
			if w.IsDenied, err = strconv.ParseBool(isDenied); err != nil {
				apierror.GenerateError("Trouble parsing boolean value for webproperty.isDenied", err, rw, r)
				return ""
			}
		}
		if requestedDate != "" {
			req, err := time.Parse(timeFormat, requestedDate)
			if err != nil {
				apierror.GenerateError("Trouble parsing boolean value for webproperty.requestedDate", err, rw, r)
				return ""
			}
			w.RequestedDate = &req
		}
		if typeID != "" {
			if w.WebPropertyType.ID, err = strconv.Atoi(typeID); err != nil {
				apierror.GenerateError("Trouble getting type ID", err, rw, r)
				return ""
			}
		}
	}

	if w.ID > 0 {
		err = w.Update(dtx)
	} else {
		//notes (text) and property requirements (reqID) can be created when web property is created
		notes := r.Form["note"]
		for _, v := range notes {
			var n webProperty_model.WebPropertyNote
			n.Text = v
			w.WebPropertyNotes = append(w.WebPropertyNotes, n)
		}
		reqIDs := r.Form["requirement"]
		for _, v := range reqIDs {
			var n webProperty_model.WebPropertyRequirement
			n.RequirementID, err = strconv.Atoi(v)
			w.WebPropertyRequirements = append(w.WebPropertyRequirements, n)
		}
		err = w.Create(dtx)
	}

	if err != nil {
		msg := "Trouble creating web property"
		if w.ID > 0 {
			msg = "Trouble updating web property"
		}
		apierror.GenerateError(msg, err, rw, r)
		return ""
	}
	return encoding.Must(enc.Encode(w))
}