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>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
|
<th></th>
|
||||||
<th>ID</th>
|
<th>ID</th>
|
||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
</tr>
|
</tr>
|
||||||
{{ range .categories }}
|
{{ range .categories }}
|
||||||
<tr>
|
<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>
|
<th spellcheck="false" contenteditable>{{ .CategoryName }}</th>
|
||||||
</tr>
|
</tr>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</table>
|
</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>
|
<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>
|
<input class="tui-input underline fullwidth" type="text" name="category-name" id="category-name"><br><br>
|
||||||
|
|
||||||
<label for="category-description">DESCRIPTION</label>
|
<!-- <label for="category-description">DESCRIPTION</label> -->
|
||||||
<input
|
<!-- <input -->
|
||||||
class="tui-input underline fullwidth" type="text" name="category-description"
|
<!-- class="tui-input underline fullwidth" type="text" name="category-description" -->
|
||||||
id="category-description"><br><br>
|
<!-- id="category-description"><br><br> -->
|
||||||
|
|
||||||
<button
|
<button
|
||||||
class="subbutton"
|
class="subbutton"
|
||||||
@@ -26,8 +26,8 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
document.body.addEventListener("wrongCredentials", function(evt) {
|
document.body.addEventListener("formState", function(evt) {
|
||||||
var messageDiv = document.getElementById("login-state")
|
var messageDiv = document.getElementById("form-state")
|
||||||
messageDiv.innerHTML = evt.detail.value
|
messageDiv.innerHTML = evt.detail.value
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@@ -36,7 +36,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Add the spinner
|
// Add the spinner
|
||||||
var btn = document.getElementById("login-submit-button");
|
var btn = document.getElementById("category-submit-button");
|
||||||
btn.addEventListener('htmx:beforeRequest', function (evt) {
|
btn.addEventListener('htmx:beforeRequest', function (evt) {
|
||||||
btn.classList.add("loading");
|
btn.classList.add("loading");
|
||||||
spinner(btn)
|
spinner(btn)
|
||||||
|
|||||||
+7
-7
@@ -9,9 +9,9 @@ CREATE TABLE expenses (
|
|||||||
ExpTypeID int NOT NULL DEFAULT 0,
|
ExpTypeID int NOT NULL DEFAULT 0,
|
||||||
ExpDate int NOT NULL DEFAULT -1,
|
ExpDate int NOT NULL DEFAULT -1,
|
||||||
CreationDate int NOT NULL DEFAULT -1,
|
CreationDate int NOT NULL DEFAULT -1,
|
||||||
FOREIGN KEY(StoreID) REFERENCES stores(StoreID),
|
FOREIGN KEY(StoreID) REFERENCES stores(StoreID) ON DELETE RESTRICT,
|
||||||
FOREIGN KEY(CategoryID) REFERENCES categories(CategoryID),
|
FOREIGN KEY(CategoryID) REFERENCES categories(CategoryID) ON DELETE RESTRICT,
|
||||||
FOREIGN KEY(OwnerUserID) REFERENCES users(UserID)
|
FOREIGN KEY(OwnerUserID) REFERENCES users(UserID) ON DELETE RESTRICT
|
||||||
);
|
);
|
||||||
|
|
||||||
DROP TABLE IF EXISTS stores;
|
DROP TABLE IF EXISTS stores;
|
||||||
@@ -51,8 +51,8 @@ CREATE TABLE expensesShares(
|
|||||||
ExpID int NOT NULL,
|
ExpID int NOT NULL,
|
||||||
UserID int NOT NULL,
|
UserID int NOT NULL,
|
||||||
Share float NOT NULL DEFAULT 0.5,
|
Share float NOT NULL DEFAULT 0.5,
|
||||||
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID),
|
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT,
|
||||||
FOREIGN KEY(UserID) REFERENCES users(UserID)
|
FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT
|
||||||
);
|
);
|
||||||
|
|
||||||
DROP TABLE IF EXISTS expensesPayments;
|
DROP TABLE IF EXISTS expensesPayments;
|
||||||
@@ -61,6 +61,6 @@ CREATE TABLE expensesPayments(
|
|||||||
ExpID int NOT NULL,
|
ExpID int NOT NULL,
|
||||||
UserID int NOT NULL,
|
UserID int NOT NULL,
|
||||||
Payed float NOT NULL DEFAULT 0,
|
Payed float NOT NULL DEFAULT 0,
|
||||||
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID),
|
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT,
|
||||||
FOREIGN KEY(UserID) REFERENCES users(UserID)
|
FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -2,9 +2,15 @@ package expenses
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNotFound = fmt.Errorf("not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Category struct {
|
type Category struct {
|
||||||
@@ -66,3 +72,54 @@ func GetCategory(catID int) (Category, error) {
|
|||||||
|
|
||||||
return cat, nil
|
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/config"
|
||||||
"expenses/expenses"
|
"expenses/expenses"
|
||||||
"expenses/templating"
|
"expenses/templating"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -18,13 +19,11 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func categoriesContent() (map[string]any, error) {
|
func categoriesContent() (map[string]any, error) {
|
||||||
log.Println("here1")
|
|
||||||
categories, err := expenses.GetAllCategories()
|
categories, err := expenses.GetAllCategories()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("here2")
|
|
||||||
data := map[string]any{
|
data := map[string]any{
|
||||||
"size": len(categories) - 1,
|
"size": len(categories) - 1,
|
||||||
"categories": categories,
|
"categories": categories,
|
||||||
@@ -92,6 +91,46 @@ func newCategoryPage(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createCategory(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) {
|
func RouteCategories(router *gin.Engine) {
|
||||||
@@ -101,4 +140,5 @@ func RouteCategories(router *gin.Engine) {
|
|||||||
|
|
||||||
router.GET(CategoriesPath+"/new", newCategoryPage)
|
router.GET(CategoriesPath+"/new", newCategoryPage)
|
||||||
router.POST(CategoriesPath+"/new", createCategory)
|
router.POST(CategoriesPath+"/new", createCategory)
|
||||||
|
router.DELETE(CategoriesPath+"/:id", deleteCategory)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user