all repos — piggy @ d8748d219af2eb8f80b1589a04f72614d5638b86

Dead simple finance manager in Go, HTML and JS.

add delete function
Marco Andronaco andronacomarco@gmail.com
Sun, 06 Oct 2024 13:32:28 +0200
commit

d8748d219af2eb8f80b1589a04f72614d5638b86

parent

aac7712a4650e29b43bbaec3b3ac295214a16367

M src/api/api.gosrc/api/api.go

@@ -56,6 +56,23 @@

jsonResponse(w, bookmaker) } +func deleteBookmakersId(w http.ResponseWriter, r *http.Request) { + id, err := getId(r) + if err != nil { + new400Error(w, err) + return + } + + err = app.DB.Delete(&app.Bookmaker{}, id).Error + if err != nil { + log.Println("could not delete bookmaker: " + err.Error()) + new500Error(w, err) + return + } + + jsonResponse(w, []uint{id}) +} + func getAccounts(w http.ResponseWriter, r *http.Request) { var accounts []app.Account err := app.DB.Find(&accounts).Error

@@ -104,6 +121,23 @@

jsonResponse(w, account) } +func deleteAccountsId(w http.ResponseWriter, r *http.Request) { + id, err := getId(r) + if err != nil { + new400Error(w, err) + return + } + + err = app.DB.Delete(&app.Account{}, id).Error + if err != nil { + log.Println("could not delete account: " + err.Error()) + new500Error(w, err) + return + } + + jsonResponse(w, []uint{id}) +} + func getRecords(w http.ResponseWriter, r *http.Request) { records, _, err := app.GetRecords() if err != nil {

@@ -147,3 +181,20 @@ }

jsonResponse(w, record) } + +func deleteRecordsId(w http.ResponseWriter, r *http.Request) { + id, err := getId(r) + if err != nil { + new400Error(w, err) + return + } + + err = app.DB.Delete(&app.Record{}, id).Error + if err != nil { + log.Println("could not delete account: " + err.Error()) + new500Error(w, err) + return + } + + jsonResponse(w, []uint{id}) +}
M src/api/routes.gosrc/api/routes.go

@@ -17,14 +17,17 @@

http.HandleFunc("GET /api/bookmakers", getBookmakers) http.HandleFunc("GET /api/bookmakers/{id}", getBookmakersId) http.HandleFunc("POST /api/bookmakers", postBookmakers) + http.HandleFunc("DELETE /api/bookmakers/{id}", deleteBookmakersId) http.HandleFunc("GET /api/accounts", getAccounts) http.HandleFunc("GET /api/accounts/{id}", getAccountsId) http.HandleFunc("POST /api/accounts", postAccounts) + http.HandleFunc("DELETE /api/accounts/{id}", deleteAccountsId) http.HandleFunc("GET /api/records", getRecords) http.HandleFunc("GET /api/records/{id}", getRecordsId) http.HandleFunc("POST /api/records", postRecords) + http.HandleFunc("DELETE /api/records/{id}", deleteRecordsId) log.Println("Serving at " + address + "...") log.Fatal(http.ListenAndServe(address, nil))
M static/accounts/edit/index.htmlstatic/accounts/edit/index.html

@@ -18,7 +18,10 @@

<div id="main-container"> </div> - <button id="save">Save</button> + <div id="buttons"> + <button id="save">Save</button> + <button id="delete">Delete</button> + </div> </main> <script src="/js/common.js"></script> <script src="/js/accounts-edit.js"></script>
M static/accounts/index.htmlstatic/accounts/index.html

@@ -23,7 +23,9 @@ <tfoot></tfoot>

</table> </div> - <button id="new-account">New Account</button> + <div id="buttons"> + <button id="new-account">New Account</button> + </div> </main> <script src="/js/common.js"></script> <script src="/js/accounts.js"></script>
M static/bookmakers/edit/index.htmlstatic/bookmakers/edit/index.html

@@ -18,7 +18,10 @@

<div id="main-container"> </div> - <button id="save">Save</button> + <div id="buttons"> + <button id="save">Save</button> + <button id="delete">Delete</button> + </div> </main> <script src="/js/common.js"></script> <script src="/js/bookmakers-edit.js"></script>
M static/bookmakers/index.htmlstatic/bookmakers/index.html

@@ -23,7 +23,9 @@ <tfoot></tfoot>

</table> </div> - <button id="new-bookmaker">New Bookmaker</button> + <div id="buttons"> + <button id="new-bookmaker">New Bookmaker</button> + </div> </main> <script src="/js/common.js"></script> <script src="/js/bookmakers.js"></script>
M static/css/styles.cssstatic/css/styles.css

@@ -1,9 +1,8 @@

body { font-family: Arial, sans-serif; - margin-top: 20px; + margin: 20px auto; background-color: beige; max-width: 800px; - margin-inline: auto; } nav > a:link, nav > a:visited {

@@ -44,9 +43,13 @@ table td, table th {

padding: 10px; /* Adjust the padding value as needed */ } -button { +#buttons { display: block; margin: 20px auto; + text-align: center; +} + +button { padding: 10px 20px; font-size: 16px; }
M static/js/accounts-edit.jsstatic/js/accounts-edit.js

@@ -2,6 +2,12 @@ document.addEventListener('DOMContentLoaded', function () {

handleID(); document.getElementById('save').addEventListener('click', submit); + + if (id === 0) { + document.getElementById('delete').style.display = "none"; + } else { + document.getElementById('delete').addEventListener('click', remove); + } }); let id;

@@ -43,3 +49,9 @@ location.href = "/accounts"

} } +async function remove() { + if (await myConfirm(deleteAccount, id)) { + location.href = "/accounts" + } +} +
M static/js/bookmakers-edit.jsstatic/js/bookmakers-edit.js

@@ -2,6 +2,12 @@ document.addEventListener('DOMContentLoaded', function () {

handleID(); document.getElementById('save').addEventListener('click', submit); + + if (id === 0) { + document.getElementById('delete').style.display = "none"; + } else { + document.getElementById('delete').addEventListener('click', remove); + } }); let id;

@@ -47,3 +53,8 @@ location.href = "/bookmakers"

} } +async function remove() { + if (await myConfirm(deleteBookmaker, id)) { + location.href = "/bookmakers" + } +}
M static/js/common.jsstatic/js/common.js

@@ -71,6 +71,11 @@ function getQueryStringID() {

return Number(new URLSearchParams(window.location.search).get("id") ?? 0); } +async function myConfirm(f, id) { + if (confirm("Are you sure?")) return await f(id); + return false; +} + async function handleFetchResult(res) { if (!res.ok) { console.error(await res.text())

@@ -94,39 +99,58 @@ });

return await handleFetchResult(res); } +async function myFetchDELETE(url) { + const res = await fetch(url, { method: 'DELETE' }); + return await handleFetchResult(res); +} + // API calls async function getBookmakers() { return await myFetch('/api/bookmakers'); } -async function getAccounts() { - return await myFetch('/api/accounts'); +async function getBookmaker(id) { + return await myFetch(`/api/bookmakers/${id}`); } -async function getRecords() { - return await myFetch('/api/records'); +async function saveBookmaker(payload) { + return await myFetchPOST("/api/bookmakers", payload); } -async function getBookmaker(id) { - return await myFetch(`/api/bookmakers/${id}`); +async function deleteBookmaker(id) { + return await myFetchDELETE(`/api/bookmakers/${id}`); +} + + +async function getAccounts() { + return await myFetch('/api/accounts'); } async function getAccount(id) { return await myFetch(`/api/accounts/${id}`); } -async function getRecord(id) { - return await myFetch(`/api/records/${id}`); +async function saveAccount(payload) { + return await myFetchPOST("/api/accounts", payload); } -async function saveBookmaker(payload) { - return await myFetchPOST("/api/bookmakers", payload); +async function deleteAccount(id) { + return await myFetchDELETE(`/api/accounts/${id}`); } -async function saveAccount(payload) { - return await myFetchPOST("/api/accounts", payload); + +async function getRecords() { + return await myFetch('/api/records'); +} + +async function getRecord(id) { + return await myFetch(`/api/records/${id}`); } async function saveRecord(payload) { return await myFetchPOST("/api/records", payload); } + +async function deleteRecord(id) { + return await myFetchDELETE(`/api/records/${id}`); +}
M static/js/records-edit.jsstatic/js/records-edit.js

@@ -2,6 +2,12 @@ document.addEventListener('DOMContentLoaded', function () {

handleID(); document.getElementById('save').addEventListener('click', submit); + + if (id === 0) { + document.getElementById('delete').style.display = "none"; + } else { + document.getElementById('delete').addEventListener('click', remove); + } }); let id;

@@ -155,3 +161,9 @@ if (await saveRecord(buildRecordObject())) {

location.href = "/records" } } + +async function remove() { + if (await myConfirm(deleteRecord, id)) { + location.href = "/records" + } +}
M static/records/edit/index.htmlstatic/records/edit/index.html

@@ -18,7 +18,10 @@

<div id="main-container"> </div> - <button id="save">Save</button> + <div id="buttons"> + <button id="save">Save</button> + <button id="delete">Delete</button> + </div> </main> <script src="/js/common.js"></script> <script src="/js/records-edit.js"></script>
M static/records/index.htmlstatic/records/index.html

@@ -23,7 +23,9 @@ <tfoot></tfoot>

</table> </div> - <button id="new-record">New Record</button> + <div id="buttons"> + <button id="new-record">New Record</button> + </div> </main> <script src="/js/common.js"></script> <script src="/js/records.js"></script>