Added some filters
This commit is contained in:
@@ -1,29 +1,4 @@
|
|||||||
{{ define "expenses" }}
|
{{ define "expenses" }}
|
||||||
<fieldset>
|
{{ template "expensesFilterBox" . }}
|
||||||
<legend>FILTERS</legend>
|
{{ template "expensesTable" . }}
|
||||||
<form action="">
|
|
||||||
</form>
|
|
||||||
</fieldset>
|
|
||||||
<table>
|
|
||||||
<tr>
|
|
||||||
<th>ID</th>
|
|
||||||
<th>OWNER</th>
|
|
||||||
<th>DESC</th>
|
|
||||||
<th>PRICE</th>
|
|
||||||
<th>DATE</th>
|
|
||||||
<!-- <th>Store</th> -->
|
|
||||||
<th>Category</th>
|
|
||||||
<!-- <th>Owner</th> -->
|
|
||||||
</tr>
|
|
||||||
{{ range .expenses }}
|
|
||||||
<tr>
|
|
||||||
<th><a href="/expenses/{{ .ExpID }}">{{ .ExpID }}</th>
|
|
||||||
<th spellcheck="false">{{ .Owner.UserName }}</th>
|
|
||||||
<th spellcheck="false">{{ .Description }}</th>
|
|
||||||
<th spellcheck="false">{{ formatPrice .Value }}</th>
|
|
||||||
<th spellcheck="false">{{ formatDate .Date }}</th>
|
|
||||||
<th spellcheck="false">{{ .Category.CategoryName }}</th>
|
|
||||||
</tr>
|
|
||||||
{{ end }}
|
|
||||||
</table>
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
{{ define "expensesFilterBox" }}
|
||||||
|
<style>
|
||||||
|
.filters {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap; /* allow wrapping to next line if needed */
|
||||||
|
gap: 1em; /* space between fields */
|
||||||
|
}
|
||||||
|
|
||||||
|
.filter-field {
|
||||||
|
flex: 0 1 200px; /* min width 200px, shrink if needed, don't force full width */
|
||||||
|
}
|
||||||
|
|
||||||
|
.filter-field label {
|
||||||
|
display: block; /* stack label above select */
|
||||||
|
margin-bottom: 0.25em;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
|
.filter-field select {
|
||||||
|
width: auto; /* let select size itself, not full width */
|
||||||
|
min-width: 100%; /* ensures it’s at least as wide as the container */
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<fieldset>
|
||||||
|
<legend>FILTERS</legend>
|
||||||
|
<form
|
||||||
|
hx-get="/expenses"
|
||||||
|
hx-target="#expenses-table"
|
||||||
|
hx-swap="innerHTML"
|
||||||
|
>
|
||||||
|
|
||||||
|
<!-- Start Date Input -->
|
||||||
|
<label for="range-start">Start date:</label>
|
||||||
|
<input
|
||||||
|
class="tui-input underline" type="text" name="range-start"
|
||||||
|
value="" id="range-start">
|
||||||
|
|
||||||
|
<!-- End Data Input -->
|
||||||
|
<label for="range-end">End date:</label>
|
||||||
|
<input
|
||||||
|
class="tui-input underline" type="text" name="range-end"
|
||||||
|
value="" id="range-end"><br><br>
|
||||||
|
|
||||||
|
<!-- Drop downs -->
|
||||||
|
<div class="filter">
|
||||||
|
<!-- Category Input -->
|
||||||
|
<div class="filter-field">
|
||||||
|
<label for="cat-dropdown[]">Categories:</label>
|
||||||
|
<select name="cat-dropdown[]" id="cat-dropdown"
|
||||||
|
class="tui-dropdown" multiple>
|
||||||
|
{{ range .categories }}
|
||||||
|
<option
|
||||||
|
value="{{ .CategoryID }}">{{ .CategoryName }}</option>
|
||||||
|
{{ end}}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Store Input -->
|
||||||
|
<div class="filter-field">
|
||||||
|
<label for="store-dropdown[]">Stores:</label>
|
||||||
|
<select name="store-dropdown[]" id="store-dropdown"
|
||||||
|
class="tui-dropdown" multiple>
|
||||||
|
{{ range .stores }}
|
||||||
|
<option
|
||||||
|
value="{{ .StoreID }}">{{ .StoreName }}</option>
|
||||||
|
{{ end}}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Type Input -->
|
||||||
|
<div class="filter-field">
|
||||||
|
<label for="type-dropdown[]">Types:</label>
|
||||||
|
<select name="type-dropdown[]" id="type-dropdown"
|
||||||
|
class="tui-dropdown" multiple>
|
||||||
|
{{ range .types }}
|
||||||
|
<option
|
||||||
|
value="{{ .TypeID }}">{{ .TypeName }}</option>
|
||||||
|
{{ end}}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- TODO -->
|
||||||
|
<!-- Ordering is left as an exercise for the reader -->
|
||||||
|
|
||||||
|
<br>
|
||||||
|
<button type="submit" id="expenses">FILTER</button>
|
||||||
|
</form>
|
||||||
|
</fieldset>
|
||||||
|
{{ end }}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
{{ define "expensesTable" }}
|
||||||
|
<table id="expenses-table">
|
||||||
|
<tr>
|
||||||
|
<th>ID</th>
|
||||||
|
<th>OWNER</th>
|
||||||
|
<th>DESC</th>
|
||||||
|
<th>PRICE</th>
|
||||||
|
<th>DATE</th>
|
||||||
|
<th>Category</th>
|
||||||
|
</tr>
|
||||||
|
{{ range .expenses }}
|
||||||
|
<tr>
|
||||||
|
<th><a href="/expenses/{{ .ExpID }}">{{ .ExpID }}</th>
|
||||||
|
<th spellcheck="false">{{ .Owner.UserName }}</th>
|
||||||
|
<th spellcheck="false">{{ .Description }}</th>
|
||||||
|
<th spellcheck="false">{{ formatPrice .Value }}</th>
|
||||||
|
<th spellcheck="false">{{ formatDate .Date }}</th>
|
||||||
|
<th spellcheck="false">{{ .Category.CategoryName }}</th>
|
||||||
|
</tr>
|
||||||
|
{{ end }}
|
||||||
|
</table>
|
||||||
|
{{ end }}
|
||||||
@@ -10,15 +10,21 @@ import (
|
|||||||
|
|
||||||
// ExpFilter has the fields required to query the database.
|
// ExpFilter has the fields required to query the database.
|
||||||
type ExpFilter struct {
|
type ExpFilter struct {
|
||||||
Start *time.Time
|
Start *time.Time // Start is the earliest an expense can be
|
||||||
End *time.Time
|
End *time.Time // End is the latest an expense can be
|
||||||
|
CatIDs []int32 // CatIDs is a list of categories to match against
|
||||||
|
StoreIDs []int32 // StoreIDs is a list of stores to match against
|
||||||
|
TypeIDs []int32 // TypeIDs is a list of types to match against
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewExpFilter returns a newly initialized ExpFilter.
|
// NewExpFilter returns a newly initialized ExpFilter.
|
||||||
func NewExpFilter() ExpFilter {
|
func NewExpFilter() ExpFilter {
|
||||||
return ExpFilter{
|
return ExpFilter{
|
||||||
Start: nil,
|
Start: nil,
|
||||||
End: nil,
|
End: nil,
|
||||||
|
CatIDs: nil,
|
||||||
|
StoreIDs: nil,
|
||||||
|
TypeIDs: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,6 +62,9 @@ func (p PgExpRepo) GetAll(ctx context.Context, filter ExpFilter, uID int32,
|
|||||||
Startdate: startPg,
|
Startdate: startPg,
|
||||||
Enddate: endPg,
|
Enddate: endPg,
|
||||||
UserID: uID,
|
UserID: uID,
|
||||||
|
Catids: filter.CatIDs,
|
||||||
|
Storeids: filter.StoreIDs,
|
||||||
|
Typeids: filter.TypeIDs,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mod.Expenses{}, err
|
return mod.Expenses{}, err
|
||||||
|
|||||||
@@ -106,12 +106,22 @@ AND
|
|||||||
($2::timestamp IS NULL OR expenses."ExpDate" >= $2::timestamp)
|
($2::timestamp IS NULL OR expenses."ExpDate" >= $2::timestamp)
|
||||||
AND
|
AND
|
||||||
($3::timestamp IS NULL OR expenses."ExpDate" <= $3::timestamp)
|
($3::timestamp IS NULL OR expenses."ExpDate" <= $3::timestamp)
|
||||||
|
AND
|
||||||
|
($4::int[] IS NULL OR expenses."CategoryID" = ANY($4::int[]))
|
||||||
|
AND
|
||||||
|
($5::int[] IS NULL OR expenses."StoreID" = ANY($5::int[]))
|
||||||
|
AND
|
||||||
|
($6::int[] IS NULL OR expenses."TypeID" = ANY($6::int[]))
|
||||||
|
ORDER BY expenses."ExpDate"
|
||||||
`
|
`
|
||||||
|
|
||||||
type GetExpensesParams struct {
|
type GetExpensesParams struct {
|
||||||
UserID int32
|
UserID int32
|
||||||
Startdate pgtype.Timestamp
|
Startdate pgtype.Timestamp
|
||||||
Enddate pgtype.Timestamp
|
Enddate pgtype.Timestamp
|
||||||
|
Catids []int32
|
||||||
|
Storeids []int32
|
||||||
|
Typeids []int32
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetExpensesRow struct {
|
type GetExpensesRow struct {
|
||||||
@@ -123,7 +133,14 @@ type GetExpensesRow struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) GetExpenses(ctx context.Context, arg GetExpensesParams) ([]GetExpensesRow, error) {
|
func (q *Queries) GetExpenses(ctx context.Context, arg GetExpensesParams) ([]GetExpensesRow, error) {
|
||||||
rows, err := q.db.Query(ctx, getExpenses, arg.UserID, arg.Startdate, arg.Enddate)
|
rows, err := q.db.Query(ctx, getExpenses,
|
||||||
|
arg.UserID,
|
||||||
|
arg.Startdate,
|
||||||
|
arg.Enddate,
|
||||||
|
arg.Catids,
|
||||||
|
arg.Storeids,
|
||||||
|
arg.Typeids,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,14 @@ WHERE
|
|||||||
AND
|
AND
|
||||||
(sqlc.narg(StartDate)::timestamp IS NULL OR expenses."ExpDate" >= sqlc.narg(StartDate)::timestamp)
|
(sqlc.narg(StartDate)::timestamp IS NULL OR expenses."ExpDate" >= sqlc.narg(StartDate)::timestamp)
|
||||||
AND
|
AND
|
||||||
(sqlc.narg(EndDate)::timestamp IS NULL OR expenses."ExpDate" <= sqlc.narg(EndDate)::timestamp);
|
(sqlc.narg(EndDate)::timestamp IS NULL OR expenses."ExpDate" <= sqlc.narg(EndDate)::timestamp)
|
||||||
|
AND
|
||||||
|
(sqlc.narg(CatIDs)::int[] IS NULL OR expenses."CategoryID" = ANY(sqlc.narg(CatIDs)::int[]))
|
||||||
|
AND
|
||||||
|
(sqlc.narg(StoreIDs)::int[] IS NULL OR expenses."StoreID" = ANY(sqlc.narg(StoreIDs)::int[]))
|
||||||
|
AND
|
||||||
|
(sqlc.narg(TypeIDs)::int[] IS NULL OR expenses."TypeID" = ANY(sqlc.narg(TypeIDs)::int[]))
|
||||||
|
ORDER BY expenses."ExpDate";
|
||||||
|
|
||||||
-- name: GetExpense :one
|
-- name: GetExpense :one
|
||||||
SELECT
|
SELECT
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
// Package ginaux will have auxiliary functions for common gin stuff
|
||||||
|
package ginaux
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package ginaux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// QueryIntArray will return an array of ints of a given query paramenter.
|
||||||
|
func QueryIntArray(c *gin.Context, param string) ([]int32, error) {
|
||||||
|
selCategories := c.QueryArray(param)
|
||||||
|
var newArr []int32
|
||||||
|
if len(selCategories) > 0 {
|
||||||
|
newArr = make([]int32, len(selCategories))
|
||||||
|
|
||||||
|
for k, val := range selCategories {
|
||||||
|
id, err := strconv.ParseInt(val, 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error converting id [%s]: %v", val, err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
newArr[k] = int32(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newArr, nil
|
||||||
|
}
|
||||||
+51
-2
@@ -41,6 +41,31 @@ func expensesPartial(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getExpenses(c *gin.Context) {
|
||||||
|
ctx, err := getLoggedInUserCTX(c)
|
||||||
|
if err != nil {
|
||||||
|
ServerErrorView(c, "Could not get logged in user")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
eFilter, err := expenseFilterFromQuery(c)
|
||||||
|
if err != nil {
|
||||||
|
ServerErrorView(c, "Could not get logged in user")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
expenses, err := exp.App.GetAllExpenses(ctx, eFilter)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error:", err)
|
||||||
|
ServerErrorView(c, "Could not get logged in user")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.HTML(http.StatusOK, "expensesTable", gin.H{
|
||||||
|
"expenses": expenses,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func expensesGlobalPage(c *gin.Context) {
|
func expensesGlobalPage(c *gin.Context) {
|
||||||
ctx, err := getLoggedInUserCTX(c)
|
ctx, err := getLoggedInUserCTX(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -56,11 +81,35 @@ func expensesGlobalPage(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
categories, err := exp.App.GetAllCategories(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error:", err)
|
||||||
|
ServerErrorView(c, "Unable to get categories")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
stores, err := exp.App.GetAllStores(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error:", err)
|
||||||
|
ServerErrorView(c, "Unable to get stores")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
types, err := exp.App.GetAllTypes(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error:", err)
|
||||||
|
ServerErrorView(c, "Unable to get types")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||||
"page": "expenses",
|
"page": "expenses",
|
||||||
"renderNavBar": true,
|
"renderNavBar": true,
|
||||||
"content": "expenses",
|
"content": "expenses",
|
||||||
"expenses": expenses,
|
"expenses": expenses,
|
||||||
|
"categories": categories,
|
||||||
|
"stores": stores,
|
||||||
|
"types": types,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -301,9 +350,9 @@ func qrRequest(c *gin.Context) {
|
|||||||
|
|
||||||
// RouteExpenses routes the endpoints for the HTML frontend.
|
// RouteExpenses routes the endpoints for the HTML frontend.
|
||||||
func RouteExpenses(router *gin.RouterGroup) {
|
func RouteExpenses(router *gin.RouterGroup) {
|
||||||
router.GET(ExpensesPath, expensesGlobalPage)
|
router.GET(ExpensesPath, getExpenses)
|
||||||
router.GET(ExpensesPath+"/:id", expensePage)
|
router.GET(ExpensesPath+"/:id", expensePage)
|
||||||
router.POST(ExpensesPath, expensesPartial)
|
router.GET("/main", expensesGlobalPage)
|
||||||
|
|
||||||
router.GET(ExpensesPath+"/new", newExpensePage)
|
router.GET(ExpensesPath+"/new", newExpensePage)
|
||||||
router.POST(ExpensesPath+"/new", createExpense)
|
router.POST(ExpensesPath+"/new", createExpense)
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
expenses "github.com/ESilva15/expenses/expenses"
|
expenses "github.com/ESilva15/expenses/expenses"
|
||||||
mod "github.com/ESilva15/expenses/expenses/models"
|
mod "github.com/ESilva15/expenses/expenses/models"
|
||||||
"github.com/ESilva15/expenses/expenses/repo"
|
"github.com/ESilva15/expenses/expenses/repo"
|
||||||
|
ginaux "github.com/ESilva15/expenses/ginAux"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
@@ -40,6 +41,24 @@ func expenseFilterFromQuery(c *gin.Context) (repo.ExpFilter, error) {
|
|||||||
filter.End = &end
|
filter.End = &end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filter.CatIDs, err = ginaux.QueryIntArray(c, "cat-dropdown[]")
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to retrieve IDs from cat-dropdown[] %+v", err)
|
||||||
|
return repo.ExpFilter{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
filter.StoreIDs, err = ginaux.QueryIntArray(c, "store-dropdown[]")
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to retrieve IDs from store-dropdown[] %+v", err)
|
||||||
|
return repo.ExpFilter{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
filter.TypeIDs, err = ginaux.QueryIntArray(c, "type-dropdown[]")
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to retrieve IDs from type-dropdown[] %+v", err)
|
||||||
|
return repo.ExpFilter{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
return filter, nil
|
return filter, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -6,8 +6,9 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RouteHome routes the home pages.
|
||||||
func RouteHome(router *gin.RouterGroup) {
|
func RouteHome(router *gin.RouterGroup) {
|
||||||
router.GET("/", func(c *gin.Context) {
|
router.GET("/", func(c *gin.Context) {
|
||||||
c.Redirect(http.StatusFound, ExpensesPath)
|
c.Redirect(http.StatusFound, "/main")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ test: remove-dev-container
|
|||||||
--name $(container)-dev \
|
--name $(container)-dev \
|
||||||
--env-file .env \
|
--env-file .env \
|
||||||
-v ./pgsql-data-dev/:/var/lib/postgresql/data/ \
|
-v ./pgsql-data-dev/:/var/lib/postgresql/data/ \
|
||||||
-it $(registry)/$(repo)/expensesdb:$(tag)
|
-it $(registry)/$(repo)/expensesdb:v0.0.91-alpha
|
||||||
|
|
||||||
build_image:
|
build_image:
|
||||||
make -C ./
|
make -C ./
|
||||||
|
|||||||
Reference in New Issue
Block a user