// add amount to user balance
func (d *UserBalanceController) addAmount() {
	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 {
		model = models.CreateUserBalance(input.UserId, input.Amount, "")
	} else {
		model.AddAmount(input.Amount)
	}

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

	return
}
Example #2
0
///mocks
func AddMockDataModel(id, balance int) {
	services.DB.Where("id = ?", id).Delete(models.UserBalance{})
	models.CreateUserBalance(id, balance, "")
}