FIX: warnings
This commit is contained in:
parent
67807992bc
commit
c32ecbff8f
57 changed files with 1898 additions and 21 deletions
5
out/production/iroon/internal/server/fetcher.go
Normal file
5
out/production/iroon/internal/server/fetcher.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package server
|
||||
|
||||
import "net/http"
|
||||
|
||||
type fetcher func(r *http.Request) (any, int, error)
|
||||
91
out/production/iroon/internal/server/get.go
Normal file
91
out/production/iroon/internal/server/get.go
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Bronku/iroon/internal/models"
|
||||
)
|
||||
|
||||
func (h *Server) orders(r *http.Request) (any, int, error) {
|
||||
//r.URL.Query().Get()
|
||||
orders, err := h.s.GetTopOrders(time.Now(), time.Now().Add(time.Hour*24))
|
||||
data := struct {
|
||||
Today string
|
||||
Orders []models.Order
|
||||
}{Today: time.Now().Format("2006-01-02"), Orders: orders}
|
||||
return data, http.StatusOK, err
|
||||
}
|
||||
|
||||
func (h *Server) ordersSearch(r *http.Request) (any, int, error) {
|
||||
q := r.URL.Query().Get("q")
|
||||
from, err := time.Parse("2006-01-02", r.URL.Query().Get("from"))
|
||||
if err != nil {
|
||||
from = time.Time{}
|
||||
}
|
||||
to, err := time.Parse("2006-01-02", r.URL.Query().Get("to"))
|
||||
if err != nil {
|
||||
to = time.Time{}
|
||||
}
|
||||
fmt.Println(from, to)
|
||||
data, err := h.s.GetFilteredOrder(q, from, to)
|
||||
return data, http.StatusOK, err
|
||||
}
|
||||
|
||||
func (h *Server) cakes(r *http.Request) (any, int, error) {
|
||||
data, err := h.s.GetCakes()
|
||||
return data, http.StatusOK, err
|
||||
}
|
||||
|
||||
func (h *Server) cake(r *http.Request) (any, int, error) {
|
||||
url := strings.Split(r.URL.String(), "/")
|
||||
if len(url) < 3 || url[2] == "" {
|
||||
return models.Cake{}, http.StatusOK, nil
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(url[2])
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
data, err := h.s.GetCake(id)
|
||||
if err != nil {
|
||||
return nil, http.StatusNotFound, err
|
||||
}
|
||||
|
||||
return data, http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (h *Server) order(r *http.Request) (any, int, error) {
|
||||
type formData struct {
|
||||
Order models.Order
|
||||
Catalogue []models.Cake
|
||||
}
|
||||
var err error
|
||||
var data formData
|
||||
|
||||
data.Catalogue, err = h.s.GetCakes()
|
||||
if err != nil {
|
||||
return nil, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
url := strings.Split(r.URL.String(), "/")
|
||||
if len(url) < 3 || url[2] == "" {
|
||||
return data, http.StatusOK, nil
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(url[2])
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
data.Order, err = h.s.GetOrder(id)
|
||||
if err != nil {
|
||||
return nil, http.StatusNotFound, err
|
||||
}
|
||||
|
||||
return data, http.StatusOK, nil
|
||||
}
|
||||
9
out/production/iroon/internal/server/http.go
Normal file
9
out/production/iroon/internal/server/http.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package server
|
||||
|
||||
import "net/http"
|
||||
|
||||
func (s *Server) redirect(path string, code int) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, path, code)
|
||||
}
|
||||
}
|
||||
81
out/production/iroon/internal/server/post.go
Normal file
81
out/production/iroon/internal/server/post.go
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Bronku/iroon/internal/models"
|
||||
)
|
||||
|
||||
func (h *Server) postCake(r *http.Request) (any, int, error) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return nil, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
var n models.Cake
|
||||
n.ID, err = strconv.Atoi(r.FormValue("id"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
n.Name = r.FormValue("name")
|
||||
n.Price, err = strconv.Atoi(r.FormValue("price"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
n.Category = r.FormValue("category")
|
||||
n.Availability = r.FormValue("availibility")
|
||||
n.ID, err = h.s.SaveCake(n)
|
||||
fmt.Println(err)
|
||||
return n, http.StatusAccepted, err
|
||||
}
|
||||
|
||||
func (h *Server) postOrder(r *http.Request) (any, int, error) {
|
||||
cakes, err := h.s.GetCakes()
|
||||
if err != nil {
|
||||
return nil, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
err = r.ParseForm()
|
||||
fmt.Println(r.PostForm)
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
var n models.Order
|
||||
n.ID, err = strconv.Atoi(r.FormValue("id"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
n.Paid, err = strconv.Atoi(r.FormValue("paid"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
n.Date, err = time.Parse("2006-01-02", r.FormValue("date"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
n.Cakes = make([]models.Cake, 0)
|
||||
for _, e := range cakes {
|
||||
e.Amount, err = strconv.Atoi(r.FormValue(fmt.Sprintf("cake[%d]", e.ID)))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
n.Cakes = append(n.Cakes, e)
|
||||
}
|
||||
|
||||
n.Accepted = time.Now()
|
||||
n.Name = strings.TrimSpace(r.FormValue("name"))
|
||||
n.Surname = strings.TrimSpace(r.FormValue("surname"))
|
||||
n.Phone = strings.TrimSpace(r.FormValue("phone"))
|
||||
n.Location = strings.TrimSpace(r.FormValue("location"))
|
||||
n.Status = strings.TrimSpace(r.FormValue("status"))
|
||||
|
||||
n.ID, err = h.s.SaveOrder(n)
|
||||
fmt.Println(n)
|
||||
return n, http.StatusAccepted, err
|
||||
}
|
||||
63
out/production/iroon/internal/server/server.go
Normal file
63
out/production/iroon/internal/server/server.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"github.com/Bronku/iroon/internal/store"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
tmpl map[string]*template.Template
|
||||
s *store.Store
|
||||
routes map[string]route
|
||||
http.Handler
|
||||
}
|
||||
|
||||
type route struct {
|
||||
function fetcher
|
||||
template string
|
||||
templateEntry string
|
||||
}
|
||||
|
||||
func (h *Server) Close() {
|
||||
}
|
||||
|
||||
//go:embed static/*
|
||||
var static embed.FS
|
||||
|
||||
func (h *Server) loadHandler() {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
for i, e := range h.routes {
|
||||
mux.HandleFunc(i, h.render(e.function, e.template, e.templateEntry))
|
||||
}
|
||||
|
||||
mux.HandleFunc("GET /", h.redirect("/orders", http.StatusSeeOther))
|
||||
|
||||
fs := http.FileServerFS(static)
|
||||
mux.Handle("GET /static/", fs)
|
||||
|
||||
h.Handler = mux
|
||||
}
|
||||
|
||||
func New(store *store.Store) *Server {
|
||||
var server Server
|
||||
|
||||
server.routes = map[string]route{
|
||||
"GET /order/": {server.order, "order", "layout"},
|
||||
"GET /orders": {server.orders, "orders", "layout"},
|
||||
"GET /orders/search/": {server.ordersSearch, "orders", "orders-table"},
|
||||
"GET /cake/": {server.cake, "cake", "layout"},
|
||||
"GET /cakes": {server.cakes, "cakes", "layout"},
|
||||
"POST /order/": {server.postOrder, "confirmation", "layout"},
|
||||
"POST /cake/": {server.postCake, "confirmation", "layout"},
|
||||
}
|
||||
|
||||
server.loadTemplates()
|
||||
server.s = store
|
||||
server.loadHandler()
|
||||
|
||||
return &server
|
||||
}
|
||||
1
out/production/iroon/internal/server/static/htmx2.04.js
Normal file
1
out/production/iroon/internal/server/static/htmx2.04.js
Normal file
File diff suppressed because one or more lines are too long
43
out/production/iroon/internal/server/template.go
Normal file
43
out/production/iroon/internal/server/template.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/Bronku/iroon/internal/logging"
|
||||
)
|
||||
|
||||
//go:embed templates/*
|
||||
var templates embed.FS
|
||||
|
||||
func (h *Server) loadTemplates() {
|
||||
h.tmpl = make(map[string]*template.Template)
|
||||
for _, page := range h.routes {
|
||||
tmpl, err := template.ParseFS(templates,
|
||||
"templates/layout/*.html",
|
||||
"templates/"+page.template+".html",
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
h.tmpl[page.template] = tmpl
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) render(fetch fetcher, templateFile string, templateEntry string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
data, code, err := fetch(r)
|
||||
if err != nil {
|
||||
logging.ErrorPage(err, code).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
err = s.tmpl[templateFile].ExecuteTemplate(w, templateEntry, data)
|
||||
if err != nil {
|
||||
logging.ErrorPage(err, http.StatusInternalServerError).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("content-type", "text/html")
|
||||
}
|
||||
}
|
||||
30
out/production/iroon/internal/server/templates/cake.html
Normal file
30
out/production/iroon/internal/server/templates/cake.html
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
{{define "title"}}
|
||||
Cake {{.ID}}
|
||||
{{end}}
|
||||
{{define "main"}}
|
||||
<main>
|
||||
<form method="post">
|
||||
<div>
|
||||
<h2>Info</h2>
|
||||
<input hidden="true" name="id" value="{{.ID}}">
|
||||
<label>name</label>
|
||||
<input type="text" name="name" value="{{.Name}}">
|
||||
<label>price</label>
|
||||
<input type="number" name="price" min="0" value="{{.Price}}">
|
||||
<label>category</label>
|
||||
<select name="category" id="category" >
|
||||
<option {{ if eq .Category "common" }}selected{{ end }} value="common">common</option>
|
||||
<option {{ if eq .Category "christmas" }}selected{{ end }} value="christmas">christmas</option>
|
||||
<option {{ if eq .Category "easter" }}selected{{ end }} value="easter">easter</option>
|
||||
<option {{ if eq .Category "donuts" }}selected{{ end }} value="donuts">donuts</option>
|
||||
</select>
|
||||
<label>availability</label>
|
||||
<select name="availibility" id="availibility" >
|
||||
<option {{ if eq .Availability "available" }}selected{{ end }} value="available">available</option>
|
||||
<option {{ if eq .Availability "unavailable" }}selected{{ end }} value="unavailable">unavailable</option>
|
||||
</select>
|
||||
</div>
|
||||
<button type="submit">submit</button>
|
||||
</form>
|
||||
</main>
|
||||
{{end}}
|
||||
25
out/production/iroon/internal/server/templates/cakes.html
Normal file
25
out/production/iroon/internal/server/templates/cakes.html
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
{{define "title"}}
|
||||
Cakes
|
||||
{{end}}
|
||||
{{define "main"}}
|
||||
<main>
|
||||
<table>
|
||||
<tr>
|
||||
<th></th>
|
||||
<th>Name</th>
|
||||
<th>Price</th>
|
||||
<th>Category</th>
|
||||
<th>Availability</th>
|
||||
</tr>
|
||||
{{range .}}
|
||||
<tr>
|
||||
<th><a href="/cake/{{.ID}}">edit</a></th>
|
||||
<th>{{.Name}}</th>
|
||||
<th>{{.Price}}</th>
|
||||
<th>{{.Category}}</th>
|
||||
<th>{{.Availability}}</th>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
</main>
|
||||
{{end}}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
{{define "title"}}
|
||||
Confirmed
|
||||
{{end}}
|
||||
{{define "main"}}
|
||||
<main>
|
||||
<h2>{{.ID}} confirmed</h2>
|
||||
</main>
|
||||
{{end}}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
{{define "layout"}}
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>{{template "title" .}}</title>
|
||||
<script src="/static/htmx2.04.js" defer></script>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>{{template "title" .}}</h1>
|
||||
</header>
|
||||
<nav>{{template "nav" .}}</nav>
|
||||
{{template "main" .}}
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
{{define "nav"}}
|
||||
<a href="/orders">All orders</a>
|
||||
<a href="/order">New order</a>
|
||||
<a href="/cakes">All Cakes</a>
|
||||
<a href="/cake">New Cake</a>
|
||||
{{end}}
|
||||
90
out/production/iroon/internal/server/templates/order.html
Normal file
90
out/production/iroon/internal/server/templates/order.html
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
{{define "title"}}
|
||||
Order {{.Order.ID}}
|
||||
{{end}}
|
||||
{{define "main"}}
|
||||
<main>
|
||||
<form method="post">
|
||||
{{with .Order}}
|
||||
<div>
|
||||
<h2>Info</h2>
|
||||
<input hidden="true" name="id" value="{{.ID}}">
|
||||
<label>name</label>
|
||||
<input type="text" name="name" value="{{.Name}}">
|
||||
<label>surname</label>
|
||||
<input type="text" name="surname" value="{{.Surname}}">
|
||||
<label>phone</label>
|
||||
<input type="tel" name="phone" value="{{.Phone}}">
|
||||
<label>location</label>
|
||||
<select name="location">
|
||||
<option {{ if eq .Location "Kartuzy" }}selected{{ end }} value="Kartuzy">Kartuzy</option>
|
||||
<option {{ if eq .Location "Somonino" }}selected{{ end }} value="Somonino">Somonino</option>
|
||||
</select>
|
||||
<label>date</label>
|
||||
<input type="date" name="date" value="{{.Date.Format "2006-01-02"}}">
|
||||
<label>status</label>
|
||||
<select name="status" id="status" >
|
||||
<option {{ if eq .Status "accepted" }}selected{{ end }} value="accepted">accepted</option>
|
||||
<option {{ if eq .Status "done" }}selected{{ end }} value="done">done</option>
|
||||
</select>
|
||||
<label>paid</label>
|
||||
<input type="number" name="paid" min="0" value="{{.Paid}}">
|
||||
</div>
|
||||
<div>
|
||||
<h2>Basket</h2>
|
||||
<ul id="basket">
|
||||
</ul>
|
||||
</div>
|
||||
{{end}}
|
||||
{{with .Catalogue}}
|
||||
<div>
|
||||
<h2>Catalogue</h2>
|
||||
<ul>
|
||||
{{range .}}
|
||||
<li>
|
||||
{{.Name}} {{.Price}} <button type=button onClick="addCake({{.ID}},'{{.Name}} {{.Price}}', 1)">add</button>
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
</div>
|
||||
{{end}}
|
||||
<button type="submit">submit</button>
|
||||
</form>
|
||||
</main>
|
||||
<script>
|
||||
function addCake(id, name, amount){
|
||||
var cake = document.getElementById("cake["+id+"]")
|
||||
if (cake != null){
|
||||
var value = Number(cake.getAttribute("value"))
|
||||
cake.setAttribute("value", value+1)
|
||||
return;
|
||||
}
|
||||
var label = document.createElement("label")
|
||||
label.innerText = name
|
||||
var input = document.createElement("input")
|
||||
input.setAttribute("id","cake["+id+"]")
|
||||
input.setAttribute("type", "number")
|
||||
input.setAttribute("min", "0")
|
||||
input.setAttribute("name", "cake["+id+"]")
|
||||
input.setAttribute("value", amount)
|
||||
var button = document.createElement("button")
|
||||
button.setAttribute("type", "button")
|
||||
button.setAttribute("onClick", "removeCake("+id+")")
|
||||
button.innerText= "delete"
|
||||
var li = document.createElement("li")
|
||||
li.appendChild(label)
|
||||
li.appendChild(input)
|
||||
li.appendChild(button)
|
||||
li.setAttribute("id", "li["+id+"]")
|
||||
var ul = document.getElementById("basket")
|
||||
ul.appendChild(li)
|
||||
}
|
||||
|
||||
function removeCake(id){
|
||||
document.getElementById("li["+id+"]").remove()
|
||||
}
|
||||
|
||||
{{range .Order.Cakes}}
|
||||
addCake({{.ID}}, '{{.Name}} {{.Price}}', {{.Amount}})
|
||||
{{end}}
|
||||
</script>
|
||||
{{end}}
|
||||
51
out/production/iroon/internal/server/templates/orders.html
Normal file
51
out/production/iroon/internal/server/templates/orders.html
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
|
||||
{{define "title"}}
|
||||
Orders
|
||||
{{end}}
|
||||
{{define "main"}}
|
||||
<main>
|
||||
<form class="search-container" hx-get="/orders/search" hx-target="#results-table", hx-trigger="keyup delay:500ms, change">
|
||||
<input type="search" name="q" placeholder="Search orders...">
|
||||
|
||||
<label for="from">form</label>
|
||||
<input type="date" value="{{.Today}}" id="from" name="from">
|
||||
<label for="from">to</label>
|
||||
<input type="date" value="{{.Today}}" id="to" name="to">
|
||||
|
||||
<div class="htmx-indicator">
|
||||
Searching...
|
||||
</div>
|
||||
|
||||
<div id="results-table">
|
||||
{{template "orders-table" .Orders}}
|
||||
</div>
|
||||
</form>
|
||||
</main>
|
||||
{{end}}
|
||||
|
||||
{{define "orders-table"}}
|
||||
<table id="orders_table">
|
||||
<tr>
|
||||
<th></th>
|
||||
<th>Name</th>
|
||||
<th>Surname</th>
|
||||
<th>Phone</th>
|
||||
<th>Location</th>
|
||||
<th>Date</th>
|
||||
<th>Status</th>
|
||||
<th>Total</th>
|
||||
</tr>
|
||||
{{range .}}
|
||||
<tr>
|
||||
<th><a href="/order/{{.ID}}">edit</a></th>
|
||||
<th>{{.Name}}</th>
|
||||
<th>{{.Surname}}</th>
|
||||
<th>{{.Phone}}</th>
|
||||
<th>{{.Location}}</th>
|
||||
<th>{{.Date.Format "2006-01-02"}}</th>
|
||||
<th>{{.Status}}</th>
|
||||
<th>{{.Total}}</th>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
{{end}}
|
||||
Loading…
Add table
Add a link
Reference in a new issue