create and remove entries and subentries
Marco Andronaco andronacomarco@gmail.com
Sun, 06 Oct 2024 15:38:16 +0200
3 files changed,
35 insertions(+),
3 deletions(-)
M
src/api/api.go
→
src/api/api.go
@@ -173,6 +173,16 @@ log.Println("could not decode record JSON: " + err.Error())
new400Error(w, err) return } + + if record.ID != 0 { + err = app.DB.Delete(&app.Entry{}, "record_id = ?", record.ID).Error + if err != nil { + log.Println("could not delete outdated entries: " + err.Error()) + new500Error(w, err) + return + } + } + err = app.DB.Session(&gorm.Session{FullSaveAssociations: true}).Save(&record).Error if err != nil { log.Println("could not save record: " + err.Error())
M
src/app/records.go
→
src/app/records.go
@@ -6,9 +6,9 @@ "time"
) func (e *Entry) GetOdds() *uint { - v := uint(1) + v := uint(100) for _, s := range e.SubEntries { - v *= s.Odds + v *= s.Odds / 100 } return &v }
M
static/js/records-edit.js
→
static/js/records-edit.js
@@ -34,7 +34,7 @@ // record.done
div.appendChild(newInputCheckbox("Done", record?.done, "record-done")); // record.entries - div.appendChild(loadEntries(record?.entries ?? [null], "record-entries")); + div.appendChild(loadEntries(record?.entries ?? [null, null], "record-entries")); return div; }@@ -42,6 +42,12 @@
function loadEntries(entries, name) { const div = document.createElement("div") div.className = name; + + const newEntry = document.createElement("button"); + newEntry.innerText = "Add entry"; + newEntry.onclick = () => div.appendChild(loadEntry(null)); + div.appendChild(newEntry); + for (const entry of entries) { div.appendChild(loadEntry(entry)); }@@ -54,6 +60,11 @@ div.setAttribute("data-type", "entry");
div.setAttribute("data-id", entry?.id ?? 0); div.classList.add("entry"); + const deleteEntry = document.createElement("button"); + deleteEntry.innerText = "Delete entry"; + deleteEntry.onclick = () => div.remove(); + div.appendChild(deleteEntry); + // entry.bookmaker_id div.appendChild(newInputText("Bookmaker ID", entry?.bookmaker_id, "entry-bookmaker_id"))@@ -81,6 +92,12 @@
function loadSubEntries(subEntries, name) { const div = document.createElement("div") div.className = name; + + const newSubEntry = document.createElement("button"); + newSubEntry.innerText = "Add subentry"; + newSubEntry.onclick = () => div.appendChild(loadSubEntry(null)); + div.appendChild(newSubEntry); + for (const subEntry of subEntries) { div.appendChild(loadSubEntry(subEntry)); }@@ -92,6 +109,11 @@ const div = document.createElement("div");
div.setAttribute("data-type", "subentry"); div.setAttribute("data-id", subEntry?.id ?? 0); div.classList.add("subentry"); + + const deleteSubEntry = document.createElement("button"); + deleteSubEntry.innerText = "Delete subentry"; + deleteSubEntry.onclick = () => div.remove(); + div.appendChild(deleteSubEntry); // subentry.description div.appendChild(newInputText("Description", subEntry?.description, "subentry-description"));