diff options
| author | ckrinitsin <101062646+ckrinitsin@users.noreply.github.com> | 2025-04-24 23:54:17 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-24 23:54:17 +0200 |
| commit | b75c7c6a9a040af03ef8b498c88c267bfa00aaf4 (patch) | |
| tree | 8b30f8b1269fe985a1fe0d5cca8b771a6e7484ff /handlers/shopping_list.go | |
| parent | 1e974f70a6c262d0b5db8b177ebb02b46446bfb0 (diff) | |
| download | shopping-list-b75c7c6a9a040af03ef8b498c88c267bfa00aaf4.tar.gz shopping-list-b75c7c6a9a040af03ef8b498c88c267bfa00aaf4.zip | |
Authentication (#1)
* add login and register templates * add jwt and gin-contrib dependencies * add List database table * add authentication * add logout
Diffstat (limited to 'handlers/shopping_list.go')
| -rw-r--r-- | handlers/shopping_list.go | 81 |
1 files changed, 65 insertions, 16 deletions
diff --git a/handlers/shopping_list.go b/handlers/shopping_list.go index 959c309..a1b90ff 100644 --- a/handlers/shopping_list.go +++ b/handlers/shopping_list.go @@ -2,26 +2,29 @@ package shopping_list import ( "net/http" - "os" "github.com/ckrinitsin/shopping-list/models" "github.com/gin-gonic/gin" ) -func getBasePath() string { - basePath := os.Getenv("BASE_PATH") - if basePath == "" { - basePath = "/" - } - - return basePath -} func LoadElements(c *gin.Context) { - title := "Shopping List" var entries []models.Entry + any_list, ok := c.Get("current_list") + if !ok { + c.String(http.StatusInternalServerError, "Internal Server Error") + return + } + + list, ok := any_list.(models.List) + if !ok { + c.String(http.StatusInternalServerError, "Internal Server Error") + return + } + err := models.DB. + Where("list_name = ?", list.Name). Order("checked asc"). Find(&entries). Error @@ -33,18 +36,31 @@ func LoadElements(c *gin.Context) { } c.HTML(http.StatusOK, "template.html", gin.H{ - "name": title, + "name": list.Name, "entries": entries, - "base_path": getBasePath(), + "base_path": models.BasePath(), }) } func CreateEntry(c *gin.Context) { value := c.PostForm("newItem") + any_list, ok := c.Get("current_list") + if !ok { + c.String(http.StatusInternalServerError, "Internal Server Error") + return + } + + list, ok := any_list.(models.List) + if !ok { + c.String(http.StatusInternalServerError, "Internal Server Error") + return + } + entry := models.Entry{ Text: value, Checked: false, + ListName: list.Name, } err := models.DB. @@ -56,21 +72,54 @@ func CreateEntry(c *gin.Context) { return } - c.Redirect(http.StatusFound, getBasePath() + "/") + c.Redirect(http.StatusFound, models.BasePath() + "/") } func DeleteEntries(c *gin.Context) { - models.DB.Delete(&models.Entry{}, "checked = 1") + any_list, ok := c.Get("current_list") + if !ok { + c.String(http.StatusInternalServerError, "Internal Server Error") + return + } + + list, ok := any_list.(models.List) + if !ok { + c.String(http.StatusInternalServerError, "Internal Server Error") + return + } - c.Redirect(http.StatusFound, getBasePath() + "/") + err := models.DB. + Where("list_name = ?", list.Name). + Delete(&models.Entry{}, "checked = 1"). + Error + + if err != nil { + c.String(http.StatusInternalServerError, "Internal Server Error") + return + } + + c.Redirect(http.StatusFound, models.BasePath() + "/") } func ToggleEntry(c *gin.Context) { id := c.PostForm("id") + any_list, ok := c.Get("current_list") + if !ok { + c.String(http.StatusInternalServerError, "Internal Server Error") + return + } + + list, ok := any_list.(models.List) + if !ok { + c.String(http.StatusInternalServerError, "Internal Server Error") + return + } + var entry models.Entry err := models.DB. + Where("list_name = ?", list.Name). First(&entry, id). Error @@ -89,5 +138,5 @@ func ToggleEntry(c *gin.Context) { return } - c.Redirect(http.StatusFound, getBasePath() + "/") + c.Redirect(http.StatusFound, models.BasePath() + "/") } |