Add persons tab

This commit is contained in:
2020-10-09 17:03:30 +02:00
parent aea74bef27
commit 6e51533596
6 changed files with 125 additions and 1 deletions

67
database/persons.go Normal file
View File

@@ -0,0 +1,67 @@
package database
import (
"context"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v4/pgxpool"
"log"
"trikotwaschliste/models"
)
func DbPersons(pool *pgxpool.Pool) gin.H {
var items []models.PersonListItem
var persons []models.Person
// Get items
witems, err := pool.Query(context.Background(), "SELECT pe.name, count(pe.name)\nFROM washlist wa\nINNER JOIN persons pe ON wa.washed = pe.id\nGROUP BY pe.name\nORDER BY pe.name ASC")
if err != nil {
log.Println(err.Error())
}
var item models.PersonListItem
for witems.Next() {
err = witems.Scan(&item.Name, &item.Count)
if err != nil {
log.Println("Items: " + err.Error())
}
items = append(items, item)
}
// Get full persons list
wpersons, err := pool.Query(context.Background(), "SELECT id, name FROM persons ORDER BY name ASC")
if err != nil {
log.Println(err.Error())
}
var person models.Person
for wpersons.Next() {
err = wpersons.Scan(&person.Id, &person.Name)
if err != nil {
log.Println("Persons: " + err.Error())
}
persons = append(persons, person)
}
// Merge the list in n^2
var merge []models.PersonListItem
merge = append(merge, items...)
for _, p := range persons {
found := false
for _, it := range items {
if p.Name == it.Name {
found = true
break
}
}
if !found {
merge = append(merge, models.PersonListItem{
Name: p.Name,
Count: 0,
})
}
}
return gin.H{"items": merge}
}

15
handler/persons.go Normal file
View File

@@ -0,0 +1,15 @@
package handler
import (
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v4/pgxpool"
"net/http"
"trikotwaschliste/database"
)
func ShowPersonsList(pool *pgxpool.Pool) gin.HandlerFunc {
return func(c *gin.Context) {
c.HTML(http.StatusAccepted, "persons.html", database.DbPersons(pool))
}
}

34
html/persons/persons.html Normal file
View File

@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Trikotwaschliste</title>
<link rel="stylesheet" type="text/css" href="/static/semantic.min.css">
</head>
<body>
{{template "navbar"}}
<div class="ui center">
<h1 class="">Trikotwaschliste</h1>
<table class="ui celled table">
<thead>
<tr>
<th>Person</th>
<th>Waschgänge</th>
</tr>
</thead>
<tbody>
{{ $items := .items}}
{{range $item := $items}}
<tr>
<td>{{$item.Name}}</td>
<td>{{$item.Count}}</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</body>
</html>

View File

@@ -1,7 +1,7 @@
{{define "navbar"}}
<div class="ui inverted menu">
<a href="/"><div class="header item">Trikotwaschliste</div></a>
<a href="/"><div class="header item">Personen</div></a>
<a href="/persons"><div class="header item">Personen</div></a>
<div class="right menu">
<a href="/credits"><div class="item">Credits</div></a>

6
models/personlistitem.go Normal file
View File

@@ -0,0 +1,6 @@
package models
type PersonListItem struct {
Name string `json:"name"`
Count int `json:"count"`
}

View File

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