Add database

This commit is contained in:
2020-10-08 14:30:30 +02:00
parent 7ee52c758b
commit 135cd976d0
3 changed files with 22 additions and 10 deletions

View File

@@ -1,18 +1,29 @@
package database
import (
"context"
"github.com/gin-gonic/gin"
"time"
"github.com/jackc/pgx/v4/pgxpool"
"log"
"trikotwaschliste/models"
)
func DbMainpage() gin.H {
func DbMainpage(pool *pgxpool.Pool) gin.H {
var items []models.Washlist
items = append(items, models.Washlist{
Person: "Gerda",
Date: time.Now(),
IsWashed: true,
})
witems, err := pool.Query(context.Background(), "SELECT pe.name, ga.created_at, wa.washed \nFROM washlist wa\nINNER JOIN persons pe ON wa.personid = pe.id\nINNER JOIN gamedata ga ON wa.gamedataid = ga.id")
if err != nil {
log.Println(err.Error())
}
var item models.Washlist
for witems.Next() {
err = witems.Scan(&item.Person, &item.Date, &item.IsWashed)
if err != nil {
log.Println(err.Error())
}
items = append(items, item)
}
return gin.H{"items": items}
}

View File

@@ -2,12 +2,13 @@ package handler
import (
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v4/pgxpool"
"net/http"
"trikotwaschliste/database"
)
func MainPage() gin.HandlerFunc {
func MainPage(pool *pgxpool.Pool) gin.HandlerFunc {
return func(c *gin.Context) {
c.HTML(http.StatusAccepted, "index.html", database.DbMainpage())
c.HTML(http.StatusAccepted, "index.html", database.DbMainpage(pool))
}
}

View File

@@ -7,5 +7,5 @@ import (
)
func RoutesInit(router *gin.Engine, pool *pgxpool.Pool) {
router.GET("/", handler.MainPage())
router.GET("/", handler.MainPage(pool))
}