Skip to content

Commit

Permalink
add a simple web ui
Browse files Browse the repository at this point in the history
  • Loading branch information
kubegu committed Jan 31, 2024
1 parent 2d6a173 commit 0209750
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 983 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.PHONY: build clean

build-bot:
build-web:
go build ./...
go build -o bin/bot cmd/bot/main.go
go build -o bin/web cmd/web/main.go

build-cli:
go build ./...
Expand Down
81 changes: 0 additions & 81 deletions bot/command.go

This file was deleted.

51 changes: 0 additions & 51 deletions cmd/bot/main.go

This file was deleted.

3 changes: 3 additions & 0 deletions cmd/web/games.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ range .Games }}
<li class="list-group-item">{{ .Title }}</li>
{{ end }}
31 changes: 31 additions & 0 deletions cmd/web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<html>
<head>
<script src="https://unpkg.com/[email protected]" integrity="sha384-D1Kt99CQMDuVetoL1lrYwg5t+9QdHe7NLX/SoJYkXDFfX37iInKRy5xLSi8nO7UC" crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="mb-3">
<form hx-post="/game-index/" hx-target="#display-games">
<div class="mb-3">
<select name="branch" class="form-select">
<option>Stadtbibliothek</option>
<option>Gohlis</option>
</select>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary">Los</button>
</div>
</form>
<div class="card">
<div class="card-header">
Ausleihbare Spiele
</div>
<ul id="display-games" class="list-group list-group-flush">

</ul>
</div>

</div>

</body>
</html>
46 changes: 46 additions & 0 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"log"
"net/http"
"strings"
"text/template"

"github.com/gunni1/leipzig-library-game-stock-api/domain"
libClient "github.com/gunni1/leipzig-library-game-stock-api/library-le"
)

const (
PLATFORM = "switch"
)

func indexHandler(respWriter http.ResponseWriter, request *http.Request) {
templ := template.Must(template.ParseFiles("cmd/web/index.html"))

templ.Execute(respWriter, nil)
}

func gameIndexHandler(respWriter http.ResponseWriter, request *http.Request) {
log.Print("received htmx game-index")
branch := strings.ToLower(request.PostFormValue("branch"))
branchCode, exists := libClient.GetBranchCode(branch)
if !exists {
log.Printf("Requested branch: %s does not exists.", branch)
return
}
client := libClient.Client{}
games := client.FindAvailabelGames(branchCode, PLATFORM)
data := map[string][]domain.Game{
"Games": games,
}
templ := template.Must(template.ParseFiles("cmd/web/games.html"))
templ.Execute(respWriter, data)
}

func main() {
mux := http.NewServeMux()

mux.HandleFunc("/", indexHandler)
mux.HandleFunc("/game-index/", gameIndexHandler)
log.Fatal(http.ListenAndServe(":3000", mux))
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gunni1/leipzig-library-game-stock-api

go 1.20
go 1.21

require github.com/stretchr/testify v1.8.3

Expand All @@ -11,6 +11,5 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.10.0 // indirect
gopkg.in/telebot.v3 v3.1.3
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 0209750

Please sign in to comment.