all repos — cameraman @ d8fa5a3117dc6aed01befba0e8a52c726bd94d6b

major bugfix
BiRabittoh andronacomarco@gmail.com
Thu, 30 May 2024 11:51:48 +0200
commit

d8fa5a3117dc6aed01befba0e8a52c726bd94d6b

parent

85f22f5ab0e2fc67d5a8679d88a09ae77d68c246

3 files changed, 49 insertions(+), 44 deletions(-)

jump to
M handlers.gohandlers.go

@@ -28,58 +28,53 @@

return nil } -func addOccurrence(c *gin.Context) { - var input Occurrence - if err := c.ShouldBindJSON(&input); err != nil { +func updateOccurrence(c *gin.Context, input Occurrence) { + var occurrence Occurrence + if err := db.First(&occurrence, input.ID).Error; err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - var isDateValid = false - // Validate date + // Update existing record with new values if input.Day != 0 || input.Month != 0 { if err := validateDate(input.Month, input.Day); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - isDateValid = true + occurrence.Month = input.Month + occurrence.Day = input.Day } + if input.Name != "" { + occurrence.Name = input.Name + } + if input.Description != "" { + occurrence.Description = input.Description + } + occurrence.Notify = input.Notify + occurrence.Notified = input.Notified + db.Save(&occurrence) + c.JSON(http.StatusOK, occurrence) +} - var occurrence Occurrence +func addOccurrence(c *gin.Context) { + var input Occurrence + if err := c.ShouldBindJSON(&input); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + if input.ID != 0 { - if err := db.First(&occurrence, input.ID).Error; err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } - - // Update existing record with new values - if isDateValid { - occurrence.Month = input.Month - occurrence.Day = input.Day - } - if input.Name != "" { - occurrence.Name = input.Name - } - if input.Description != "" { - occurrence.Description = input.Description - } - occurrence.Notify = input.Notify - occurrence.Notified = input.Notified - db.Save(&occurrence) - c.JSON(http.StatusOK, occurrence) + updateOccurrence(c, input) return } - // Create a new record if no existing record is found - if !isDateValid { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid date"}) + if err := validateDate(input.Month, input.Day); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - occurrence = input - occurrence.Notified = false - db.Create(&occurrence) - c.JSON(http.StatusOK, occurrence) + db.Create(&input) + c.JSON(http.StatusOK, input) } func getOccurrences(c *gin.Context) {
M notify.gonotify.go

@@ -22,7 +22,7 @@ )

func notifyTelegram(occurrence Occurrence) error { log.Println("Sending notification for occurrence", occurrence.ID) - message := fmt.Sprintf("*Giorno %02d/%02d*:\n\n_%s_\n%s", + message := fmt.Sprintf("*Giorno %02d/%02d*.\n\n_%s_\n%s", occurrence.Day, occurrence.Month, occurrence.Name, occurrence.Description) url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", telegramToken)
M templates/index.htmltemplates/index.html

@@ -64,6 +64,9 @@ }

.small-input { width: 32px; } + .big-input { + width: 100%; + } .actions i { cursor: pointer;

@@ -112,6 +115,7 @@ <th>Nome</th>

<th>Descrizione</th> <th>Data (gg/mm)</th> <th>Notifica</th> + <th>Inviata</th> <th>Azioni</th> </tr> {{ range .Occurrences }}

@@ -120,6 +124,7 @@ <td>{{ .Name }}</td>

<td>{{ .Description }}</td> <td>{{ padZero .Day }}/{{ padZero .Month }}</td> <td><input type="checkbox" {{if .Notify}}checked{{end}} disabled></td> + <td><input type="checkbox" {{if .Notified}}checked{{end}} disabled></td> <td class="actions"> <i class="fas fa-edit" title="Modifica" onclick="editOccurrence('{{ .ID }}')"></i> <i class="fas fa-trash-alt" title="Elimina" onclick="deleteOccurrence('{{ .ID }}')"></i>

@@ -127,7 +132,7 @@ </td>

</tr> {{ end }} <tr id="occurrence-none" class="hidden"> - <td colspan="5">Nessuna ricorrenza.</td> + <td colspan="6">Nessuna ricorrenza.</td> </tr> </table> <div style="margin-top: 10px; text-align: center;">

@@ -185,12 +190,13 @@ function padNumber(input, n=2) {

return input.toString().padStart(n, '0'); } - function createRow(id, name, description, day, month, notify) { + function createRow(id, name, description, day, month, notify, notified) { return ` <td>${name}</td> <td>${description}</td> <td>${padNumber(day)}/${padNumber(month)}</td> <td><input type="checkbox" ${notify ? 'checked' : ''} disabled></td> + <td><input type="checkbox" ${notified ? 'checked' : ''} disabled></td> <td class="actions"> <i class="fas fa-edit" title="Edit" onclick="editOccurrence(${id})"></i> <i class="fas fa-trash-alt" title="Delete" onclick="deleteOccurrence(${id})"></i>

@@ -198,12 +204,13 @@ </td>

`; } - function createInputFields(id, name, description, day, month, notify, isNew) { + function createInputFields(id, name, description, day, month, notify, notified, isNew) { return ` - <td><input type="text" value="${name || ''}" id="name-${id}"></td> - <td><input type="text" value="${description || ''}" id="description-${id}"></td> + <td><input class="big-input" type="text" value="${name || ''}" id="name-${id}"></td> + <td><input class="big-input" type="text" value="${description || ''}" id="description-${id}"></td> <td class="date-inputs"><input type="number" value="${day || ''}" id="day-${id}" class="small-input" min="1" max="31" onchange="this.value = padNumber(this.value);"> / <input type="number" value="${month || ''}" id="month-${id}" class="small-input" min="1" max="12" onchange="this.value = padNumber(this.value);"></td> <td><input type="checkbox" id="notify-${id}" ${notify ? 'checked' : ''}></td> + <td><input type="checkbox" id="notified-${id}" ${notified ? 'checked' : ''}></td> <td class="actions"> ${isNew ? '' : ` <i class="fas fa-save" title="Save" onclick="saveOccurrence(${id})"></i>

@@ -221,8 +228,9 @@ const name = cells[0].innerText;

const description = cells[1].innerText; const [day, month] = cells[2].innerText.split('/'); const notify = cells[3].getElementsByTagName('input')[0].checked; + const notified = cells[4].getElementsByTagName('input')[0].checked; - row.innerHTML = createInputFields(id, name, description, day, month, notify, false); + row.innerHTML = createInputFields(id, name, description, day, month, notify, notified, false); } function cancelEdit(id, name, description, day, month, notify) {

@@ -236,6 +244,7 @@ const description = document.getElementById(`description-${id}`).value;

const day = parseInt(document.getElementById(`day-${id}`).value); const month = parseInt(document.getElementById(`month-${id}`).value); const notify = document.getElementById(`notify-${id}`).checked; + const notified = document.getElementById(`notified-${id}`).checked; const isNew = id === '0'; const updatedData = {

@@ -244,7 +253,8 @@ name: name,

description: description, month: month, day: day, - notify: notify + notify: notify, + notified: notified }; fetch('/occurrences', {

@@ -275,7 +285,7 @@

function addNewOccurrenceRow() { const newRow = mainTable.insertRow(-1); newRow.id = 'new-occurrence'; - newRow.innerHTML = createInputFields('0', '', '', '', '', true, true); + newRow.innerHTML = createInputFields('0', '', '', '', '', true, false, true); hideAddButton(); updateNoneRowDisplay();

@@ -296,7 +306,7 @@ function updateRow(rowElementId, response) {

const newRow = document.getElementById(rowElementId); response.json().then((res) => { newRow.id = `occurrence-${res.id}`; - newRow.innerHTML = createRow(res.id, res.name, res.description, res.day, res.month, res.notify); + newRow.innerHTML = createRow(res.id, res.name, res.description, res.day, res.month, res.notify, res.notified); updateNoneRowDisplay() }); }