You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

55 lines
1.1 KiB

package main
import (
"encoding/json"
"net/http"
"github.com/bvinc/go-sqlite-lite/sqlite3"
"git.sporestack.com/SporeStack/settlers"
)
func add(token string, amount uint64, db *sqlite3.Conn) (userErr, err error) {
if userErr = settlers.ValidateToken(token); userErr != nil {
return
}
// Check if account is enabled.
_, userErr, err = balance(token, db)
if userErr != nil || err != nil {
return
}
// sqlite3 only supports signed integers
var signedAmount int64
signedAmount = int64(amount)
err = db.Exec("UPDATE balances SET balance = balance + ? WHERE combined_token = ?", signedAmount, token)
if err != nil {
return
}
return
}
func httpAdd(w http.ResponseWriter, r *http.Request, db *sqlite3.Conn) {
defer r.Body.Close()
var request settlers.AddRequest
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
userErr := decoder.Decode(&request)
if userErr != nil {
httpHandle400(w, userErr)
return
}
userErr, err := add(request.CombinedToken, request.Amount, db)
if userErr != nil {
httpHandle400(w, userErr)
return
}
if err != nil {
httpHandle500(w, err)
return
}
return
}