Add migrations

This commit is contained in:
2020-10-09 15:10:13 +02:00
parent 135cd976d0
commit 011619d3c3
8 changed files with 81 additions and 9 deletions

View File

@@ -5,25 +5,54 @@ import (
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v4/pgxpool"
"log"
"strconv"
"trikotwaschliste/models"
)
func DbMainpage(pool *pgxpool.Pool) gin.H {
var items []models.Washlist
var persons []models.Person
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")
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")
if err != nil {
log.Println(err.Error())
}
var item models.Washlist
for witems.Next() {
err = witems.Scan(&item.Person, &item.Date, &item.IsWashed)
err = witems.Scan(&item.Id, &item.Person, &item.Date, &item.Washed)
if err != nil {
log.Println(err.Error())
}
items = append(items, item)
}
return gin.H{"items": items}
// get persons
wperson, err := pool.Query(context.Background(), "SELECT id, name FROM persons")
if err != nil {
log.Println(err.Error())
}
var person models.Person
for wperson.Next() {
err = wperson.Scan(&person.Id, &person.Name)
if err != nil {
log.Println(err.Error())
}
persons = append(persons, person)
}
return gin.H{"items": items, "persons": persons}
}
func UploadName(pool *pgxpool.Pool, id, washID string) bool {
sID, _ := strconv.Atoi(id)
if sID <= 0 {
return false
}
pool.Exec(context.Background(), "UPDATE washlist SET washed = $1 WHERE id = $2", id, washID)
return true
}

View File

@@ -0,0 +1,3 @@
--
-- DROP/Reverse everything here
--

View File

@@ -0,0 +1,4 @@
ALTER TABLE public.washlist
ALTER COLUMN washed TYPE character varying(100);
ALTER TABLE public.washlist
ALTER COLUMN washed DROP NOT NULL;

View File

@@ -12,3 +12,13 @@ func MainPage(pool *pgxpool.Pool) gin.HandlerFunc {
c.HTML(http.StatusAccepted, "index.html", database.DbMainpage(pool))
}
}
func UploadName(pool *pgxpool.Pool) gin.HandlerFunc {
return func(c *gin.Context) {
selectedID := c.PostForm("names")
washID := c.PostForm("washid")
database.UploadName(pool, selectedID, washID)
c.Redirect(302, "/")
}
}

View File

@@ -9,21 +9,39 @@
{{template "navbar"}}
<div class="ui center">
<h1 class="">Trikotwaschliste <small>Version: {{ .version }}</small></h1>
<h1 class="">Trikotwaschliste</h1>
<table class="ui celled table">
<thead>
<tr>
<th>Person</th>
<th>Datum</th>
<th>Gewaschen?</th>
<th>Waschperson</th>
</tr>
</thead>
<tbody>
{{range .items}}
{{ $items := .items}}
{{ $persons := .persons}}
{{range $item := $items}}
<tr>
<td>{{.Person}}</td>
<td>{{.Date}}</td>
<td>{{.IsWashed}}</td>
<td>{{$item.Person}}</td>
<td>{{$item.Date}}</td>
<td>{{$item.Washed}}</td>
<td>
<div class="center">
<form class="ui form" action="/uploadname" method="POST">
<input type="hidden" id="washid" name="washid" value="{{ $item.Id}}">
<select name="names" id="names">
<option value="-1" selected></option>
{{range $person := $persons}}
<option value="{{$person.Id}}">{{$person.Name}}</option>
{{end}}
</select>
<input type="submit" value="Speichern">
</form>
</div>
</td>
</tr>
{{end}}
</tbody>

6
models/person.go Normal file
View File

@@ -0,0 +1,6 @@
package models
type Person struct {
Id int `json:"id"`
Name string `json:"name"`
}

View File

@@ -3,7 +3,8 @@ package models
import "time"
type Washlist struct {
Id int `json:"id"`
Person string `json:"person"`
Date time.Time `json:"date"`
IsWashed bool `json:"iswashed"`
Washed string `json:"iswashed"`
}

View File

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