Moved implementation to fancy handler with interfaces to avoid duplicate code
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
ab1750760a
commit
a6a9b6c936
|
@ -1,15 +1,15 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
type Community struct {
|
type Community struct {
|
||||||
ID int64 `xorm:"pk autoincr" json:"id"`
|
ID int64 `xorm:"pk autoincr" json:"id" form:"id"`
|
||||||
Name string `xorm:"text" json:"name"`
|
Name string `xorm:"text" json:"name" form:"name"`
|
||||||
KCoins int64 `xorm:"bigint(11)" json:"kcoins"`
|
KCoins int64 `xorm:"bigint(11)" json:"kcoins"`
|
||||||
|
|
||||||
KonfiCount int64 `xorm:"bigint(11)" json:"konfi_count"`
|
KonfiCount int64 `xorm:"bigint(11)" json:"konfi_count" form:"konfis"`
|
||||||
CoinsQuota float64 `xorm:"-" json:"coins_quota"`
|
CoinsQuota float64 `xorm:"-" json:"coins_quota"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Community) Add() (err error) {
|
func (c *Community) Create() (err error) {
|
||||||
_, err = x.Insert(c)
|
_, err = x.Insert(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -19,25 +19,26 @@ func (c *Community) Delete() (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadAllCommunities(orderbyname bool) (communities []*Community, err error) {
|
func (c *Community) ReadAll(orderby string) (interface{}, error) {
|
||||||
|
|
||||||
orderby := "CoinsQuota DESC"
|
orderbyStmt := "CoinsQuota DESC"
|
||||||
if orderbyname {
|
if orderby == "name" {
|
||||||
orderby = "Name ASC"
|
orderbyStmt = "Name ASC"
|
||||||
}
|
}
|
||||||
|
|
||||||
err = x.Select("*, (cast(k_coins AS FLOAT) / cast(konfi_count AS FLOAT)) as CoinsQuota").
|
communities := []*Community{}
|
||||||
OrderBy(orderby).
|
err := x.Select("*, (cast(k_coins AS FLOAT) / cast(konfi_count AS FLOAT)) as CoinsQuota").
|
||||||
|
OrderBy(orderbyStmt).
|
||||||
Find(&communities)
|
Find(&communities)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, c := range communities {
|
for i, c := range communities {
|
||||||
communities[i].CoinsQuota = float64(c.KCoins) / float64(c.KonfiCount)
|
communities[i].CoinsQuota = float64(c.KCoins) / float64(c.KonfiCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return communities, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Community) Update(moreCoins int64) (err error) {
|
func (c *Community) Update(moreCoins int64) (err error) {
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
type Kofi struct {
|
type Kofi struct {
|
||||||
ID int64 `xorm:"pk autoincr" json:"id"`
|
ID int64 `xorm:"pk autoincr" json:"id" form:"id"`
|
||||||
Name string `xorm:"text" json:"name"`
|
Name string `xorm:"text" json:"name" form:"name"`
|
||||||
Gemeinde string `xorm:"text" json:"gemeinde"`
|
Gemeinde string `xorm:"text" json:"gemeinde" form:"gemeinde"`
|
||||||
KCoins int64 `xorm:"bigint(11)" json:"kcoins"`
|
KCoins int64 `xorm:"bigint(11)" json:"kcoins"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *Kofi) Add() (err error) {
|
func (k *Kofi) Create() (err error) {
|
||||||
_, err = x.Insert(k)
|
_, err = x.Insert(k)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -34,11 +34,12 @@ func (k *Kofi) Update(moreCoins int64) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadAllKofis(orderbyNames bool) (kofis []*Kofi, err error) {
|
func (k *Kofi) ReadAll(orderby string) (interface{}, error) {
|
||||||
var orderby = "k_coins DESC"
|
var orderbyStmt = "k_coins DESC"
|
||||||
if orderbyNames {
|
if orderby == "name" {
|
||||||
orderby = "name ASC"
|
orderbyStmt = "name ASC"
|
||||||
}
|
}
|
||||||
err = x.OrderBy(orderby).Find(&kofis)
|
kofis := []*Kofi{}
|
||||||
return
|
err := x.OrderBy(orderbyStmt).Find(&kofis)
|
||||||
|
return kofis, err
|
||||||
}
|
}
|
||||||
|
|
8
pkg/models/managable.go
Normal file
8
pkg/models/managable.go
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
type Managable interface {
|
||||||
|
ReadAll(string) (interface{}, error)
|
||||||
|
Create() error
|
||||||
|
Update(int64) error
|
||||||
|
Delete() error
|
||||||
|
}
|
|
@ -1,56 +0,0 @@
|
||||||
package router
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/config"
|
|
||||||
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/models"
|
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"github.com/labstack/gommon/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
func addKonfi(c echo.Context) error {
|
|
||||||
|
|
||||||
if !isLoggedIn(c) {
|
|
||||||
return echo.NewHTTPError(http.StatusForbidden, "Login first.")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mode nach Kofis
|
|
||||||
if config.GetMode() == 0 {
|
|
||||||
kofi := &models.Kofi{}
|
|
||||||
kofi.Name = c.FormValue("name")
|
|
||||||
kofi.Gemeinde = c.FormValue("gemeinde")
|
|
||||||
|
|
||||||
// Einfügen
|
|
||||||
err := kofi.Add()
|
|
||||||
if err != nil {
|
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Error.")
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, "success")
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mode nach Gemeinden
|
|
||||||
if config.GetMode() == 1 {
|
|
||||||
|
|
||||||
var err error
|
|
||||||
community := &models.Community{}
|
|
||||||
community.Name = c.FormValue("name")
|
|
||||||
community.KonfiCount, err = strconv.ParseInt(c.FormValue("konfis"), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "konfiCount not int")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = community.Add()
|
|
||||||
if err != nil {
|
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Error.")
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, "success")
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Error("Invalid mode.")
|
|
||||||
return echo.ErrInternalServerError
|
|
||||||
}
|
|
|
@ -1,42 +0,0 @@
|
||||||
package router
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/config"
|
|
||||||
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/models"
|
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"github.com/labstack/gommon/log"
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
func deleteKonfi(c echo.Context) error {
|
|
||||||
|
|
||||||
if !isLoggedIn(c) {
|
|
||||||
return echo.NewHTTPError(http.StatusForbidden, "Login first.")
|
|
||||||
}
|
|
||||||
|
|
||||||
id, _ := strconv.ParseInt(c.FormValue("id"), 10, 64)
|
|
||||||
|
|
||||||
if config.GetMode() == 0 {
|
|
||||||
k := &models.Kofi{ID: id}
|
|
||||||
err := k.Delete()
|
|
||||||
if err != nil {
|
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Error.")
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, "success")
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.GetMode() == 1 {
|
|
||||||
community := &models.Community{ID: id}
|
|
||||||
err := community.Delete()
|
|
||||||
if err != nil {
|
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Error.")
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, "success")
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Error("Invalid mode.")
|
|
||||||
return echo.ErrInternalServerError
|
|
||||||
}
|
|
|
@ -1,35 +0,0 @@
|
||||||
package router
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"github.com/labstack/gommon/log"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/config"
|
|
||||||
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
func getList(c echo.Context) error {
|
|
||||||
if config.GetMode() == 0 {
|
|
||||||
asc := c.QueryParam("asc")
|
|
||||||
kofis, err := models.ReadAllKofis(asc != "")
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
return echo.ErrInternalServerError
|
|
||||||
}
|
|
||||||
return c.JSON(http.StatusOK, kofis)
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.GetMode() == 1 {
|
|
||||||
asc := c.QueryParam("asc")
|
|
||||||
communities, err := models.ReadAllCommunities(asc != "")
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
return echo.ErrInternalServerError
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, communities)
|
|
||||||
}
|
|
||||||
|
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "Error. (Wrong mode)")
|
|
||||||
}
|
|
94
pkg/router/handler.go
Normal file
94
pkg/router/handler.go
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
package router
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/models"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/labstack/gommon/log"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Handler struct {
|
||||||
|
str func() models.Managable
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpdatedMessage struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
Data models.Managable `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) ReadAll(c echo.Context) error {
|
||||||
|
|
||||||
|
str := h.str()
|
||||||
|
if err := c.Bind(str); err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
asc := c.QueryParam("asc")
|
||||||
|
data, err := str.ReadAll(asc)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return echo.ErrInternalServerError
|
||||||
|
}
|
||||||
|
return c.JSON(http.StatusOK, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) Create(c echo.Context) error {
|
||||||
|
|
||||||
|
if !isLoggedIn(c) {
|
||||||
|
return echo.NewHTTPError(http.StatusForbidden, "Login first.")
|
||||||
|
}
|
||||||
|
|
||||||
|
str := h.str()
|
||||||
|
if err := c.Bind(str); err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
err := str.Create()
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusInternalServerError, "Error.")
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(http.StatusOK, "success")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) Delete(c echo.Context) error {
|
||||||
|
if !isLoggedIn(c) {
|
||||||
|
return echo.NewHTTPError(http.StatusForbidden, "Login first.")
|
||||||
|
}
|
||||||
|
|
||||||
|
str := h.str()
|
||||||
|
if err := c.Bind(str); err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
err := str.Delete()
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusInternalServerError, "Error.")
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(http.StatusOK, "success")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) Update(c echo.Context) error {
|
||||||
|
if !isLoggedIn(c) {
|
||||||
|
return echo.NewHTTPError(http.StatusForbidden, "Login first.")
|
||||||
|
}
|
||||||
|
|
||||||
|
str := h.str()
|
||||||
|
if err := c.Bind(str); err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
addcoins, _ := strconv.ParseInt(c.FormValue("addcoins"), 10, 64)
|
||||||
|
err := str.Update(addcoins)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, err)
|
||||||
|
}
|
||||||
|
return c.JSON(http.StatusOK, UpdatedMessage{
|
||||||
|
Message: "success",
|
||||||
|
Data: str,
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,8 @@
|
||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/config"
|
||||||
|
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/models"
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
"github.com/labstack/echo-contrib/session"
|
"github.com/labstack/echo-contrib/session"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
@ -37,14 +39,30 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
e.GET("/admin", adminHandler)
|
e.GET("/admin", adminHandler)
|
||||||
e.GET("/", showList)
|
e.GET("/", showList)
|
||||||
|
|
||||||
e.GET("/list", getList)
|
|
||||||
e.GET("/ws", ws)
|
e.GET("/ws", ws)
|
||||||
|
|
||||||
e.POST("/login", login)
|
e.POST("/login", login)
|
||||||
e.GET("/logout", logout)
|
e.GET("/logout", logout)
|
||||||
|
|
||||||
|
var handler Handler
|
||||||
|
if config.GetMode() == 0 {
|
||||||
|
handler = Handler{
|
||||||
|
str: func() models.Managable {
|
||||||
|
return &models.Kofi{}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if config.GetMode() == 1 {
|
||||||
|
handler = Handler{
|
||||||
|
str: func() models.Managable {
|
||||||
|
return &models.Community{}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
e.GET("/list", handler.ReadAll)
|
||||||
// Routes with auth -> TODO: build a middleware which checks this
|
// Routes with auth -> TODO: build a middleware which checks this
|
||||||
e.POST("/update", update)
|
e.POST("/update", handler.Update)
|
||||||
e.POST("/delete", deleteKonfi)
|
e.POST("/delete", handler.Delete)
|
||||||
e.POST("/add", addKonfi)
|
e.POST("/add", handler.Create)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,60 +0,0 @@
|
||||||
package router
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/config"
|
|
||||||
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/models"
|
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"github.com/labstack/gommon/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
type UpdatedMessageKofi struct {
|
|
||||||
Message string `json:"message"`
|
|
||||||
Data *models.Kofi `json:"data"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type UpdatedMessageCommunity struct {
|
|
||||||
Message string `json:"message"`
|
|
||||||
Data *models.Community `json:"data"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func update(c echo.Context) error {
|
|
||||||
|
|
||||||
if !isLoggedIn(c) {
|
|
||||||
return echo.NewHTTPError(http.StatusForbidden, "Login first.")
|
|
||||||
}
|
|
||||||
|
|
||||||
id, _ := strconv.ParseInt(c.FormValue("id"), 10, 64)
|
|
||||||
addcoins, _ := strconv.ParseInt(c.FormValue("addcoins"), 10, 64)
|
|
||||||
|
|
||||||
if config.GetMode() == 0 {
|
|
||||||
kofi := &models.Kofi{ID: id}
|
|
||||||
err := kofi.Update(addcoins)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, err)
|
|
||||||
}
|
|
||||||
return c.JSON(http.StatusOK, UpdatedMessageKofi{
|
|
||||||
Message: "success",
|
|
||||||
Data: kofi,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.GetMode() == 1 {
|
|
||||||
community := &models.Community{ID: id}
|
|
||||||
err := community.Update(addcoins)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, err)
|
|
||||||
}
|
|
||||||
return c.JSON(http.StatusOK, UpdatedMessageCommunity{
|
|
||||||
Message: "success",
|
|
||||||
Data: community,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Error("Invalid mode.")
|
|
||||||
return echo.ErrInternalServerError
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user