// 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 }
// get balance bu uid func (d *UserBalanceController) getBalance() { var model = new(models.UserBalance) var userId int debug, _ := conf.AppConfig.Bool("debug") defer LogErr() request := d.Request.URL.Query() userIdString := request.Get("user") userId, _ = strconv.Atoi(userIdString) if debug { Log.WithField("request", request).Printf("balance", userIdString) } if userId == 0 { d.makeIncorrectRequestParamsReponse("user param is required") return } model = model.GetById(userId) if model.Id != 0 { resp := &BalanceResponse{ Balance: model.Balance, } d.ResponseWriter(d.Request, d.Response, resp) } else { d.makeNotFound("balance.not_found", "User not found") return } return }
// 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 }
func TestDepositeNew(t *testing.T) { DeleteMockDataModel(101) conf.AppConfig.Set("enable_gzip", true) b := bytes.NewBufferString(`{"user":101, "amount":100}`) r, _ := http.NewRequest("POST", "/deposite", b) r.Header.Add("Accept-Encoding", "deflate, gzip") w := httptest.NewRecorder() var reader io.ReadCloser router := httprouter.New() router.POST("/deposite", controllers.AddAmount) router.ServeHTTP(w, r) var responseText string switch w.Header().Get("Content-Encoding") { case "gzip": reader, _ = gzip.NewReader(w.Body) var uncompressed []byte uncompressed, _ = ioutil.ReadAll(reader) responseText = string(uncompressed) defer reader.Close() default: responseText = w.Body.String() } log.Printf("\n response == ", responseText, w.Header().Get("Content-Encoding")) var model = new(models.UserBalance) model = model.GetById(101) Convey("Subject: Test Deposite \n", t, func() { Convey("Status Code Should Be 200", func() { So(w.Code, ShouldEqual, 200) }) Convey("Db record must be exists", func() { So(model.Balance, ShouldEqual, 100) So(model.Id, ShouldEqual, 101) }) }) }
func TestTransfer(t *testing.T) { var model = new(models.UserBalance) var model2 = new(models.UserBalance) AddMockDataModel(101, 100) AddMockDataModel(205, 100) conf.AppConfig.Set("enable_gzip", true) b := bytes.NewBufferString(`{"from":101, "to":205,"amount":25}`) r, _ := http.NewRequest("POST", "/transfer", b) r.Header.Add("Accept-Encoding", "deflate, gzip") w := httptest.NewRecorder() var reader io.ReadCloser router := httprouter.New() router.POST("/transfer", controllers.TransferAmount) router.ServeHTTP(w, r) var responseText string switch w.Header().Get("Content-Encoding") { case "gzip": reader, _ = gzip.NewReader(w.Body) var uncompressed []byte uncompressed, _ = ioutil.ReadAll(reader) responseText = string(uncompressed) defer reader.Close() default: responseText = w.Body.String() } log.Printf("\n response == ", responseText, w.Header().Get("Content-Encoding")) modelFrom := model.GetById(101) modelTo := model2.GetById(205) Convey("Subject: Test Transfer \n", t, func() { Convey("Status Code Should Be 200", func() { So(w.Code, ShouldEqual, 200) }) Convey("Db record from balance must equals to 75", func() { So(modelFrom.Balance, ShouldEqual, 75) }) Convey("Db record to balance must equals to 125", func() { So(modelTo.Balance, ShouldEqual, 125) }) }) }