コード例 #1
0
// transfer amount from between users balance
func (d *UserBalanceController) transferAmount() {
	var input InputRequest
	var from = new(models.UserBalance)
	var to = new(models.UserBalance)
	debug, _ := conf.AppConfig.Bool("debug")
	defer LogErr()
	body, error := ioutil.ReadAll(d.Request.Body)
	error = json.Unmarshal(body, &input)
	if debug {
		Log.WithField("request", string(body)).Printf("transfer - %+v", input)
	}
	if error != nil {
		Log.WithField("error", error).Printf("read body or json error")
	}
	if input.FromUid == 0 || input.Amount == 0 || input.ToUid == 0 {
		d.makeIncorrectRequestParamsReponse("From, To and Amount params is required")
		return
	}

	from = from.GetById(input.FromUid)
	to = to.GetById(input.ToUid)
	if from.Id == 0 || to.Id == 0 {
		d.makeNotFound("balance.not_found", "User not found")
		return
	}
	from.WithdrawAmount(input.Amount)
	to.AddAmount(input.Amount)

	d.ResponseWriter(d.Request, d.Response, "")

	return
}
コード例 #2
0
// withdraw amount from user balance
func (d *UserBalanceController) withdrawAmount() {
	var input InputRequest
	var model = new(models.UserBalance)

	debug, _ := conf.AppConfig.Bool("debug")
	defer LogErr()
	body, error := ioutil.ReadAll(d.Request.Body)
	error = json.Unmarshal(body, &input)
	if debug {
		Log.WithField("request", string(body)).Printf("deposit")
	}
	if error != nil {
		Log.WithField("error", error).Printf("read body or json error")
	}
	if input.UserId == 0 || input.Amount == 0 {
		d.makeIncorrectRequestParamsReponse("User and Amount params is required")
		return
	}

	model = model.GetById(input.UserId)
	if model.Id == 0 {
		d.makeNotFound("balance.not_found", "User not found")
		return
	}

	model.WithdrawAmount(input.Amount)
	d.ResponseWriter(d.Request, d.Response, "")

	return
}