Add admin index page

This commit is contained in:
2020-10-11 23:02:57 +02:00
parent e5c0df6f6f
commit 2a1ef7f2cb
5 changed files with 39 additions and 11 deletions

View File

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

View File

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

View File

@@ -16,7 +16,6 @@
<tr>
<th>Person</th>
<th>Datum</th>
<th>Waschperson</th>
</tr>
</thead>
<tbody>
@@ -24,16 +23,14 @@
{{ $persons := .persons}}
{{range $item := $items}}
<tr>
<td>{{$item.Person}}</td>
<td>{{$item.DateString}}</td>
<td>
<div class="center">
<form class="ui form" action="/uploadname" method="POST">
<input type="hidden" id="washid" name="washid" value="{{ $item.Id}}">
<form class="ui form" action="/admin/changename" method="POST">
<input type="hidden" id="personid" name="personid" value="{{ $item.Id}}">
<select name="names" id="names">
<!--<option value="-1" selected disabled hidden>Choose here</option>-->
<!--<option value="-1" selected disabled hidden>Choose here</option>-->
{{range $person := $persons}}
{{if eq $item.Washed $person.Id}}
{{if eq $item.PersonId $person.Id}}
<option value="{{$person.Id}}" selected>{{$person.Name}}</option>
{{else}}
<option value="{{$person.Id}}">{{$person.Name}}</option>
@@ -45,6 +42,7 @@
</div>
</td>
<td>{{$item.DateString}}</td>
</tr>
{{end}}
</tbody>

View File

@@ -9,3 +9,10 @@ type Washlist struct {
Washed int `json:"iswashed"`
DateString string `json:"datestring"`
}
type WashlistAdmin struct {
Id int `json:"id"`
PersonId int `json:"personid"`
Date time.Time `json:"date"`
DateString string `json:"datestring"`
}

View File

@@ -21,5 +21,6 @@ func RoutesInit(router *gin.Engine, pool *pgxpool.Pool, config *models.Config) {
// Basic auth routes
authorized.GET("/", handler.AdminMainPage(pool))
authorized.POST("/changename", handler.ChangeName(pool))
}