Add database
This commit is contained in:
@@ -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}
|
||||
}
|
||||
|
@@ -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))
|
||||
}
|
||||
}
|
||||
|
@@ -7,5 +7,5 @@ import (
|
||||
)
|
||||
|
||||
func RoutesInit(router *gin.Engine, pool *pgxpool.Pool) {
|
||||
router.GET("/", handler.MainPage())
|
||||
router.GET("/", handler.MainPage(pool))
|
||||
}
|
||||
|
Reference in New Issue
Block a user