diff options
| -rw-r--r-- | handlers/shopping_list.go | 56 | ||||
| -rw-r--r-- | models/database.go | 2 | ||||
| -rw-r--r-- | templates/template.html | 25 |
3 files changed, 69 insertions, 14 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, "/") } diff --git a/models/database.go b/models/database.go index 12d4253..2daaa1e 100644 --- a/models/database.go +++ b/models/database.go @@ -8,7 +8,7 @@ import ( ) type Entry struct { - ID uint + ID uint `gorm:"primaryKey"` Text string Checked bool CreatedAt time.Time diff --git a/templates/template.html b/templates/template.html index 5504301..5f56372 100644 --- a/templates/template.html +++ b/templates/template.html @@ -3,7 +3,7 @@ <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <title> {{ .Name }}</title> + <title> {{ .name }}</title> <style> html, body { width: 100%; @@ -166,26 +166,27 @@ </head> <body> <div class="container"> - <h1>{{ .Name }}</h1> + <h1>{{ .name }}</h1> <ul class="checklist" id="checklist"> - <li>not selected</li> - - <li> - test not selected + {{ range .entries }} + {{ if .Checked }} + <li class="selected"> + {{ else }} + <li> + {{ end }} + + {{ .Text }} <form action="/toggle" method="POST"> - <input type="hidden" name="id" value="<test-id>" /> + <input type="hidden" name="id" value="{{ .ID }}" /> <button type="submit" /> </form> </li> - - - <li class="selected">selected</li> - + {{ end }} </ul> <form action="/create" method="POST"> <div class="input-container"> - <input type="text" id="newItem" name="item" tabindex="0"/> + <input type="text" id="newItem" name="newItem" tabindex="0"/> </div> </form> |