Moved implementation to fancy handler with interfaces to avoid duplicate code
continuous-integration/drone/push Build is passing Details

This commit is contained in:
kolaente 2019-09-03 20:08:25 +02:00
parent ab1750760a
commit a6a9b6c936
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
9 changed files with 148 additions and 219 deletions

View File

@ -1,15 +1,15 @@
package models
type Community struct {
ID int64 `xorm:"pk autoincr" json:"id"`
Name string `xorm:"text" json:"name"`
ID int64 `xorm:"pk autoincr" json:"id" form:"id"`
Name string `xorm:"text" json:"name" form:"name"`
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"`
}
func (c *Community) Add() (err error) {
func (c *Community) Create() (err error) {
_, err = x.Insert(c)
return
}
@ -19,25 +19,26 @@ func (c *Community) Delete() (err error) {
return
}
func ReadAllCommunities(orderbyname bool) (communities []*Community, err error) {
func (c *Community) ReadAll(orderby string) (interface{}, error) {
orderby := "CoinsQuota DESC"
if orderbyname {
orderby = "Name ASC"
orderbyStmt := "CoinsQuota DESC"
if orderby == "name" {
orderbyStmt = "Name ASC"
}
err = x.Select("*, (cast(k_coins AS FLOAT) / cast(konfi_count AS FLOAT)) as CoinsQuota").
OrderBy(orderby).
communities := []*Community{}
err := x.Select("*, (cast(k_coins AS FLOAT) / cast(konfi_count AS FLOAT)) as CoinsQuota").
OrderBy(orderbyStmt).
Find(&communities)
if err != nil {
return
return nil, err
}
for i, c := range communities {
communities[i].CoinsQuota = float64(c.KCoins) / float64(c.KonfiCount)
}
return
return communities, nil
}
func (c *Community) Update(moreCoins int64) (err error) {

View File

@ -1,13 +1,13 @@
package models
type Kofi struct {
ID int64 `xorm:"pk autoincr" json:"id"`
Name string `xorm:"text" json:"name"`
Gemeinde string `xorm:"text" json:"gemeinde"`
ID int64 `xorm:"pk autoincr" json:"id" form:"id"`
Name string `xorm:"text" json:"name" form:"name"`
Gemeinde string `xorm:"text" json:"gemeinde" form:"gemeinde"`
KCoins int64 `xorm:"bigint(11)" json:"kcoins"`
}
func (k *Kofi) Add() (err error) {
func (k *Kofi) Create() (err error) {
_, err = x.Insert(k)
return
}
@ -34,11 +34,12 @@ func (k *Kofi) Update(moreCoins int64) (err error) {
return
}
func ReadAllKofis(orderbyNames bool) (kofis []*Kofi, err error) {
var orderby = "k_coins DESC"
if orderbyNames {
orderby = "name ASC"
func (k *Kofi) ReadAll(orderby string) (interface{}, error) {
var orderbyStmt = "k_coins DESC"
if orderby == "name" {
orderbyStmt = "name ASC"
}
err = x.OrderBy(orderby).Find(&kofis)
return
kofis := []*Kofi{}
err := x.OrderBy(orderbyStmt).Find(&kofis)
return kofis, err
}

8
pkg/models/managable.go Normal file
View File

@ -0,0 +1,8 @@
package models
type Managable interface {
ReadAll(string) (interface{}, error)
Create() error
Update(int64) error
Delete() error
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
View 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,
})
}

View File

@ -1,6 +1,8 @@
package router
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/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
@ -37,14 +39,30 @@ func RegisterRoutes(e *echo.Echo) {
e.GET("/admin", adminHandler)
e.GET("/", showList)
e.GET("/list", getList)
e.GET("/ws", ws)
e.POST("/login", login)
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
e.POST("/update", update)
e.POST("/delete", deleteKonfi)
e.POST("/add", addKonfi)
e.POST("/update", handler.Update)
e.POST("/delete", handler.Delete)
e.POST("/add", handler.Create)
}

View File

@ -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
}