45 lines
874 B
Go
45 lines
874 B
Go
package main
|
|
|
|
import (
|
|
"github.com/labstack/echo"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
func update(c echo.Context) error {
|
|
//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"))
|
|
|
|
//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, UpdatedMessage{"success", kofi})
|
|
}
|
|
}
|
|
return c.JSON(http.StatusOK, Message{"Error."})
|
|
} else {
|
|
return c.JSON(http.StatusOK, Message{"Login first."})
|
|
}
|
|
}
|