diff options
| author | Christian Krinitsin <mail@krinitsin.com> | 2025-04-21 15:59:52 +0200 |
|---|---|---|
| committer | Christian Krinitsin <mail@krinitsin.com> | 2025-04-21 15:59:52 +0200 |
| commit | a7359cc7d19cb5bd95fd4873fb9765e5caaec63e (patch) | |
| tree | 1a902ebd3b66604dc66d7c3493916603433c444b /handlers/shopping_list.go | |
| parent | 0afc83fd6402ef8e4beb29aa77b89cae78f64b44 (diff) | |
| download | shopping-list-a7359cc7d19cb5bd95fd4873fb9765e5caaec63e.tar.gz shopping-list-a7359cc7d19cb5bd95fd4873fb9765e5caaec63e.zip | |
add all basic requests
Diffstat (limited to 'handlers/shopping_list.go')
| -rw-r--r-- | handlers/shopping_list.go | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/handlers/shopping_list.go b/handlers/shopping_list.go index bdd7468..52ee6b4 100644 --- a/handlers/shopping_list.go +++ b/handlers/shopping_list.go @@ -3,26 +3,80 @@ package shopping_list import ( "net/http" + "github.com/ckrinitsin/go-backend/models" "github.com/gin-gonic/gin" ) func LoadElements(c *gin.Context) { + + title := "Shopping List" + var entries []models.Entry + + err := models.DB. + Find(&entries). + Error + + if err != nil { + c.String(http.StatusInternalServerError, "Internal Server Error") + c.Error(err) + return + } + c.HTML(http.StatusOK, "template.html", gin.H{ - "Name": "Shopping List", + "name": title, + "entries": entries, }) } func CreateEntry(c *gin.Context) { + value := c.PostForm("newItem") + + entry := models.Entry{ + Text: value, + Checked: false, + } + + err := models.DB. + Create(&entry). + Error + + if err != nil { + c.String(http.StatusInternalServerError, "Internal Server Error") + return + } c.Redirect(http.StatusFound, "/") } func DeleteEntries(c *gin.Context) { + models.DB.Delete(&models.Entry{}, "checked = 1") c.Redirect(http.StatusFound, "/") } func ToggleEntry(c *gin.Context) { + id := c.PostForm("id") + + var entry models.Entry + + err := models.DB. + First(&entry, id). + Error + + if err != nil { + c.String(http.StatusInternalServerError, "Internal Server Error") + return + } + + entry.Checked = !entry.Checked + err = models.DB. + Save(&entry). + Error + + if err != nil { + c.String(http.StatusInternalServerError, "Internal Server Error") + return + } c.Redirect(http.StatusFound, "/") } |