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>Price</th>
|
||||
<th>Date</th>
|
||||
<th>Store</th>
|
||||
<th>Category</th>
|
||||
<th>Owner</th>
|
||||
</tr>
|
||||
{{ range .expenses }}
|
||||
<tr>
|
||||
@@ -11,6 +14,9 @@
|
||||
<th spellcheck="false" contenteditable>{{ .Description }}</th>
|
||||
<th spellcheck="false" contenteditable>{{ formatPrice .Value }}</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>
|
||||
{{ end }}
|
||||
</table>
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
id="home-button"
|
||||
style="cursor: pointer;"
|
||||
class="neon-text {{ if .warning }}warning{{ end }}"
|
||||
hx-get="/aboutMe"
|
||||
hx-get="/"
|
||||
hx-target="#container"
|
||||
hx-replace-url="true">EXPENSES</legend>
|
||||
<div id="main-container-content" class="main-container-content">
|
||||
|
||||
@@ -12,6 +12,13 @@ type Category struct {
|
||||
CategoryName string
|
||||
}
|
||||
|
||||
func NewCategory() Category {
|
||||
return Category{
|
||||
CategoryID: -1,
|
||||
CategoryName: "",
|
||||
}
|
||||
}
|
||||
|
||||
func GetAllCategories() ([]Category, error) {
|
||||
db, err := sql.Open("sqlite3", "./data/data.db")
|
||||
if err != nil {
|
||||
|
||||
+49
-13
@@ -12,13 +12,26 @@ type Expense struct {
|
||||
ExpID int
|
||||
Description string
|
||||
Value float32
|
||||
StoreID int
|
||||
CategoryID int
|
||||
OwnerUserID int
|
||||
ExpStore Store
|
||||
ExpCategory Category
|
||||
OwnerUser User
|
||||
ExpDate 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) {
|
||||
db, err := sql.Open("sqlite3", "./data/data.db")
|
||||
if err != nil {
|
||||
@@ -26,9 +39,14 @@ func GetAllExpenses() ([]Expense, error) {
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
query := "SELECT ExpID,Description,Value,StoreID,CategoryID," +
|
||||
"OwnerUserID,ExpDate,CreationDate " +
|
||||
"FROM expenses"
|
||||
query := "SELECT ExpID,Description,Value," +
|
||||
"Stores.StoreID,Stores.StoreName," +
|
||||
"Categories.CategoryID,Categories.CategoryName," +
|
||||
"Users.UserID,Users.UserName," +
|
||||
"ExpDate,CreationDate " +
|
||||
"FROM expenses " +
|
||||
"JOIN Stores, Categories " +
|
||||
"JOIN Users ON UserID = OwnerUserId"
|
||||
|
||||
var expList []Expense
|
||||
rows, err := db.Query(query)
|
||||
@@ -38,8 +56,12 @@ func GetAllExpenses() ([]Expense, error) {
|
||||
|
||||
for rows.Next() {
|
||||
exp := &Expense{}
|
||||
err := rows.Scan(&exp.ExpID, &exp.Description, &exp.Value, &exp.StoreID,
|
||||
&exp.CategoryID, &exp.OwnerUserID, &exp.ExpDate, &exp.CreationDate,
|
||||
err := rows.Scan(
|
||||
&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 {
|
||||
log.Fatalf("Failed to parse data from db: %v", err)
|
||||
@@ -57,16 +79,30 @@ func GetExpense(expID int) (Expense, error) {
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
query := "SELECT ExpID,Description,Value,StoreID,CategoryID," +
|
||||
"OwnerUserID,ExpDate,CreationDate " +
|
||||
query := "SELECT ExpID,Description,Value," +
|
||||
"Stores.StoreID,Stores.StoreName," +
|
||||
"Categories.CategoryID,Categories.CategoryName," +
|
||||
"Users.UserID,Users.UserName," +
|
||||
"ExpDate,CreationDate " +
|
||||
"FROM expenses " +
|
||||
"JOIN Stores, Categories " +
|
||||
"JOIN Users ON UserID = OwnerUserId " +
|
||||
"WHERE ExpID = " + strconv.Itoa(expID)
|
||||
|
||||
var exp Expense
|
||||
log.Println(query)
|
||||
|
||||
exp := Expense{ExpID: -1}
|
||||
err = db.QueryRow(query).Scan(
|
||||
&exp.ExpID, &exp.Description, &exp.Value, &exp.StoreID,
|
||||
&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 == sql.ErrNoRows {
|
||||
return Expense{ExpID: -1}, nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return Expense{}, nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
package expenses
|
||||
|
||||
type Stores struct {
|
||||
StoreID int
|
||||
type Store struct {
|
||||
StoreID int
|
||||
StoreName string
|
||||
}
|
||||
|
||||
func NewStore() Store {
|
||||
return Store{
|
||||
StoreID: -1,
|
||||
StoreName: "",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
package expenses
|
||||
|
||||
type Users struct {
|
||||
type User struct {
|
||||
UserID int
|
||||
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) {
|
||||
router.GET(CategoriesPath, categoriesGlobalPage)
|
||||
router.GET(CategoriesPath+"/:id", categoryPage)
|
||||
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/expenses"
|
||||
"expenses/templating"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"net/http"
|
||||
@@ -51,7 +52,7 @@ func ExpensesGlobalPage(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func ExpensePage(c *gin.Context) {
|
||||
func expensePage(c *gin.Context) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
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{
|
||||
"page": "expense",
|
||||
"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) {
|
||||
router.GET(ExpensesPath, ExpensesGlobalPage)
|
||||
router.GET(ExpensesPath+"/:id", expensePage)
|
||||
router.POST(ExpensesPath, ExpensesGlobalPage)
|
||||
|
||||
router.GET(ExpensesPath+"/:id", ExpensePage)
|
||||
router.GET(ExpensesPath+"/new", newExpensePage)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user