Konfi-Castle-Kasino/update.go

79 lines
1.6 KiB
Go

package main
import (
"github.com/labstack/echo"
"net/http"
"strconv"
)
func update(c echo.Context) error {
//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
if logged != nil {
id, _ := strconv.Atoi(c.FormValue("id"))
addcoins, _ := strconv.Atoi(c.FormValue("addcoins"))
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
// 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)
if err == nil {
// Coins pP aausrechnen
gemeinde.CoinsQuota = float64(gemeinde.KCoins) / float64(gemeinde.KonfiCount)
return c.JSON(http.StatusOK, UpdatedMessageGemeinde{"success", gemeinde})
}
}
}
return c.JSON(http.StatusInternalServerError, Message{"Error."})
} else {
return c.JSON(http.StatusForbidden, Message{"Login first."})
}
}