diff --git a/database/admin.go b/database/admin.go index 19bc533..7dedcd1 100644 --- a/database/admin.go +++ b/database/admin.go @@ -5,21 +5,22 @@ import ( "github.com/gin-gonic/gin" "github.com/jackc/pgx/v4/pgxpool" "log" + "strconv" "trikotwaschliste/models" ) func DbAdminMainpage(pool *pgxpool.Pool) gin.H { - var items []models.Washlist + var items []models.WashlistAdmin var persons []models.Person - witems, err := pool.Query(context.Background(), "SELECT wa.id, 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 ORDER BY ga.created_at ASC") + witems, err := pool.Query(context.Background(), "SELECT wa.id, wa.personid, ga.created_at\nFROM washlist wa\nINNER JOIN gamedata ga ON wa.gamedataid = ga.id ORDER BY ga.created_at ASC") if err != nil { log.Println(err.Error()) } - var item models.Washlist + var item models.WashlistAdmin for witems.Next() { - err = witems.Scan(&item.Id, &item.Person, &item.Date, &item.Washed) + err = witems.Scan(&item.Id, &item.PersonId, &item.Date) if err != nil { log.Println(err.Error()) } @@ -49,3 +50,14 @@ func DbAdminMainpage(pool *pgxpool.Pool) gin.H { return gin.H{"items": items, "persons": persons} } + +func ChangeName(pool *pgxpool.Pool, id, personID string) bool { + sID, _ := strconv.Atoi(id) + if sID <= 0 { + return false + } + + pool.Exec(context.Background(), "UPDATE washlist SET personid = $1 WHERE id = $2", id, personID) + + return true +} diff --git a/handler/admin.go b/handler/admin.go index 91730ae..b6e2ca7 100644 --- a/handler/admin.go +++ b/handler/admin.go @@ -12,3 +12,13 @@ func AdminMainPage(pool *pgxpool.Pool) gin.HandlerFunc { c.HTML(http.StatusAccepted, "adminindex.html", database.DbAdminMainpage(pool)) } } + +func ChangeName(pool *pgxpool.Pool) gin.HandlerFunc { + return func(c *gin.Context) { + selectedID := c.PostForm("names") + personID := c.PostForm("personid") + + database.ChangeName(pool, selectedID, personID) + c.Redirect(302, "/admin") + } +} diff --git a/html/admin/adminindex.html b/html/admin/adminindex.html index a26d5c5..18d7ce5 100644 --- a/html/admin/adminindex.html +++ b/html/admin/adminindex.html @@ -16,7 +16,6 @@