Added new pages
This commit is contained in:
@@ -0,0 +1,49 @@
|
|||||||
|
<div>
|
||||||
|
<form id="category-form" action="submit">
|
||||||
|
<fieldset id="form-box-boundaries">
|
||||||
|
<legend>CATEGORY FORM</legend>
|
||||||
|
<div class="form-container form-left">
|
||||||
|
<div id="form-state" class="warning"></div><br>
|
||||||
|
|
||||||
|
<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>
|
||||||
|
|
||||||
|
<button
|
||||||
|
class="subbutton"
|
||||||
|
hx-post="/categories/new"
|
||||||
|
hx-swap="none"
|
||||||
|
type="submit"
|
||||||
|
id="category-submit-button">CREATE</button>
|
||||||
|
<br>
|
||||||
|
</div>
|
||||||
|
</fieldset>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
document.body.addEventListener("wrongCredentials", function(evt) {
|
||||||
|
var messageDiv = document.getElementById("login-state")
|
||||||
|
messageDiv.innerHTML = evt.detail.value
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
messageDiv.innerHTML = ""
|
||||||
|
}, 3000);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add the spinner
|
||||||
|
var btn = document.getElementById("login-submit-button");
|
||||||
|
btn.addEventListener('htmx:beforeRequest', function (evt) {
|
||||||
|
btn.classList.add("loading");
|
||||||
|
spinner(btn)
|
||||||
|
});
|
||||||
|
|
||||||
|
btn.addEventListener('htmx:afterRequest', function (evt) {
|
||||||
|
btn.classList.remove("loading");
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<td>ID:</td>
|
||||||
|
<td>{{ .expense.ExpID }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Description:</td>
|
||||||
|
<td>{{ .expense.Description }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Date:</td>
|
||||||
|
<td>{{ formatDate .expense.ExpDate }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Category:</td>
|
||||||
|
<td>
|
||||||
|
<select name="newexp-cat-dropdown" id="newexp-cat-dropdown">
|
||||||
|
{{ range .categories }}
|
||||||
|
<option value="{{ .CategoryID }}">{{ .CategoryName }}</option>
|
||||||
|
{{ end}}
|
||||||
|
</select>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
|
||||||
@@ -4,6 +4,9 @@
|
|||||||
<th>Desc</th>
|
<th>Desc</th>
|
||||||
<th>Price</th>
|
<th>Price</th>
|
||||||
<th>Date</th>
|
<th>Date</th>
|
||||||
|
<th>Store</th>
|
||||||
|
<th>Category</th>
|
||||||
|
<th>Owner</th>
|
||||||
</tr>
|
</tr>
|
||||||
{{ range .expenses }}
|
{{ range .expenses }}
|
||||||
<tr>
|
<tr>
|
||||||
@@ -11,6 +14,9 @@
|
|||||||
<th spellcheck="false" contenteditable>{{ .Description }}</th>
|
<th spellcheck="false" contenteditable>{{ .Description }}</th>
|
||||||
<th spellcheck="false" contenteditable>{{ formatPrice .Value }}</th>
|
<th spellcheck="false" contenteditable>{{ formatPrice .Value }}</th>
|
||||||
<th spellcheck="false" contenteditable>{{ formatDate .ExpDate }}</th>
|
<th spellcheck="false" contenteditable>{{ formatDate .ExpDate }}</th>
|
||||||
|
<th spellcheck="false">{{ .ExpStore.StoreName }}</th>
|
||||||
|
<th spellcheck="false">{{ .ExpCategory.CategoryName }}</th>
|
||||||
|
<th spellcheck="false">{{ .OwnerUser.UserName }}</th>
|
||||||
</tr>
|
</tr>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
id="home-button"
|
id="home-button"
|
||||||
style="cursor: pointer;"
|
style="cursor: pointer;"
|
||||||
class="neon-text {{ if .warning }}warning{{ end }}"
|
class="neon-text {{ if .warning }}warning{{ end }}"
|
||||||
hx-get="/aboutMe"
|
hx-get="/"
|
||||||
hx-target="#container"
|
hx-target="#container"
|
||||||
hx-replace-url="true">EXPENSES</legend>
|
hx-replace-url="true">EXPENSES</legend>
|
||||||
<div id="main-container-content" class="main-container-content">
|
<div id="main-container-content" class="main-container-content">
|
||||||
|
|||||||
@@ -12,6 +12,13 @@ type Category struct {
|
|||||||
CategoryName string
|
CategoryName string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewCategory() Category {
|
||||||
|
return Category{
|
||||||
|
CategoryID: -1,
|
||||||
|
CategoryName: "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func GetAllCategories() ([]Category, error) {
|
func GetAllCategories() ([]Category, error) {
|
||||||
db, err := sql.Open("sqlite3", "./data/data.db")
|
db, err := sql.Open("sqlite3", "./data/data.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+49
-13
@@ -12,13 +12,26 @@ type Expense struct {
|
|||||||
ExpID int
|
ExpID int
|
||||||
Description string
|
Description string
|
||||||
Value float32
|
Value float32
|
||||||
StoreID int
|
ExpStore Store
|
||||||
CategoryID int
|
ExpCategory Category
|
||||||
OwnerUserID int
|
OwnerUser User
|
||||||
ExpDate int
|
ExpDate int
|
||||||
CreationDate int
|
CreationDate int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewExpense() Expense {
|
||||||
|
return Expense{
|
||||||
|
ExpID: -1,
|
||||||
|
Description: "",
|
||||||
|
Value: 0.0,
|
||||||
|
ExpStore: NewStore(),
|
||||||
|
ExpCategory: NewCategory(),
|
||||||
|
OwnerUser: NewUser(),
|
||||||
|
ExpDate: 0,
|
||||||
|
CreationDate: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func GetAllExpenses() ([]Expense, error) {
|
func GetAllExpenses() ([]Expense, error) {
|
||||||
db, err := sql.Open("sqlite3", "./data/data.db")
|
db, err := sql.Open("sqlite3", "./data/data.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -26,9 +39,14 @@ func GetAllExpenses() ([]Expense, error) {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
query := "SELECT ExpID,Description,Value,StoreID,CategoryID," +
|
query := "SELECT ExpID,Description,Value," +
|
||||||
"OwnerUserID,ExpDate,CreationDate " +
|
"Stores.StoreID,Stores.StoreName," +
|
||||||
"FROM expenses"
|
"Categories.CategoryID,Categories.CategoryName," +
|
||||||
|
"Users.UserID,Users.UserName," +
|
||||||
|
"ExpDate,CreationDate " +
|
||||||
|
"FROM expenses " +
|
||||||
|
"JOIN Stores, Categories " +
|
||||||
|
"JOIN Users ON UserID = OwnerUserId"
|
||||||
|
|
||||||
var expList []Expense
|
var expList []Expense
|
||||||
rows, err := db.Query(query)
|
rows, err := db.Query(query)
|
||||||
@@ -38,8 +56,12 @@ func GetAllExpenses() ([]Expense, error) {
|
|||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
exp := &Expense{}
|
exp := &Expense{}
|
||||||
err := rows.Scan(&exp.ExpID, &exp.Description, &exp.Value, &exp.StoreID,
|
err := rows.Scan(
|
||||||
&exp.CategoryID, &exp.OwnerUserID, &exp.ExpDate, &exp.CreationDate,
|
&exp.ExpID, &exp.Description, &exp.Value,
|
||||||
|
&exp.ExpStore.StoreID, &exp.ExpStore.StoreName,
|
||||||
|
&exp.ExpCategory.CategoryID, &exp.ExpCategory.CategoryName,
|
||||||
|
&exp.OwnerUser.UserID, &exp.OwnerUser.UserName,
|
||||||
|
&exp.ExpDate, &exp.CreationDate,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to parse data from db: %v", err)
|
log.Fatalf("Failed to parse data from db: %v", err)
|
||||||
@@ -57,16 +79,30 @@ func GetExpense(expID int) (Expense, error) {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
query := "SELECT ExpID,Description,Value,StoreID,CategoryID," +
|
query := "SELECT ExpID,Description,Value," +
|
||||||
"OwnerUserID,ExpDate,CreationDate " +
|
"Stores.StoreID,Stores.StoreName," +
|
||||||
|
"Categories.CategoryID,Categories.CategoryName," +
|
||||||
|
"Users.UserID,Users.UserName," +
|
||||||
|
"ExpDate,CreationDate " +
|
||||||
"FROM expenses " +
|
"FROM expenses " +
|
||||||
|
"JOIN Stores, Categories " +
|
||||||
|
"JOIN Users ON UserID = OwnerUserId " +
|
||||||
"WHERE ExpID = " + strconv.Itoa(expID)
|
"WHERE ExpID = " + strconv.Itoa(expID)
|
||||||
|
|
||||||
var exp Expense
|
log.Println(query)
|
||||||
|
|
||||||
|
exp := Expense{ExpID: -1}
|
||||||
err = db.QueryRow(query).Scan(
|
err = db.QueryRow(query).Scan(
|
||||||
&exp.ExpID, &exp.Description, &exp.Value, &exp.StoreID,
|
&exp.ExpID, &exp.Description, &exp.Value,
|
||||||
&exp.CategoryID, &exp.OwnerUserID, &exp.ExpDate, &exp.CreationDate,
|
&exp.ExpStore.StoreID, &exp.ExpStore.StoreName,
|
||||||
|
&exp.ExpCategory.CategoryID, &exp.ExpCategory.CategoryName,
|
||||||
|
&exp.OwnerUser.UserID, &exp.OwnerUser.UserName,
|
||||||
|
&exp.ExpDate, &exp.CreationDate,
|
||||||
)
|
)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return Expense{ExpID: -1}, nil
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Expense{}, nil
|
return Expense{}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
package expenses
|
package expenses
|
||||||
|
|
||||||
type Stores struct {
|
type Store struct {
|
||||||
StoreID int
|
StoreID int
|
||||||
StoreName string
|
StoreName string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewStore() Store {
|
||||||
|
return Store{
|
||||||
|
StoreID: -1,
|
||||||
|
StoreName: "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
package expenses
|
package expenses
|
||||||
|
|
||||||
type Users struct {
|
type User struct {
|
||||||
UserID int
|
UserID int
|
||||||
UserName string
|
UserName string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewUser() User {
|
||||||
|
return User{
|
||||||
|
UserID: -1,
|
||||||
|
UserName: "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+20
-1
@@ -77,9 +77,28 @@ func categoryPage(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newCategoryPage(c *gin.Context) {
|
||||||
|
cfg := config.GetInstance()
|
||||||
|
|
||||||
|
content := templating.HtmlTemplate(
|
||||||
|
fp.Join(cfg.AssetsDir, "/htmx/categoryNew.html"), map[string]any{},
|
||||||
|
)
|
||||||
|
|
||||||
|
c.HTML(http.StatusOK, "terminal.gotempl", gin.H{
|
||||||
|
"page": "expenseNew",
|
||||||
|
"renderNavBar": false,
|
||||||
|
"content": content,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func createCategory(c *gin.Context) {
|
||||||
|
}
|
||||||
|
|
||||||
func RouteCategories(router *gin.Engine) {
|
func RouteCategories(router *gin.Engine) {
|
||||||
router.GET(CategoriesPath, categoriesGlobalPage)
|
router.GET(CategoriesPath, categoriesGlobalPage)
|
||||||
|
router.GET(CategoriesPath+"/:id", categoryPage)
|
||||||
router.POST(CategoriesPath, categoriesGlobalPage)
|
router.POST(CategoriesPath, categoriesGlobalPage)
|
||||||
|
|
||||||
router.GET(CategoriesPath+"/:id", categoryPage)
|
router.GET(CategoriesPath+"/new", newCategoryPage)
|
||||||
|
router.POST(CategoriesPath+"/new", createCategory)
|
||||||
}
|
}
|
||||||
|
|||||||
+32
-2
@@ -4,6 +4,7 @@ import (
|
|||||||
"expenses/config"
|
"expenses/config"
|
||||||
"expenses/expenses"
|
"expenses/expenses"
|
||||||
"expenses/templating"
|
"expenses/templating"
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -51,7 +52,7 @@ func ExpensesGlobalPage(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExpensePage(c *gin.Context) {
|
func expensePage(c *gin.Context) {
|
||||||
cfg := config.GetInstance()
|
cfg := config.GetInstance()
|
||||||
|
|
||||||
expenseID, err := strconv.Atoi(c.Param("id"))
|
expenseID, err := strconv.Atoi(c.Param("id"))
|
||||||
@@ -67,6 +68,11 @@ func ExpensePage(c *gin.Context) {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if expense.ExpID == -1 {
|
||||||
|
NotFoundView(c, fmt.Sprintf("ID `%d` doesn't exist", expenseID))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.HTML(http.StatusOK, "terminal.gotempl", gin.H{
|
c.HTML(http.StatusOK, "terminal.gotempl", gin.H{
|
||||||
"page": "expense",
|
"page": "expense",
|
||||||
"renderNavBar": false,
|
"renderNavBar": false,
|
||||||
@@ -74,9 +80,33 @@ func ExpensePage(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newExpensePage(c *gin.Context) {
|
||||||
|
cfg := config.GetInstance()
|
||||||
|
|
||||||
|
categories, err := expenses.GetAllCategories()
|
||||||
|
if err != nil {
|
||||||
|
ServerErrorView(c, "failed to fetch categories")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content := templating.HtmlTemplate(
|
||||||
|
fp.Join(cfg.AssetsDir, "/htmx/expenseNew.html"), map[string]any{
|
||||||
|
"expense": expenses.NewExpense(),
|
||||||
|
"categories": categories,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
c.HTML(http.StatusOK, "terminal.gotempl", gin.H{
|
||||||
|
"page": "expenseNew",
|
||||||
|
"renderNavBar": false,
|
||||||
|
"content": content,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func RouteExpenses(router *gin.Engine) {
|
func RouteExpenses(router *gin.Engine) {
|
||||||
router.GET(ExpensesPath, ExpensesGlobalPage)
|
router.GET(ExpensesPath, ExpensesGlobalPage)
|
||||||
|
router.GET(ExpensesPath+"/:id", expensePage)
|
||||||
router.POST(ExpensesPath, ExpensesGlobalPage)
|
router.POST(ExpensesPath, ExpensesGlobalPage)
|
||||||
|
|
||||||
router.GET(ExpensesPath+"/:id", ExpensePage)
|
router.GET(ExpensesPath+"/new", newExpensePage)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user