commiting the changes did yesterday about the qr data insertion
This commit is contained in:
@@ -5,6 +5,15 @@
|
||||
<fieldset id="form-box-boundaries">
|
||||
<legend>EXPENSE FORM</legend>
|
||||
<div class="form-container form-inline">
|
||||
|
||||
<label css="white-space: nowrap;" for="expense-qr">QR:</label>
|
||||
<input style="flex: 1; box-sizing: border-box;"
|
||||
class="tui-input underline" type="text" name="expense-qr"
|
||||
value="{{ .expense.QRString }}"
|
||||
id="expense-qr" hx-trigger="keydown[key=='Enter'] consume"
|
||||
hx-post="/expenses/scanQR" hx-swap="none">
|
||||
|
||||
|
||||
{{ if eq .method "put" }}
|
||||
<label for="expense-desc">ID:</label>
|
||||
<input
|
||||
@@ -110,5 +119,37 @@
|
||||
row.remove();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
// Avoid triggering the form submission action
|
||||
// Create a class or whatever to use a query-select on this and put this on
|
||||
// the forms.js thing
|
||||
document.getElementById("expense-qr").addEventListener("keydown", function(e) {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
}
|
||||
});
|
||||
|
||||
document.body.addEventListener("htmx:afterRequest", function(evt){
|
||||
if (evt.detail.xhr.responseType === ""
|
||||
|| evt.detail.xhr.responseType === "text") {
|
||||
try {
|
||||
const data = JSON.parse(evt.detail.xhr.responseText);
|
||||
console.log(data);
|
||||
|
||||
document.getElementById("expense-date").value = data["date"];
|
||||
document.getElementById("expense-value").value = data["total"];
|
||||
|
||||
if (data["storeID"] !== "-1") {
|
||||
document.getElementById("newexp-store-dropdown").value = data["storeID"];
|
||||
}
|
||||
|
||||
addPaymentRow(data["total"]);
|
||||
addShareRow();
|
||||
} catch(e) {
|
||||
console.error("Invalid JSON", e);
|
||||
}
|
||||
}
|
||||
})
|
||||
</script>
|
||||
{{ end }}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{{ define "expensePayments" }}
|
||||
<button
|
||||
type="button"
|
||||
type="button" id="add-payment-row"
|
||||
onclick="addPaymentRow()"
|
||||
>+ Payment</button>
|
||||
<div id="operation-state" class="warning"> </div><br>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{{ define "expenseShares" }}
|
||||
<button
|
||||
type="button"
|
||||
type="button" id="add-share-row"
|
||||
onclick="addShareRow()"
|
||||
>+ Share</button>
|
||||
<table id="shares-table">
|
||||
|
||||
@@ -6,6 +6,11 @@
|
||||
<div class="form-container form-inline">
|
||||
<div id="form-state" class="warning"></div><br>
|
||||
|
||||
<label class="fullwidth-label" for="store-qr">QR:</label>
|
||||
<input class="tui-input underline" type="text" name="store-qr"
|
||||
hx-post="/stores/getNIF" hx-swap="none"
|
||||
hx-trigger="keydown[key=='Enter'] consume" id="store-qr" value="">
|
||||
|
||||
{{ if eq .method "put" }}
|
||||
<label class="fullwidth-label" for="store-id">ID:</label>
|
||||
<input class="tui-input underline" type="text" name="store-id"
|
||||
@@ -15,6 +20,10 @@
|
||||
<label class="fullwidth-label" for="store-name">NAME:</label>
|
||||
<input class="tui-input underline" type="text" name="store-name"
|
||||
id="store-name" value="{{ .store.StoreName }}">
|
||||
|
||||
<label class="fullwidth-label" for="store-nif">NIF:</label>
|
||||
<input class="tui-input underline" type="text" name="store-nif"
|
||||
id="store-nif" value="{{ .store.StoreNIF }}">
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
@@ -41,5 +50,33 @@
|
||||
|
||||
</form>
|
||||
</div>
|
||||
<script>
|
||||
// TODO
|
||||
// Avoid triggering the form submission action
|
||||
// Create a class or whatever to use a query-select on this and put this on
|
||||
// the forms.js thing
|
||||
document.getElementById("store-qr").addEventListener("keydown", function(e) {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
}
|
||||
});
|
||||
|
||||
// TODO
|
||||
// Find a way to make this more generic or whatever, it also happens
|
||||
// in the expense.html thing
|
||||
document.body.addEventListener("htmx:afterRequest", function(evt){
|
||||
if (evt.detail.xhr.responseType === ""
|
||||
|| evt.detail.xhr.responseType === "text") {
|
||||
try {
|
||||
const data = JSON.parse(evt.detail.xhr.responseText);
|
||||
console.log(data);
|
||||
|
||||
document.getElementById("store-nif").value = data["nif"];
|
||||
} catch(e) {
|
||||
console.error("Invalid JSON", e);
|
||||
}
|
||||
}
|
||||
})
|
||||
</script>
|
||||
{{ template "deleteModal" }}
|
||||
{{ end }}
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
function addRow(tableNameID, templateRowID) {
|
||||
function addRow(tableNameID, templateRowID, value) {
|
||||
const table = document.getElementById(tableNameID);
|
||||
const templateRow = document.getElementById(templateRowID);
|
||||
const clone = templateRow.cloneNode(true);
|
||||
|
||||
// Remove the ID so we don't end up duplicating it and unhide it
|
||||
clone.removeAttribute("id");
|
||||
clone.style.display = "";
|
||||
clone.style.display = "";
|
||||
|
||||
// Enable all the disabled input fields
|
||||
// They are disabled so that they don't get grabbed by the backend
|
||||
clone.querySelectorAll('input, select').forEach(el => el.disabled = false);
|
||||
if (value) {
|
||||
const field = clone.querySelector(`[name="payments-payment[]"]`)
|
||||
field.value = value;
|
||||
}
|
||||
|
||||
table.appendChild(clone);
|
||||
}
|
||||
@@ -18,8 +22,8 @@ function addShareRow() {
|
||||
addRow("shares-table", "template-share-row");
|
||||
}
|
||||
|
||||
function addPaymentRow() {
|
||||
addRow("payments-table", "template-payment-row");
|
||||
function addPaymentRow(value) {
|
||||
addRow("payments-table", "template-payment-row", value);
|
||||
}
|
||||
|
||||
// forms main function
|
||||
|
||||
+1
-1
@@ -7,4 +7,4 @@ postgres:
|
||||
host: 127.0.0.1
|
||||
port: 5431
|
||||
migrations_path: "file://expenses/repo/pgdb/migrations/"
|
||||
mig_custom_scripts: "./expenses/db/migrations/custom/"
|
||||
mig_custom_scripts: "./expenses/repo/pgdb/migrations/custom/"
|
||||
|
||||
@@ -98,6 +98,9 @@ func StartApp() error {
|
||||
|
||||
// Run the migrations
|
||||
err = runMigrations(luaVM)
|
||||
if err != nil {
|
||||
log.Printf("Unable to run migrations: %v", err)
|
||||
}
|
||||
|
||||
// Create our final app thing
|
||||
conn, err := pgxpool.New(ctx, pgStr)
|
||||
|
||||
@@ -20,6 +20,7 @@ type Expense struct {
|
||||
Debts Debts `json:"Debts"`
|
||||
PaidOff bool `json:"PaidOff"`
|
||||
SharesEven bool `json:"SharesEven"`
|
||||
QRString string `json:"qr"`
|
||||
CreationDate time.Time `json:"CreationDate"`
|
||||
}
|
||||
type Expenses []Expense
|
||||
|
||||
@@ -3,6 +3,7 @@ package models
|
||||
type Store struct {
|
||||
StoreID int32 `json:"StoreID"`
|
||||
StoreName string `json:"StoreName"`
|
||||
StoreNIF string `json:"StoreNIF"`
|
||||
}
|
||||
type Stores []Store
|
||||
|
||||
|
||||
@@ -49,3 +49,7 @@ func pgBoolToBool(n pgtype.Bool) bool {
|
||||
func pgTimestampToTime(ts pgtype.Timestamp) time.Time {
|
||||
return ts.Time
|
||||
}
|
||||
|
||||
func pgTextToString(s pgtype.Text) string {
|
||||
return s.String
|
||||
}
|
||||
|
||||
@@ -114,6 +114,7 @@ func (p PgExpRepo) Update(
|
||||
OwnerUserID: exp.Owner.UserID,
|
||||
PaidOff: paidOff,
|
||||
SharesEven: sharesEven,
|
||||
Qr: exp.QRString,
|
||||
ExpDate: expDate,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -197,6 +198,7 @@ func (p PgExpRepo) Insert(
|
||||
ExpDate: expDate,
|
||||
PaidOff: paidOff,
|
||||
SharesEven: sharesEven,
|
||||
Qr: exp.QRString,
|
||||
CreationDate: creationDate,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -37,6 +37,7 @@ func mapRepoGetExpenseRow(e pgsqlc.GetExpenseRow) mod.Expense {
|
||||
Payments: []mod.Payment{},
|
||||
PaidOff: paidOff,
|
||||
SharesEven: sharesEven,
|
||||
QRString: e.Expense.Qr,
|
||||
CreationDate: creationDate,
|
||||
}
|
||||
}
|
||||
@@ -73,6 +74,7 @@ func mapRepoGetExpenseRowMulti(e pgsqlc.GetExpensesRow) mod.Expense {
|
||||
Payments: []mod.Payment{},
|
||||
PaidOff: paidOff,
|
||||
SharesEven: sharesEven,
|
||||
QRString: e.Expense.Qr,
|
||||
CreationDate: creationDate,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,16 @@ func (p PgStoreRepo) Get(ctx context.Context, id int32) (mod.Store, error) {
|
||||
return mapRepoStore(store), nil
|
||||
}
|
||||
|
||||
func (p PgStoreRepo) GetByNIF(ctx context.Context, nif string) (mod.Store, error) {
|
||||
queries := pgsqlc.New(p.DB)
|
||||
store, err := queries.GetStoreByNIF(ctx, nif)
|
||||
if err != nil {
|
||||
return mod.Store{}, err
|
||||
}
|
||||
|
||||
return mapRepoStore(store), nil
|
||||
}
|
||||
|
||||
func (p PgStoreRepo) GetAll(ctx context.Context) (mod.Stores, error) {
|
||||
queries := pgsqlc.New(p.DB)
|
||||
storeList, err := queries.GetStores(ctx)
|
||||
@@ -48,6 +58,7 @@ func (p PgStoreRepo) Update(ctx context.Context, s mod.Store) error {
|
||||
_, err := queries.UpdateStore(ctx, pgsqlc.UpdateStoreParams{
|
||||
StoreID: s.StoreID,
|
||||
StoreName: s.StoreName,
|
||||
NIF: s.StoreNIF,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -58,7 +69,10 @@ func (p PgStoreRepo) Update(ctx context.Context, s mod.Store) error {
|
||||
|
||||
func (p PgStoreRepo) Insert(ctx context.Context, s mod.Store) error {
|
||||
queries := pgsqlc.New(p.DB)
|
||||
res, err := queries.InsertStore(ctx, s.StoreName)
|
||||
res, err := queries.InsertStore(ctx, pgsqlc.InsertStoreParams{
|
||||
StoreName: s.StoreName,
|
||||
NIF: s.StoreNIF,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ func mapRepoStore(rs pgsqlc.Store) mod.Store {
|
||||
return mod.Store{
|
||||
StoreID: rs.StoreID,
|
||||
StoreName: rs.StoreName,
|
||||
StoreNIF: rs.NIF,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
--- 0001_qr_feature.down.sql
|
||||
|
||||
ALTER TABLE expenses DROP COLUMN "qr";
|
||||
ALTER TABLE stores DROP COLUMN "NIF";
|
||||
@@ -0,0 +1,4 @@
|
||||
--- 0001_qr_feature.up.sql
|
||||
|
||||
ALTER TABLE expenses ADD qr TEXT DEFAULT '';
|
||||
ALTER TABLE stores ADD "NIF" TEXT DEFAULT '';
|
||||
@@ -28,8 +28,8 @@ func (q *Queries) DeleteExpense(ctx context.Context, expid int32) (pgconn.Comman
|
||||
|
||||
const getExpense = `-- name: GetExpense :one
|
||||
SELECT
|
||||
expenses."ExpID", expenses."Description", expenses."Value", expenses."StoreID", expenses."CategoryID", expenses."OwnerUserID", expenses."TypeID", expenses."ExpDate", expenses."CreationDate", expenses."PaidOff", expenses."SharesEven",
|
||||
stores."StoreID", stores."StoreName",
|
||||
expenses."ExpID", expenses."Description", expenses."Value", expenses."StoreID", expenses."CategoryID", expenses."OwnerUserID", expenses."TypeID", expenses."ExpDate", expenses."CreationDate", expenses."PaidOff", expenses."SharesEven", expenses.qr,
|
||||
stores."StoreID", stores."StoreName", stores."NIF",
|
||||
categories."CategoryID", categories."CategoryName",
|
||||
users."UserID", users."UserName", users."UserPass",
|
||||
types."TypeID", types."TypeName"
|
||||
@@ -69,8 +69,10 @@ func (q *Queries) GetExpense(ctx context.Context, expid int32) (GetExpenseRow, e
|
||||
&i.Expense.CreationDate,
|
||||
&i.Expense.PaidOff,
|
||||
&i.Expense.SharesEven,
|
||||
&i.Expense.Qr,
|
||||
&i.Store.StoreID,
|
||||
&i.Store.StoreName,
|
||||
&i.Store.NIF,
|
||||
&i.Category.CategoryID,
|
||||
&i.Category.CategoryName,
|
||||
&i.User.UserID,
|
||||
@@ -84,8 +86,8 @@ func (q *Queries) GetExpense(ctx context.Context, expid int32) (GetExpenseRow, e
|
||||
|
||||
const getExpenses = `-- name: GetExpenses :many
|
||||
SELECT
|
||||
expenses."ExpID", expenses."Description", expenses."Value", expenses."StoreID", expenses."CategoryID", expenses."OwnerUserID", expenses."TypeID", expenses."ExpDate", expenses."CreationDate", expenses."PaidOff", expenses."SharesEven",
|
||||
stores."StoreID", stores."StoreName",
|
||||
expenses."ExpID", expenses."Description", expenses."Value", expenses."StoreID", expenses."CategoryID", expenses."OwnerUserID", expenses."TypeID", expenses."ExpDate", expenses."CreationDate", expenses."PaidOff", expenses."SharesEven", expenses.qr,
|
||||
stores."StoreID", stores."StoreName", stores."NIF",
|
||||
categories."CategoryID", categories."CategoryName",
|
||||
users."UserID", users."UserName", users."UserPass",
|
||||
types."TypeID", types."TypeName"
|
||||
@@ -138,8 +140,10 @@ func (q *Queries) GetExpenses(ctx context.Context, arg GetExpensesParams) ([]Get
|
||||
&i.Expense.CreationDate,
|
||||
&i.Expense.PaidOff,
|
||||
&i.Expense.SharesEven,
|
||||
&i.Expense.Qr,
|
||||
&i.Store.StoreID,
|
||||
&i.Store.StoreName,
|
||||
&i.Store.NIF,
|
||||
&i.Category.CategoryID,
|
||||
&i.Category.CategoryName,
|
||||
&i.User.UserID,
|
||||
@@ -169,9 +173,10 @@ INSERT INTO expenses(
|
||||
"ExpDate",
|
||||
"PaidOff",
|
||||
"SharesEven",
|
||||
"qr",
|
||||
"CreationDate"
|
||||
)
|
||||
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
||||
RETURNING "ExpID"
|
||||
`
|
||||
|
||||
@@ -185,6 +190,7 @@ type InsertExpenseParams struct {
|
||||
ExpDate pgtype.Timestamp
|
||||
PaidOff pgtype.Bool
|
||||
SharesEven pgtype.Bool
|
||||
Qr string
|
||||
CreationDate pgtype.Timestamp
|
||||
}
|
||||
|
||||
@@ -199,6 +205,7 @@ func (q *Queries) InsertExpense(ctx context.Context, arg InsertExpenseParams) (i
|
||||
arg.ExpDate,
|
||||
arg.PaidOff,
|
||||
arg.SharesEven,
|
||||
arg.Qr,
|
||||
arg.CreationDate,
|
||||
)
|
||||
var ExpID int32
|
||||
@@ -217,8 +224,9 @@ SET
|
||||
"OwnerUserID" = $6,
|
||||
"PaidOff" = $7,
|
||||
"SharesEven" = $8,
|
||||
"ExpDate" = $9
|
||||
WHERE "ExpID" = $10
|
||||
"qr" = $9,
|
||||
"ExpDate" = $10
|
||||
WHERE "ExpID" = $11
|
||||
`
|
||||
|
||||
type UpdateExpenseParams struct {
|
||||
@@ -230,6 +238,7 @@ type UpdateExpenseParams struct {
|
||||
OwnerUserID int32
|
||||
PaidOff pgtype.Bool
|
||||
SharesEven pgtype.Bool
|
||||
Qr string
|
||||
ExpDate pgtype.Timestamp
|
||||
ExpID int32
|
||||
}
|
||||
@@ -244,6 +253,7 @@ func (q *Queries) UpdateExpense(ctx context.Context, arg UpdateExpenseParams) (p
|
||||
arg.OwnerUserID,
|
||||
arg.PaidOff,
|
||||
arg.SharesEven,
|
||||
arg.Qr,
|
||||
arg.ExpDate,
|
||||
arg.ExpID,
|
||||
)
|
||||
|
||||
@@ -25,6 +25,7 @@ type Expense struct {
|
||||
CreationDate pgtype.Timestamp
|
||||
PaidOff pgtype.Bool
|
||||
SharesEven pgtype.Bool
|
||||
Qr string
|
||||
}
|
||||
|
||||
type ExpenseType struct {
|
||||
@@ -55,6 +56,7 @@ type SchemaMigration struct {
|
||||
type Store struct {
|
||||
StoreID int32
|
||||
StoreName string
|
||||
NIF string
|
||||
}
|
||||
|
||||
type User struct {
|
||||
|
||||
@@ -20,18 +20,29 @@ func (q *Queries) DeleteStore(ctx context.Context, storeid int32) (pgconn.Comman
|
||||
}
|
||||
|
||||
const getStore = `-- name: GetStore :one
|
||||
SELECT "StoreID", "StoreName" FROM stores WHERE "StoreID" = $1
|
||||
SELECT "StoreID", "StoreName", "NIF" FROM stores WHERE "StoreID" = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetStore(ctx context.Context, storeid int32) (Store, error) {
|
||||
row := q.db.QueryRow(ctx, getStore, storeid)
|
||||
var i Store
|
||||
err := row.Scan(&i.StoreID, &i.StoreName)
|
||||
err := row.Scan(&i.StoreID, &i.StoreName, &i.NIF)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getStoreByNIF = `-- name: GetStoreByNIF :one
|
||||
SELECT "StoreID", "StoreName", "NIF" FROM stores WHERE "NIF" = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetStoreByNIF(ctx context.Context, nif string) (Store, error) {
|
||||
row := q.db.QueryRow(ctx, getStoreByNIF, nif)
|
||||
var i Store
|
||||
err := row.Scan(&i.StoreID, &i.StoreName, &i.NIF)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getStores = `-- name: GetStores :many
|
||||
SELECT "StoreID", "StoreName" FROM stores
|
||||
SELECT "StoreID", "StoreName", "NIF" FROM stores
|
||||
`
|
||||
|
||||
func (q *Queries) GetStores(ctx context.Context) ([]Store, error) {
|
||||
@@ -43,7 +54,7 @@ func (q *Queries) GetStores(ctx context.Context) ([]Store, error) {
|
||||
var items []Store
|
||||
for rows.Next() {
|
||||
var i Store
|
||||
if err := rows.Scan(&i.StoreID, &i.StoreName); err != nil {
|
||||
if err := rows.Scan(&i.StoreID, &i.StoreName, &i.NIF); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
@@ -55,22 +66,28 @@ func (q *Queries) GetStores(ctx context.Context) ([]Store, error) {
|
||||
}
|
||||
|
||||
const insertStore = `-- name: InsertStore :execresult
|
||||
INSERT INTO stores("StoreName") VALUES($1)
|
||||
INSERT INTO stores("StoreName", "NIF") VALUES($1, $2)
|
||||
`
|
||||
|
||||
func (q *Queries) InsertStore(ctx context.Context, storename string) (pgconn.CommandTag, error) {
|
||||
return q.db.Exec(ctx, insertStore, storename)
|
||||
type InsertStoreParams struct {
|
||||
StoreName string
|
||||
NIF string
|
||||
}
|
||||
|
||||
func (q *Queries) InsertStore(ctx context.Context, arg InsertStoreParams) (pgconn.CommandTag, error) {
|
||||
return q.db.Exec(ctx, insertStore, arg.StoreName, arg.NIF)
|
||||
}
|
||||
|
||||
const updateStore = `-- name: UpdateStore :execresult
|
||||
UPDATE stores SET "StoreName" = $1 WHERE "StoreID" = $2
|
||||
UPDATE stores SET "StoreName" = $1, "NIF" = $2 WHERE "StoreID" = $3
|
||||
`
|
||||
|
||||
type UpdateStoreParams struct {
|
||||
StoreName string
|
||||
NIF string
|
||||
StoreID int32
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateStore(ctx context.Context, arg UpdateStoreParams) (pgconn.CommandTag, error) {
|
||||
return q.db.Exec(ctx, updateStore, arg.StoreName, arg.StoreID)
|
||||
return q.db.Exec(ctx, updateStore, arg.StoreName, arg.NIF, arg.StoreID)
|
||||
}
|
||||
|
||||
@@ -49,9 +49,10 @@ INSERT INTO expenses(
|
||||
"ExpDate",
|
||||
"PaidOff",
|
||||
"SharesEven",
|
||||
"qr",
|
||||
"CreationDate"
|
||||
)
|
||||
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
||||
RETURNING "ExpID";
|
||||
|
||||
-- name: DeleteExpense :execresult
|
||||
@@ -74,5 +75,6 @@ SET
|
||||
"OwnerUserID" = $6,
|
||||
"PaidOff" = $7,
|
||||
"SharesEven" = $8,
|
||||
"ExpDate" = $9
|
||||
WHERE "ExpID" = $10;
|
||||
"qr" = $9,
|
||||
"ExpDate" = $10
|
||||
WHERE "ExpID" = $11;
|
||||
|
||||
@@ -5,10 +5,13 @@ SELECT * FROM stores;
|
||||
SELECT * FROM stores WHERE "StoreID" = $1;
|
||||
|
||||
-- name: InsertStore :execresult
|
||||
INSERT INTO stores("StoreName") VALUES($1);
|
||||
INSERT INTO stores("StoreName", "NIF") VALUES($1, $2);
|
||||
|
||||
-- name: UpdateStore :execresult
|
||||
UPDATE stores SET "StoreName" = $1 WHERE "StoreID" = $2;
|
||||
UPDATE stores SET "StoreName" = $1, "NIF" = $2 WHERE "StoreID" = $3;
|
||||
|
||||
-- name: DeleteStore :execresult
|
||||
DELETE FROM stores WHERE "StoreID" = $1;
|
||||
|
||||
-- name: GetStoreByNIF :one
|
||||
SELECT * FROM stores WHERE "NIF" = $1;
|
||||
|
||||
@@ -14,7 +14,8 @@ CREATE TABLE "expenseTypes" (
|
||||
|
||||
CREATE TABLE stores (
|
||||
"StoreID" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
"StoreName" TEXT NOT NULL
|
||||
"StoreName" TEXT NOT NULL,
|
||||
"NIF" TEXT DEFAULT '' NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE users (
|
||||
@@ -35,6 +36,7 @@ CREATE TABLE expenses (
|
||||
"CreationDate" TIMESTAMP NOT NULL,
|
||||
"PaidOff" BOOLEAN DEFAULT FALSE,
|
||||
"SharesEven" BOOLEAN DEFAULT FALSE,
|
||||
"qr" TEXT DEFAULT '' NOT NULL,
|
||||
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,
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
type StoreRepository interface {
|
||||
Get(ctx context.Context, id int32) (mod.Store, error)
|
||||
GetByNIF(ctx context.Context, nif string) (mod.Store, error)
|
||||
GetAll(ctx context.Context) (mod.Stores, error)
|
||||
Update(ctx context.Context, cat mod.Store) error
|
||||
Insert(ctx context.Context, cat mod.Store) error
|
||||
|
||||
+15
-2
@@ -15,22 +15,24 @@ func (a *ExpensesApp) GetStore(id int32) (mod.Store, error) {
|
||||
return a.StoreRepo.Get(ctx, id)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) NewStore(name string) error {
|
||||
func (a *ExpensesApp) NewStore(name string, nif string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
newStore := mod.Store{
|
||||
StoreName: name,
|
||||
StoreNIF: nif,
|
||||
}
|
||||
|
||||
return a.StoreRepo.Insert(ctx, newStore)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) UpdateStore(id int32, name string) error {
|
||||
func (a *ExpensesApp) UpdateStore(id int32, name string, nif string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
store := mod.Store{
|
||||
StoreID: id,
|
||||
StoreName: name,
|
||||
StoreNIF: nif,
|
||||
}
|
||||
|
||||
return a.StoreRepo.Update(ctx, store)
|
||||
@@ -40,3 +42,14 @@ func (a *ExpensesApp) DeleteStore(id int32) error {
|
||||
ctx := context.Background()
|
||||
return a.StoreRepo.Delete(ctx, id)
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) GetStoreIDFromNIF(nif string) (int32, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
store, err := a.StoreRepo.GetByNIF(ctx, nif)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return store.StoreID, nil
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,14 +1,14 @@
|
||||
module expenses
|
||||
|
||||
go 1.24.4
|
||||
go 1.24.6
|
||||
|
||||
require (
|
||||
github.com/ESilva15/gofatqr v1.0.0
|
||||
github.com/gin-gonic/gin v1.10.1
|
||||
github.com/golang-migrate/migrate/v4 v4.18.3
|
||||
github.com/jackc/pgx/v5 v5.5.4
|
||||
github.com/layeh/gopher-json v0.0.0-20201124131017-552bb3c4c3bf
|
||||
github.com/mattn/go-sqlite3 v1.14.28
|
||||
github.com/shopspring/decimal v1.2.0
|
||||
github.com/shopspring/decimal v1.4.0
|
||||
github.com/spf13/cobra v1.9.1
|
||||
github.com/yuin/gopher-lua v1.1.1
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
|
||||
+6
-8
@@ -1,5 +1,7 @@
|
||||
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
|
||||
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
|
||||
github.com/ESilva15/gofatqr v1.0.0 h1:uQXfqdDW9dZwLlAZvUOwHmmXF5jvLgsGEXDgNAMAces=
|
||||
github.com/ESilva15/gofatqr v1.0.0/go.mod h1:9sstTFOww1SkBUGbdtW+iWmIi7tikaZkvX+qA4ahgbo=
|
||||
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
|
||||
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
|
||||
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||
@@ -50,8 +52,8 @@ github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/golang-migrate/migrate/v4 v4.18.3 h1:EYGkoOsvgHHfm5U/naS1RP/6PL/Xv3S4B/swMiAmDLs=
|
||||
github.com/golang-migrate/migrate/v4 v4.18.3/go.mod h1:99BKpIi6ruaaXRM1A77eqZ+FWPQ3cfRa+ZVy5bmWMaY=
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
|
||||
@@ -86,8 +88,6 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A=
|
||||
github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
|
||||
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
|
||||
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
|
||||
@@ -112,8 +112,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
||||
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
|
||||
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
|
||||
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
|
||||
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
|
||||
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
|
||||
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
|
||||
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
|
||||
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
|
||||
@@ -161,8 +161,6 @@ golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
||||
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
|
||||
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
|
||||
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU=
|
||||
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
|
||||
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
||||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
||||
+32
-1
@@ -6,9 +6,10 @@ import (
|
||||
experr "expenses/expenses/errors"
|
||||
mod "expenses/expenses/models"
|
||||
"fmt"
|
||||
fatqr "github.com/ESilva15/gofatqr"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -164,6 +165,7 @@ func createExpense(c *gin.Context) {
|
||||
// TODO
|
||||
// Change this to something the user can see
|
||||
c.Header("HX-Redirect", "/500")
|
||||
return
|
||||
}
|
||||
|
||||
err = exp.App.NewExpense(*newExp)
|
||||
@@ -226,6 +228,33 @@ func deleteExpense(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func qrRequest(c *gin.Context) {
|
||||
qr := c.PostForm("expense-qr")
|
||||
log.Println(qr)
|
||||
|
||||
var fat fatqr.FatQR
|
||||
err := fat.Scan(qr, 0)
|
||||
if err != nil {
|
||||
// TODO
|
||||
// handle the error here for the user
|
||||
return
|
||||
}
|
||||
|
||||
// try to find storeID
|
||||
storeID, err := exp.App.GetStoreIDFromNIF(fat.TaxRegistrationNumber)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
response := map[string]string{
|
||||
"total": fat.GrossTotal.String(),
|
||||
"date": fat.InvoiceDate.Format("02-Jan-2006"),
|
||||
"storeID": fmt.Sprintf("%d", storeID),
|
||||
}
|
||||
|
||||
c.JSON(200, response)
|
||||
}
|
||||
|
||||
func RouteExpenses(router *gin.Engine) {
|
||||
router.GET(ExpensesPath, ExpensesGlobalPage)
|
||||
router.GET(ExpensesPath+"/:id", expensePage)
|
||||
@@ -235,4 +264,6 @@ func RouteExpenses(router *gin.Engine) {
|
||||
router.POST(ExpensesPath+"/new", createExpense)
|
||||
router.PUT(ExpensesPath+"/:id", updateExpense)
|
||||
router.DELETE(ExpensesPath+"/:id", deleteExpense)
|
||||
|
||||
router.POST(ExpensesPath+"/scanQR", qrRequest)
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ func processFormPayments(c *gin.Context) ([]mod.Payment, error) {
|
||||
func expenseFromForm(c *gin.Context) (*mod.Expense, error) {
|
||||
newDescription := c.PostForm("expense-desc")
|
||||
newDate := c.PostForm("expense-date")
|
||||
newQR := c.PostForm("expense-qr")
|
||||
|
||||
formattedDate, err := time.ParseInLocation("02-Jan-2006", newDate, time.UTC)
|
||||
if err != nil {
|
||||
@@ -88,6 +89,7 @@ func expenseFromForm(c *gin.Context) (*mod.Expense, error) {
|
||||
CreationDate: time.Now(),
|
||||
Shares: shares,
|
||||
Payments: payments,
|
||||
QRString: newQR,
|
||||
}
|
||||
return &newExp, nil
|
||||
}
|
||||
|
||||
+29
-2
@@ -5,7 +5,10 @@ import (
|
||||
exp "expenses/expenses"
|
||||
experr "expenses/expenses/errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
fatqr "github.com/ESilva15/gofatqr"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -65,10 +68,12 @@ func newStorePage(c *gin.Context) {
|
||||
|
||||
func createStore(c *gin.Context) {
|
||||
storeName := c.PostForm("store-name")
|
||||
storeNIF := c.PostForm("store-nif")
|
||||
|
||||
err := exp.App.NewStore(storeName)
|
||||
err := exp.App.NewStore(storeName, storeNIF)
|
||||
if err != nil {
|
||||
c.Header("HX-Trigger", fmt.Sprintf("{\"formState\":\"%s\"}", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
c.Header("HX-Trigger", "{\"formState\":\"Success\"}")
|
||||
@@ -80,8 +85,9 @@ func updateStore(c *gin.Context) {
|
||||
c.Redirect(404, "/404")
|
||||
}
|
||||
newName := c.PostForm("store-name")
|
||||
newNIF := c.PostForm("store-nif")
|
||||
|
||||
err = exp.App.UpdateStore(storeID, newName)
|
||||
err = exp.App.UpdateStore(storeID, newName, newNIF)
|
||||
if err != nil {
|
||||
c.Header("HX-Trigger", fmt.Sprintf("{\"formState\":\"%s\"}", err.Error()))
|
||||
return
|
||||
@@ -112,6 +118,25 @@ func deleteStore(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func getNIF(c *gin.Context) {
|
||||
qr := c.PostForm("store-qr")
|
||||
log.Println(qr)
|
||||
|
||||
var fat fatqr.FatQR
|
||||
err := fat.Scan(qr, 0)
|
||||
if err != nil {
|
||||
// TODO
|
||||
// handle the error here for the user
|
||||
return
|
||||
}
|
||||
|
||||
response := map[string]string{
|
||||
"nif": fat.TaxRegistrationNumber,
|
||||
}
|
||||
|
||||
c.JSON(200, response)
|
||||
}
|
||||
|
||||
func RouteStores(router *gin.Engine) {
|
||||
router.GET(StoresPath, storesGlobalPage)
|
||||
router.GET(StoresPath+"/:id", storePage)
|
||||
@@ -121,4 +146,6 @@ func RouteStores(router *gin.Engine) {
|
||||
router.POST(StoresPath+"/new", createStore)
|
||||
router.PUT(StoresPath+"/:id", updateStore)
|
||||
router.DELETE(StoresPath+"/:id", deleteStore)
|
||||
|
||||
router.POST(StoresPath+"/getNIF", getNIF)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func storeFromForm(c *gin.Context) (*mod.Store, error) {
|
||||
name := c.PostForm("store-name")
|
||||
nif := c.PostForm("store-nif")
|
||||
|
||||
store := mod.Store{
|
||||
StoreName: name,
|
||||
StoreNIF: nif,
|
||||
}
|
||||
|
||||
return &store, nil
|
||||
}
|
||||
Reference in New Issue
Block a user