Moving the behaviour to types too

This commit is contained in:
2025-07-28 22:59:34 +01:00
parent 5261ba3ea3
commit ef21189d6c
6 changed files with 52 additions and 57 deletions
+7 -1
View File
@@ -5,7 +5,13 @@
<legend>CATEGORY FORM</legend>
<div id="form-state" class="warning"></div><br>
<div class="form-container form-left">
<div class="form-container form-inline">
{{ if eq .method "put" }}
<label for="category-id">ID:</label>
<input class="tui-input underline" type="text" readonly
name="category-id" id="category-id" value="{{ .category.CategoryID }}">
{{ end }}
<label for="category-name">NAME:</label>
<input class="tui-input underline" type="text" name="category-name"
id="category-name" value="{{ .category.CategoryName }}">
+2 -4
View File
@@ -61,8 +61,6 @@
{{ template "categories" . }}
{{ else if eq .content "category" }}
{{ template "category" . }}
{{ else if eq .content "category" }}
{{ template "category" . }}
{{ else if eq .content "overview" }}
{{ template "overview" . }}
{{ else if eq .content "stores" }}
@@ -73,8 +71,8 @@
{{ template "storeNew" . }}
{{ else if eq .content "types" }}
{{ template "types" . }}
{{ else if eq .content "typeNew" }}
{{ template "typeNew" . }}
{{ else if eq .content "type" }}
{{ template "type" . }}
{{ else }}
<div>Unknown page</div>
{{ end }}
+31 -23
View File
@@ -6,32 +6,40 @@
<div class="form-container form-inline">
<div id="form-state" class="warning"></div><br>
<p>
<label for="type-id">ID:</label>
<input class="tui-input underline fullwidth"
type="text" readonly
name="type-id" id="type-id" value="{{ .type.TypeID }}">
</p>
<br>
{{ if eq .method "put" }}
<label for="type-id">ID:</label>
<input class="tui-input underline" type="text" readonly
name="type-id" id="type-id" value="{{ .type.TypeID }}">
{{ end }}
<label class="fullwidth-label" for="type-name">NAME:</label>
<input class="tui-input underline" type="text" name="type-name"
id="type-name" value="{{ .type.TypeName }}">
<p>
<label for="type-name">NAME</label>
<input class="tui-input underline fullwidth"
type="text"
name="type-name" id="type-name" value="{{ .type.TypeName }}">
</p>
<br>
<!-- TODO -->
<!-- The after attribute of this button is written on a newline for no reason -->
<button
class="subbutton"
hx-put="/types/{{ .type.TypeID }}"
hx-swap="none"
type="submit"
id="type-submit-button">UPDATE</button><br>
</div>
</fieldset>
<br>
<button
class="subbutton"
{{ if eq .method "put" }}
hx-put="/types/{{ .type.TypeID }}"
{{ end }}
{{ if eq .method "post" }}
hx-post="/types/new"
{{ end }}
hx-swap="none"
type="submit">
{{- if eq .method "put" }}UPDATE{{- end }}
{{- if eq .method "post" }}CREATE{{- end }}</button>
{{ if eq .method "put" }}
<button
onclick="confirmDelete(this)"
type="button"
class="delete-button"
data-delete-url="/types/{{ .type.TypeID }}">DELETE</button>
{{ end }}
</form>
</div>
{{ template "deleteModal" }}
{{ end }}
-23
View File
@@ -1,23 +0,0 @@
{{ define "typeNew" }}
<div>
<form id="type-form" action="submit">
<fieldset id="form-box-boundaries">
<legend>TYPE FORM</legend>
<div class="form-container form-left">
<div id="form-state" class="warning"></div><br>
<label class="fullwidth-label" for="type-name">NAME</label>
<input class="tui-input underline fullwidth" type="text" name="type-name" id="type-name"><br><br>
<button
class="subbutton"
hx-post="/types/new"
hx-swap="none"
type="submit"
id="type-submit-button">CREATE</button>
<br>
</div>
</fieldset>
</form>
</div>
{{ end }}
+4 -5
View File
@@ -4,7 +4,6 @@ import (
"database/sql"
"fmt"
"log"
"strconv"
_ "github.com/mattn/go-sqlite3"
)
@@ -58,12 +57,12 @@ func GetType(typID int) (Type, error) {
query := "SELECT TypeID,TypeName " +
"FROM expenseTypes " +
"WHERE TypeID = " + strconv.Itoa(typID)
"WHERE TypeID = ?"
var nType Type
err = db.QueryRow(query).Scan(&nType.TypeID, &nType.TypeName)
nType := Type{TypeID: -1}
err = db.QueryRow(query, typID).Scan(&nType.TypeID, &nType.TypeName)
if err != nil {
return Type{}, nil
return Type{TypeID: -1}, nil
}
return nType, nil
+8 -1
View File
@@ -39,6 +39,11 @@ func typePage(c *gin.Context) {
typ, err := expenses.GetType(typeID)
if err != nil {
ServerErrorView(c, "failed to retrieve requested type")
return
}
if typ.TypeID == -1 {
NotFoundView(c, fmt.Sprintf("Couldn't find type `%d`", typeID))
return
}
c.HTML(http.StatusOK, "terminal", gin.H{
@@ -46,6 +51,7 @@ func typePage(c *gin.Context) {
"renderNavBar": false,
"content": "type",
"type": typ,
"method": "put",
})
}
@@ -53,7 +59,8 @@ func newTypePage(c *gin.Context) {
c.HTML(http.StatusOK, "terminal", gin.H{
"page": "typeNew",
"renderNavBar": false,
"content": "typeNew",
"content": "type",
"method": "post",
})
}