summary refs log tree commit diff stats
path: root/handlers
diff options
context:
space:
mode:
Diffstat (limited to 'handlers')
-rw-r--r--handlers/shopping_list.go56
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, "/")
 }