add delete function
Marco Andronaco andronacomarco@gmail.com
Sun, 06 Oct 2024 13:32:28 +0200
13 files changed,
152 insertions(+),
21 deletions(-)
jump to
M
src/api/api.go
→
src/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.go
→
src/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.html
→
static/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.html
→
static/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.html
→
static/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.html
→
static/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.css
→
static/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.js
→
static/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.js
→
static/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.js
→
static/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.js
→
static/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.html
→
static/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.html
→
static/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>