Page to add new categories and delete
This commit is contained in:
@@ -1,12 +1,52 @@
|
||||
<div id="operation-state" class="warning"></div><br>
|
||||
<table>
|
||||
<tr>
|
||||
<th></th>
|
||||
<th>ID</th>
|
||||
<th>Name</th>
|
||||
</tr>
|
||||
{{ range .categories }}
|
||||
<tr>
|
||||
<th><a href="/categories/{{ .CategoryID }}">{{ .CategoryID }}</th>
|
||||
<th>
|
||||
<a class="category-delete-button"
|
||||
hx-delete="/categories/{{ .CategoryID }}"
|
||||
hx-on::after-request="handleDeleteResponse(event)"
|
||||
> X </a>
|
||||
</th>
|
||||
<th><a href="/categories/{{ .CategoryID }}">{{ .CategoryID }}</a></th>
|
||||
<th spellcheck="false" contenteditable>{{ .CategoryName }}</th>
|
||||
</tr>
|
||||
{{ end }}
|
||||
</table>
|
||||
|
||||
<script>
|
||||
function handleDeleteResponse(evt) {
|
||||
const status = evt.detail.xhr.status;
|
||||
const messageDiv = document.getElementById("operation-state")
|
||||
|
||||
if (status >= 400) {
|
||||
messageDiv.textContent = evt.detail.xhr.responseText || "Delete failed";
|
||||
|
||||
setTimeout(() => {
|
||||
messageDiv.textContent = "";
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
if (status == 204) {
|
||||
const tr = evt.target.closest('tr');
|
||||
tr.remove();
|
||||
}
|
||||
}
|
||||
|
||||
document.querySelectorAll(".category-delete-button").forEach(btn => {
|
||||
// Add the spinner
|
||||
btn.addEventListener('htmx:beforeRequest', function () {
|
||||
btn.classList.add("loading");
|
||||
})
|
||||
|
||||
// Remove the spinner
|
||||
btn.addEventListener('htmx:afterRequest', function (evt) {
|
||||
btn.classList.remove("loading");
|
||||
});
|
||||
})
|
||||
</script>
|
||||
|
||||
@@ -8,10 +8,10 @@
|
||||
<label class="fullwidth-label" for="category-name">NAME</label>
|
||||
<input class="tui-input underline fullwidth" type="text" name="category-name" id="category-name"><br><br>
|
||||
|
||||
<label for="category-description">DESCRIPTION</label>
|
||||
<input
|
||||
class="tui-input underline fullwidth" type="text" name="category-description"
|
||||
id="category-description"><br><br>
|
||||
<!-- <label for="category-description">DESCRIPTION</label> -->
|
||||
<!-- <input -->
|
||||
<!-- class="tui-input underline fullwidth" type="text" name="category-description" -->
|
||||
<!-- id="category-description"><br><br> -->
|
||||
|
||||
<button
|
||||
class="subbutton"
|
||||
@@ -26,8 +26,8 @@
|
||||
</div>
|
||||
|
||||
<script>
|
||||
document.body.addEventListener("wrongCredentials", function(evt) {
|
||||
var messageDiv = document.getElementById("login-state")
|
||||
document.body.addEventListener("formState", function(evt) {
|
||||
var messageDiv = document.getElementById("form-state")
|
||||
messageDiv.innerHTML = evt.detail.value
|
||||
|
||||
setTimeout(() => {
|
||||
@@ -36,7 +36,7 @@
|
||||
});
|
||||
|
||||
// Add the spinner
|
||||
var btn = document.getElementById("login-submit-button");
|
||||
var btn = document.getElementById("category-submit-button");
|
||||
btn.addEventListener('htmx:beforeRequest', function (evt) {
|
||||
btn.classList.add("loading");
|
||||
spinner(btn)
|
||||
|
||||
+7
-7
@@ -9,9 +9,9 @@ CREATE TABLE expenses (
|
||||
ExpTypeID int NOT NULL DEFAULT 0,
|
||||
ExpDate int NOT NULL DEFAULT -1,
|
||||
CreationDate int NOT NULL DEFAULT -1,
|
||||
FOREIGN KEY(StoreID) REFERENCES stores(StoreID),
|
||||
FOREIGN KEY(CategoryID) REFERENCES categories(CategoryID),
|
||||
FOREIGN KEY(OwnerUserID) REFERENCES users(UserID)
|
||||
FOREIGN KEY(StoreID) REFERENCES stores(StoreID) ON DELETE RESTRICT,
|
||||
FOREIGN KEY(CategoryID) REFERENCES categories(CategoryID) ON DELETE RESTRICT,
|
||||
FOREIGN KEY(OwnerUserID) REFERENCES users(UserID) ON DELETE RESTRICT
|
||||
);
|
||||
|
||||
DROP TABLE IF EXISTS stores;
|
||||
@@ -51,8 +51,8 @@ CREATE TABLE expensesShares(
|
||||
ExpID int NOT NULL,
|
||||
UserID int NOT NULL,
|
||||
Share float NOT NULL DEFAULT 0.5,
|
||||
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID),
|
||||
FOREIGN KEY(UserID) REFERENCES users(UserID)
|
||||
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT,
|
||||
FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT
|
||||
);
|
||||
|
||||
DROP TABLE IF EXISTS expensesPayments;
|
||||
@@ -61,6 +61,6 @@ CREATE TABLE expensesPayments(
|
||||
ExpID int NOT NULL,
|
||||
UserID int NOT NULL,
|
||||
Payed float NOT NULL DEFAULT 0,
|
||||
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID),
|
||||
FOREIGN KEY(UserID) REFERENCES users(UserID)
|
||||
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT,
|
||||
FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT
|
||||
);
|
||||
|
||||
@@ -2,9 +2,15 @@ package expenses
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = fmt.Errorf("not found")
|
||||
)
|
||||
|
||||
type Category struct {
|
||||
@@ -66,3 +72,54 @@ func GetCategory(catID int) (Category, error) {
|
||||
|
||||
return cat, nil
|
||||
}
|
||||
|
||||
func (cat *Category) Insert() error {
|
||||
db, err := sql.Open("sqlite3", "./data/data.db")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
query := "INSERT INTO categories(CategoryName) VALUES(?)"
|
||||
res, err := db.Exec(query, cat.CategoryName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rowsAffected, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
} else if rowsAffected == 0 {
|
||||
return fmt.Errorf("no rows were created")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cat *Category) Delete() error {
|
||||
db, err := sql.Open("sqlite3", "./data/data.db")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
_, err = db.Exec("PRAGMA foreign_keys = ON")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
query := "DELETE FROM categories WHERE CategoryID = ?"
|
||||
res, err := db.Exec(query, cat.CategoryID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rowsAffected, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
} else if rowsAffected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
+42
-2
@@ -4,6 +4,7 @@ import (
|
||||
"expenses/config"
|
||||
"expenses/expenses"
|
||||
"expenses/templating"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"net/http"
|
||||
@@ -18,13 +19,11 @@ const (
|
||||
)
|
||||
|
||||
func categoriesContent() (map[string]any, error) {
|
||||
log.Println("here1")
|
||||
categories, err := expenses.GetAllCategories()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Println("here2")
|
||||
data := map[string]any{
|
||||
"size": len(categories) - 1,
|
||||
"categories": categories,
|
||||
@@ -92,6 +91,46 @@ func newCategoryPage(c *gin.Context) {
|
||||
}
|
||||
|
||||
func createCategory(c *gin.Context) {
|
||||
newCatName := c.PostForm("category-name")
|
||||
|
||||
newCat := expenses.Category{
|
||||
CategoryName: newCatName,
|
||||
}
|
||||
|
||||
err := newCat.Insert()
|
||||
if err != nil {
|
||||
c.Header("HX-Trigger", fmt.Sprintf("{\"formState\":\"%s\"}", err.Error()))
|
||||
}
|
||||
|
||||
c.Header("HX-Trigger", "{\"formState\":\"Success\"}")
|
||||
}
|
||||
|
||||
func deleteCategory(c *gin.Context) {
|
||||
categoryID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.Redirect(404, "/404")
|
||||
}
|
||||
|
||||
log.Println("Delete requested for category: ", categoryID)
|
||||
|
||||
category := expenses.Category{
|
||||
CategoryID: categoryID,
|
||||
}
|
||||
log.Println("here1")
|
||||
err = category.Delete()
|
||||
if err == expenses.ErrNotFound {
|
||||
errMsg := fmt.Sprintf("category %d not found", categoryID)
|
||||
c.String(http.StatusNotFound, errMsg)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
errMsg := fmt.Sprintf("error deleting category %d", categoryID)
|
||||
c.String(http.StatusInternalServerError, errMsg)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func RouteCategories(router *gin.Engine) {
|
||||
@@ -101,4 +140,5 @@ func RouteCategories(router *gin.Engine) {
|
||||
|
||||
router.GET(CategoriesPath+"/new", newCategoryPage)
|
||||
router.POST(CategoriesPath+"/new", createCategory)
|
||||
router.DELETE(CategoriesPath+"/:id", deleteCategory)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user