Konfi-Castle-Kasino/update.go

79 lines
1.6 KiB
Go
Raw Normal View History

package main
import (
"github.com/labstack/echo"
"net/http"
"strconv"
)
func update(c echo.Context) error {
2017-08-31 16:37:43 +00:00
//Config
SiteConf := initConfig()
//Datenbankverbindung aufbauen
db := DBinit()
rw := c.Response()
r := c.Request()
//Session init
sess := GlobalSessions.SessionStart(rw, r)
logged := sess.Get("login")
//Wenn das password stimmt
2017-08-30 20:23:35 +00:00
if logged != nil {
id, _ := strconv.Atoi(c.FormValue("id"))
addcoins, _ := strconv.Atoi(c.FormValue("addcoins"))
2017-08-31 16:37:43 +00:00
if SiteConf.Mode == 0 {
//Aktuelle Coins holen
var kofi= Kofi{ID: id}
has, err := db.Get(&kofi)
checkErr(err)
if has {
newCoins := kofi.KCoins + addcoins
//Updaten
kofi.KCoins = newCoins
_, err := db.Id(id).Update(kofi)
if err == nil {
return c.JSON(http.StatusOK, UpdatedMessageKofi{"success", kofi})
}
}
} else if SiteConf.Mode == 1{
var gemeinde = Gemeinde{ID: id}
has, err := db.Get(&gemeinde)
checkErr(err)
if has {
newCoins := gemeinde.KCoins + addcoins
2017-08-31 16:37:43 +00:00
// Updaten
gemeinde.KCoins = newCoins
_, err := db.ID(id).Update(gemeinde)
if err != nil {
return c.JSON(http.StatusInternalServerError, Message{"Error."})
}
_, err = db.ID(id).Get(&gemeinde)
2017-08-31 16:37:43 +00:00
if err == nil {
// Coins pP aausrechnen
gemeinde.CoinsQuota = float64(gemeinde.KCoins) / float64(gemeinde.KonfiCount)
2017-08-31 16:37:43 +00:00
return c.JSON(http.StatusOK, UpdatedMessageGemeinde{"success", gemeinde})
}
}
2017-08-31 16:37:43 +00:00
}
2017-08-31 16:37:43 +00:00
return c.JSON(http.StatusInternalServerError, Message{"Error."})
} else {
2017-08-31 16:37:43 +00:00
return c.JSON(http.StatusForbidden, Message{"Login first."})
}
}